Skip to content

Commit

Permalink
fmt content length
Browse files Browse the repository at this point in the history
  • Loading branch information
Sianao committed Nov 25, 2024
1 parent 8976209 commit 5d3bc78
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ dist

# DynamoDB Local files
.dynamodb/

./.idea
# TernJS port file
.tern-port

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
7 changes: 6 additions & 1 deletion handler/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handler

import (
"context"
"github.com/dustin/go-humanize"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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方便进入路由匹配
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 3 additions & 7 deletions service/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"net/http"
"time"
)

type consoleColorModeValue int
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down

0 comments on commit 5d3bc78

Please sign in to comment.