diff --git a/.gitignore b/.gitignore index a537cff..e525866 100644 --- a/.gitignore +++ b/.gitignore @@ -115,7 +115,7 @@ dist # DynamoDB Local files .dynamodb/ - +./.idea # TernJS port file .tern-port diff --git a/go.mod b/go.mod index a20c283..fe1c8dd 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module github.com/sianao/gitproxy go 1.22.5 require github.com/gorilla/mux v1.8.1 + +require github.com/dustin/go-humanize v1.0.1 // indirect diff --git a/go.sum b/go.sum index 7128337..94d676b 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/handler/url.go b/handler/url.go index 4656763..925f61e 100644 --- a/handler/url.go +++ b/handler/url.go @@ -2,8 +2,10 @@ package handler import ( "context" + "github.com/dustin/go-humanize" "net/http" "net/url" + "strconv" "strings" "github.com/gorilla/mux" @@ -64,8 +66,11 @@ func NewHandler(route *mux.Router) http.HandlerFunc { if !ok { v = []string{r.RemoteAddr} } + length, _ := strconv.Atoi(w.Header().Get("Content-Length")) + service.DefaultLogFormatter( - service.LogFormatterParams{StatusCode: 404, ClientIP: v[0], Method: r.Method, Path: r.URL.Path}) + service.LogFormatterParams{StatusCode: 404, + ContentLength: humanize.Bytes(uint64(length)), ClientIP: v[0], Method: r.Method, Path: r.URL.Path}) return } //去除掉host方便进入路由匹配 diff --git a/main.go b/main.go index 40b57c1..f1a7523 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,9 @@ import ( ) func main() { - // r := gin.Default() - router := router.NewRouter() + newRouter := router.NewRouter() srv := &http.Server{ - Handler: handler.NewHandler(router), + Handler: handler.NewHandler(newRouter), Addr: "0.0.0.0:8888", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, diff --git a/service/log.go b/service/log.go index aa9bc99..b583ee7 100644 --- a/service/log.go +++ b/service/log.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "net/http" - "time" ) type consoleColorModeValue int @@ -55,9 +54,9 @@ type LogFormatterParams struct { // TimeStamp shows the time after the server returns a response. // StatusCode is HTTP response code. - StatusCode int + StatusCode int + ContentLength string // Latency is how much time the server cost to process a certain request. - Latency time.Duration // ClientIP equals Context's ClientIP method. ClientIP string // Method is the HTTP method given to the request. @@ -129,12 +128,9 @@ var DefaultLogFormatter = func(param LogFormatterParams) { resetColor = param.ResetColor() } - if param.Latency > time.Minute { - param.Latency = param.Latency.Truncate(time.Second) - } fmt.Printf("[GitProxy] |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s", statusColor, param.StatusCode, resetColor, - param.Latency, + param.ContentLength, param.ClientIP, methodColor, param.Method, resetColor, param.Path, diff --git a/service/proxy.go b/service/proxy.go index 5dc3bd9..8caeea8 100644 --- a/service/proxy.go +++ b/service/proxy.go @@ -2,8 +2,10 @@ package service import ( "fmt" + "github.com/dustin/go-humanize" "io" "net/http" + "strconv" ) func PacketProxy(w http.ResponseWriter, r *http.Request, address string) { @@ -24,7 +26,12 @@ func PacketProxy(w http.ResponseWriter, r *http.Request, address string) { if !ok { v = []string{r.RemoteAddr} } - DefaultLogFormatter(LogFormatterParams{StatusCode: resp.StatusCode, ClientIP: v[0], Method: r.Method, Path: r.URL.Path}) + length, _ := strconv.Atoi(w.Header().Get("Content-Length")) + + DefaultLogFormatter( + LogFormatterParams{StatusCode: resp.StatusCode, + ContentLength: humanize.Bytes(uint64(length)), ClientIP: v[0], Method: r.Method, Path: r.URL.Path}) + w.WriteHeader(resp.StatusCode) io.Copy(w, resp.Body) }