日志的优雅写法
This commit is contained in:
		
							
								
								
									
										26
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -1,9 +1,11 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@@ -12,9 +14,9 @@ import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
 | 
			
		||||
}
 | 
			
		||||
//func init() {
 | 
			
		||||
//	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
 | 
			
		||||
//}
 | 
			
		||||
 | 
			
		||||
// string 转换为 int, 如果转换失败则返回默认值
 | 
			
		||||
func stringToInt(str string, defaultValue int) int {
 | 
			
		||||
@@ -28,6 +30,11 @@ func stringToInt(str string, defaultValue int) int {
 | 
			
		||||
	return value
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LogComponent(startTime int64, r *http.Request) {
 | 
			
		||||
	endTime := fmt.Sprintf("%dms", (time.Now().UnixNano()-startTime)/1000000)
 | 
			
		||||
	log.Println(r.Method, r.URL.Path, endTime)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	runtime.GOMAXPROCS(runtime.NumCPU())
 | 
			
		||||
 | 
			
		||||
@@ -36,19 +43,19 @@ func main() {
 | 
			
		||||
 | 
			
		||||
	// URL 格式: /img/{type}-{id}.{format}?width=320&height=320&fit=cover
 | 
			
		||||
	http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		log.Println(r.Method, r.URL.Path)
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
 | 
			
		||||
		reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`)
 | 
			
		||||
		matches := reg.FindStringSubmatch(r.URL.Path)
 | 
			
		||||
		if len(matches) != 4 {
 | 
			
		||||
			log.Println("URL 格式错误", matches)
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			http.Error(w, "URL 格式错误", http.StatusNotFound)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		group, id, format, width, height, fit := matches[1], matches[2], matches[3], stringToInt(r.URL.Query().Get("width"), 0), stringToInt(r.URL.Query().Get("height"), 0), r.URL.Query().Get("fit")
 | 
			
		||||
		content, err := mysqlConnection.GetImageContent(group, id)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取图片失败", format, err)
 | 
			
		||||
			w.WriteHeader(http.StatusNotFound)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusNotFound)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		var img models.Image
 | 
			
		||||
@@ -56,7 +63,7 @@ func main() {
 | 
			
		||||
		data, err := img.ToWebP(width, height, fit)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("转换图片失败", err)
 | 
			
		||||
			w.WriteHeader(http.StatusBadRequest)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -67,7 +74,8 @@ func main() {
 | 
			
		||||
 | 
			
		||||
	// URL 格式: /webp/{type}-{id}-{version}-{width}-{height}-{fit}.{format}
 | 
			
		||||
	http.HandleFunc("/webp/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		log.Println(r.Method, r.URL.Path)
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
 | 
			
		||||
		reg := regexp.MustCompile(`^/webp/([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+).(jpg|jpeg|png|webp)$`)
 | 
			
		||||
		matches := reg.FindStringSubmatch(r.URL.Path)
 | 
			
		||||
		if len(matches) != 8 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user