合併
This commit is contained in:
		
							
								
								
									
										52
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								main.go
									
									
									
									
									
								
							@@ -2,18 +2,60 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"runtime"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
						runtime.GOMAXPROCS(runtime.NumCPU())
 | 
				
			||||||
	fmt.Println("Hello, World!")
 | 
						fmt.Println("Hello, World!")
 | 
				
			||||||
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
						http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						http.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						http.HandleFunc("/models", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						http.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						http.HandleFunc("/tags", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
		fmt.Fprintf(w, "Hello, World!")
 | 
							fmt.Fprintf(w, "Hello, World!")
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	http.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {})
 | 
					 | 
				
			||||||
	http.HandleFunc("/models", func(w http.ResponseWriter, r *http.Request) {})
 | 
					 | 
				
			||||||
	http.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {})
 | 
					 | 
				
			||||||
	http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {})
 | 
					 | 
				
			||||||
	http.HandleFunc("/tags", func(w http.ResponseWriter, r *http.Request) {})
 | 
					 | 
				
			||||||
	http.ListenAndServe(":8080", nil)
 | 
						http.ListenAndServe(":8080", nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func LogComponent(startTime int64, r *http.Request) {
 | 
				
			||||||
 | 
						ms := (time.Now().UnixNano() - startTime) / 1000000
 | 
				
			||||||
 | 
						color := "\033[1;32m%d\033[0m"
 | 
				
			||||||
 | 
						if ms > 800 {
 | 
				
			||||||
 | 
							color = "\033[1;31m%dms\033[0m" // 紅色加重
 | 
				
			||||||
 | 
						} else if ms > 500 {
 | 
				
			||||||
 | 
							color = "\033[1;33m%dms\033[0m" // 黃色加重
 | 
				
			||||||
 | 
						} else if ms > 300 {
 | 
				
			||||||
 | 
							color = "\033[1;32m%dms\033[0m" // 綠色加重
 | 
				
			||||||
 | 
						} else if ms > 200 {
 | 
				
			||||||
 | 
							color = "\033[1;34m%dms\033[0m" // 藍色加重
 | 
				
			||||||
 | 
						} else if ms > 100 {
 | 
				
			||||||
 | 
							color = "\033[1;35m%dms\033[0m" // 紫色加重
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							color = "\033[1;36m%dms\033[0m" // 黑色加重
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						endTime := fmt.Sprintf(color, ms)
 | 
				
			||||||
 | 
						method := fmt.Sprintf("\033[1;32m%s\033[0m", r.Method) // 綠色加重
 | 
				
			||||||
 | 
						url := fmt.Sprintf("\033[1;34m%s\033[0m", r.URL)       // 藍色加重
 | 
				
			||||||
 | 
						log.Println(method, url, endTime)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user