diff --git a/router/default.go b/router/default.go index 4b6fba2..6efc371 100644 --- a/router/default.go +++ b/router/default.go @@ -2,12 +2,13 @@ package router import ( "fmt" - "github.com/dustin/go-humanize" - "github.com/sianao/gitproxy/cache" "net/http" "strconv" "strings" + "github.com/dustin/go-humanize" + "github.com/sianao/gitproxy/cache" + "github.com/gorilla/mux" "github.com/sianao/gitproxy/moudule" "github.com/sianao/gitproxy/service" @@ -86,7 +87,7 @@ func NewRouter(c *cache.Redis) *mux.Router { userBaisc[0], userBaisc[1], r.URL.Path) path := fmt.Sprintf("raw.githubusercontent.com/%s/%s%s", userBaisc[0], userBaisc[1], r.URL.Path) - c.Incr(path) + if c.Exists(path) { r.URL.Path = path http.FileServer(http.Dir("./cache/")).ServeHTTP(w, r) @@ -102,6 +103,7 @@ func NewRouter(c *cache.Redis) *mux.Router { return } + c.Incr(path) service.PacketProxy(w, r, address) }) diff --git a/service/proxy.go b/service/proxy.go index 8caeea8..f42ff6e 100644 --- a/service/proxy.go +++ b/service/proxy.go @@ -2,10 +2,11 @@ package service import ( "fmt" - "github.com/dustin/go-humanize" "io" "net/http" "strconv" + + "github.com/dustin/go-humanize" ) func PacketProxy(w http.ResponseWriter, r *http.Request, address string) { @@ -35,16 +36,3 @@ func PacketProxy(w http.ResponseWriter, r *http.Request, address string) { w.WriteHeader(resp.StatusCode) io.Copy(w, resp.Body) } - -// // StatusCode is HTTP response code. -// StatusCode int -// // 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. -// Method string -// // Path is a path the client requests. -// Path string -// // ErrorMessage is set if error has occurred in processing the request. -// ErrorMessage string