diff --git a/example/graceful/dns/api/handler/gracefulhandler.go b/example/graceful/dns/api/handler/gracefulhandler.go index 43268d68..1332a1d0 100644 --- a/example/graceful/dns/api/handler/gracefulhandler.go +++ b/example/graceful/dns/api/handler/gracefulhandler.go @@ -8,11 +8,11 @@ import ( "time" "zero/core/executors" - "zero/core/httpx" "zero/core/logx" "zero/example/graceful/dns/api/svc" "zero/example/graceful/dns/api/types" "zero/example/graceful/dns/rpc/graceful" + "zero/ngin/httpx" ) func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc { diff --git a/example/graceful/etcd/api/handler/gracefulhandler.go b/example/graceful/etcd/api/handler/gracefulhandler.go index a875016b..04d49c1d 100644 --- a/example/graceful/etcd/api/handler/gracefulhandler.go +++ b/example/graceful/etcd/api/handler/gracefulhandler.go @@ -8,11 +8,11 @@ import ( "time" "zero/core/executors" - "zero/core/httpx" "zero/core/logx" "zero/example/graceful/etcd/api/svc" "zero/example/graceful/etcd/api/types" "zero/example/graceful/etcd/rpc/graceful" + "zero/ngin/httpx" ) func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc { diff --git a/example/http/demo/main.go b/example/http/demo/main.go index 93fb986a..1c42e1d2 100644 --- a/example/http/demo/main.go +++ b/example/http/demo/main.go @@ -4,10 +4,10 @@ import ( "flag" "net/http" - "zero/core/httpx" "zero/core/logx" "zero/core/service" "zero/ngin" + "zero/ngin/httpx" ) var ( diff --git a/example/http/post/main.go b/example/http/post/main.go index 4a8fc175..45b5f72e 100644 --- a/example/http/post/main.go +++ b/example/http/post/main.go @@ -5,10 +5,10 @@ import ( "fmt" "net/http" - "zero/core/httpx" "zero/core/logx" "zero/core/service" "zero/ngin" + "zero/ngin/httpx" ) var ( diff --git a/example/http/shedding/main.go b/example/http/shedding/main.go index 887ae854..d5cda076 100644 --- a/example/http/shedding/main.go +++ b/example/http/shedding/main.go @@ -6,10 +6,10 @@ import ( "net/http" "time" - "zero/core/httpx" "zero/core/logx" "zero/core/service" "zero/ngin" + "zero/ngin/httpx" ) var ( diff --git a/example/http/signature/server/server.go b/example/http/signature/server/server.go index 5833ac5b..44c5152b 100644 --- a/example/http/signature/server/server.go +++ b/example/http/signature/server/server.go @@ -5,10 +5,10 @@ import ( "io" "net/http" - "zero/core/httpx" "zero/core/logx" "zero/core/service" "zero/ngin" + "zero/ngin/httpx" ) var keyPem = flag.String("prikey", "private.pem", "the private key file") diff --git a/example/jwt/user/user.go b/example/jwt/user/user.go index d36caac0..cba8f02a 100644 --- a/example/jwt/user/user.go +++ b/example/jwt/user/user.go @@ -9,8 +9,8 @@ import ( "time" "zero/core/conf" - "zero/core/httpx" "zero/ngin" + "zero/ngin/httpx" "github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go/request" diff --git a/example/tracing/edge/main.go b/example/tracing/edge/main.go index 20e7b164..8b530d11 100644 --- a/example/tracing/edge/main.go +++ b/example/tracing/edge/main.go @@ -6,11 +6,11 @@ import ( "net/http" "zero/core/conf" - "zero/core/httpx" "zero/core/logx" "zero/core/service" "zero/example/tracing/remote/portal" "zero/ngin" + "zero/ngin/httpx" "zero/rpcx" ) diff --git a/core/httphandler/authhandler.go b/ngin/handler/authhandler.go similarity index 96% rename from core/httphandler/authhandler.go rename to ngin/handler/authhandler.go index 5e9b3e75..7971e079 100644 --- a/core/httphandler/authhandler.go +++ b/ngin/handler/authhandler.go @@ -1,12 +1,12 @@ -package httphandler +package handler import ( "context" "net/http" "net/http/httputil" - "zero/core/httpsecurity" "zero/core/logx" + "zero/ngin/internal" "github.com/dgrijalva/jwt-go" ) @@ -37,7 +37,7 @@ func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.H opt(&authOpts) } - parser := httpsecurity.NewTokenParser() + parser := internal.NewTokenParser() return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { token, err := parser.ParseToken(r, secret, authOpts.PrevSecret) diff --git a/core/httphandler/authhandler_test.go b/ngin/handler/authhandler_test.go similarity index 99% rename from core/httphandler/authhandler_test.go rename to ngin/handler/authhandler_test.go index 7e89a875..43395222 100644 --- a/core/httphandler/authhandler_test.go +++ b/ngin/handler/authhandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/breakerhandler.go b/ngin/handler/breakerhandler.go similarity index 82% rename from core/httphandler/breakerhandler.go rename to ngin/handler/breakerhandler.go index 9c1325b2..50487148 100644 --- a/core/httphandler/breakerhandler.go +++ b/ngin/handler/breakerhandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "fmt" @@ -6,10 +6,10 @@ import ( "strings" "zero/core/breaker" - "zero/core/httphandler/internal" - "zero/core/httpx" "zero/core/logx" "zero/core/stat" + "zero/ngin/internal" + "zero/ngin/internal/security" ) const breakerSeparator = "://" @@ -22,12 +22,12 @@ func BreakerHandler(method, path string, metrics *stat.Metrics) func(http.Handle if err != nil { metrics.AddDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } - cw := &internal.WithCodeResponseWriter{Writer: w} + cw := &security.WithCodeResponseWriter{Writer: w} defer func() { if cw.Code < http.StatusInternalServerError { promise.Accept() diff --git a/core/httphandler/breakerhandler_test.go b/ngin/handler/breakerhandler_test.go similarity index 99% rename from core/httphandler/breakerhandler_test.go rename to ngin/handler/breakerhandler_test.go index 230e2231..31e2323c 100644 --- a/core/httphandler/breakerhandler_test.go +++ b/ngin/handler/breakerhandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "fmt" diff --git a/core/httphandler/contentsecurityhandler.go b/ngin/handler/contentsecurityhandler.go similarity index 89% rename from core/httphandler/contentsecurityhandler.go rename to ngin/handler/contentsecurityhandler.go index 404fc6cb..8f78ff55 100644 --- a/core/httphandler/contentsecurityhandler.go +++ b/ngin/handler/contentsecurityhandler.go @@ -1,13 +1,13 @@ -package httphandler +package handler import ( "net/http" "time" "zero/core/codec" - "zero/core/httphandler/internal" - "zero/core/httpx" "zero/core/logx" + "zero/ngin/httpx" + "zero/ngin/internal/security" ) const contentSecurity = "X-Content-Security" @@ -24,12 +24,12 @@ func ContentSecurityHandler(decrypters map[string]codec.RsaDecrypter, tolerance return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodDelete, http.MethodGet, http.MethodPost, http.MethodPut: - header, err := internal.ParseContentSecurity(decrypters, r) + header, err := security.ParseContentSecurity(decrypters, r) if err != nil { logx.Infof("Signature parse failed, X-Content-Security: %s, error: %s", r.Header.Get(contentSecurity), err.Error()) executeCallbacks(w, r, next, strict, httpx.CodeSignatureInvalidHeader, callbacks) - } else if code := internal.VerifySignature(r, header, tolerance); code != httpx.CodeSignaturePass { + } else if code := security.VerifySignature(r, header, tolerance); code != httpx.CodeSignaturePass { logx.Infof("Signature verification failed, X-Content-Security: %s", r.Header.Get(contentSecurity)) executeCallbacks(w, r, next, strict, code, callbacks) diff --git a/core/httphandler/contentsecurityhandler_test.go b/ngin/handler/contentsecurityhandler_test.go similarity index 99% rename from core/httphandler/contentsecurityhandler_test.go rename to ngin/handler/contentsecurityhandler_test.go index b22dfeff..85e84540 100644 --- a/core/httphandler/contentsecurityhandler_test.go +++ b/ngin/handler/contentsecurityhandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" @@ -18,7 +18,7 @@ import ( "time" "zero/core/codec" - "zero/core/httpx" + "zero/ngin/httpx" "github.com/stretchr/testify/assert" ) diff --git a/core/httphandler/cryptionhandler.go b/ngin/handler/cryptionhandler.go similarity index 99% rename from core/httphandler/cryptionhandler.go rename to ngin/handler/cryptionhandler.go index 7aeccd5f..3a256ad9 100644 --- a/core/httphandler/cryptionhandler.go +++ b/ngin/handler/cryptionhandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" diff --git a/core/httphandler/cryptionhandler_test.go b/ngin/handler/cryptionhandler_test.go similarity index 99% rename from core/httphandler/cryptionhandler_test.go rename to ngin/handler/cryptionhandler_test.go index caaeaa51..fcb8fff8 100644 --- a/core/httphandler/cryptionhandler_test.go +++ b/ngin/handler/cryptionhandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" diff --git a/core/httphandler/gunziphandler.go b/ngin/handler/gunziphandler.go similarity index 92% rename from core/httphandler/gunziphandler.go rename to ngin/handler/gunziphandler.go index ede0209c..5788038d 100644 --- a/core/httphandler/gunziphandler.go +++ b/ngin/handler/gunziphandler.go @@ -1,11 +1,11 @@ -package httphandler +package handler import ( "compress/gzip" "net/http" "strings" - "zero/core/httpx" + "zero/ngin/httpx" ) const gzipEncoding = "gzip" diff --git a/core/httphandler/gunziphandler_test.go b/ngin/handler/gunziphandler_test.go similarity index 97% rename from core/httphandler/gunziphandler_test.go rename to ngin/handler/gunziphandler_test.go index 824c9fcd..a2a2b5e7 100644 --- a/core/httphandler/gunziphandler_test.go +++ b/ngin/handler/gunziphandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" @@ -10,7 +10,7 @@ import ( "testing" "zero/core/codec" - "zero/core/httpx" + "zero/ngin/httpx" "github.com/stretchr/testify/assert" ) diff --git a/core/httphandler/loghandler.go b/ngin/handler/loghandler.go similarity index 88% rename from core/httphandler/loghandler.go rename to ngin/handler/loghandler.go index 49e02a83..1bbe8694 100644 --- a/core/httphandler/loghandler.go +++ b/ngin/handler/loghandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" @@ -9,12 +9,11 @@ import ( "net/http/httputil" "time" - "zero/core/httplog" - "zero/core/httpx" "zero/core/iox" "zero/core/logx" "zero/core/timex" "zero/core/utils" + "zero/ngin/internal" ) const slowThreshold = time.Millisecond * 500 @@ -41,7 +40,7 @@ func (w *LoggedResponseWriter) WriteHeader(code int) { func LogHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer := utils.NewElapsedTimer() - logs := new(httplog.LogCollector) + logs := new(internal.LogCollector) lrw := LoggedResponseWriter{ w: w, r: r, @@ -50,7 +49,7 @@ func LogHandler(next http.Handler) http.Handler { var dup io.ReadCloser r.Body, dup = iox.DupReadCloser(r.Body) - next.ServeHTTP(&lrw, r.WithContext(context.WithValue(r.Context(), httplog.LogContext, logs))) + next.ServeHTTP(&lrw, r.WithContext(context.WithValue(r.Context(), internal.LogContext, logs))) r.Body = dup logBrief(r, lrw.code, timer, logs) }) @@ -93,8 +92,8 @@ func DetailedLogHandler(next http.Handler) http.Handler { var dup io.ReadCloser r.Body, dup = iox.DupReadCloser(r.Body) - logs := new(httplog.LogCollector) - next.ServeHTTP(lrw, r.WithContext(context.WithValue(r.Context(), httplog.LogContext, logs))) + logs := new(internal.LogCollector) + next.ServeHTTP(lrw, r.WithContext(context.WithValue(r.Context(), internal.LogContext, logs))) r.Body = dup logDetails(r, lrw, timer, logs) }) @@ -109,14 +108,14 @@ func dumpRequest(r *http.Request) string { } } -func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *httplog.LogCollector) { +func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *internal.LogCollector) { var buf bytes.Buffer duration := timer.Duration() buf.WriteString(fmt.Sprintf("%d - %s - %s - %s - %s", - code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) + code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) if duration > slowThreshold { logx.Slowf("[HTTP] %d - %s - %s - %s - slowcall(%s)", - code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) + code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) } ok := isOkResponse(code) @@ -137,7 +136,7 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *httplo } func logDetails(r *http.Request, response *DetailLoggedResponseWriter, timer *utils.ElapsedTimer, - logs *httplog.LogCollector) { + logs *internal.LogCollector) { var buf bytes.Buffer duration := timer.Duration() buf.WriteString(fmt.Sprintf("%d - %s - %s\n=> %s\n", diff --git a/core/httphandler/loghandler_test.go b/ngin/handler/loghandler_test.go similarity index 93% rename from core/httphandler/loghandler_test.go rename to ngin/handler/loghandler_test.go index aee7f520..d57ddf7d 100644 --- a/core/httphandler/loghandler_test.go +++ b/ngin/handler/loghandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "io/ioutil" @@ -8,9 +8,9 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" + "zero/ngin/internal" - "zero/core/httplog" + "github.com/stretchr/testify/assert" ) func init() { @@ -26,7 +26,7 @@ func TestLogHandler(t *testing.T) { for _, logHandler := range handlers { req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - r.Context().Value(httplog.LogContext).(*httplog.LogCollector).Append("anything") + r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything") w.Header().Set("X-Test", "test") w.WriteHeader(http.StatusServiceUnavailable) _, err := w.Write([]byte("content")) diff --git a/core/httphandler/maxbyteshandler.go b/ngin/handler/maxbyteshandler.go similarity index 77% rename from core/httphandler/maxbyteshandler.go rename to ngin/handler/maxbyteshandler.go index bcb534d1..110ac7bc 100644 --- a/core/httphandler/maxbyteshandler.go +++ b/ngin/handler/maxbyteshandler.go @@ -1,9 +1,9 @@ -package httphandler +package handler import ( "net/http" - "zero/core/httplog" + "zero/ngin/internal" ) func MaxBytesHandler(n int64) func(http.Handler) http.Handler { @@ -16,7 +16,7 @@ func MaxBytesHandler(n int64) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.ContentLength > n { - httplog.Errorf(r, "request entity too large, limit is %d, but got %d, rejected with code %d", + internal.Errorf(r, "request entity too large, limit is %d, but got %d, rejected with code %d", n, r.ContentLength, http.StatusRequestEntityTooLarge) w.WriteHeader(http.StatusRequestEntityTooLarge) } else { diff --git a/core/httphandler/maxbyteshandler_test.go b/ngin/handler/maxbyteshandler_test.go similarity index 98% rename from core/httphandler/maxbyteshandler_test.go rename to ngin/handler/maxbyteshandler_test.go index 760a6954..ddb1035f 100644 --- a/core/httphandler/maxbyteshandler_test.go +++ b/ngin/handler/maxbyteshandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "bytes" diff --git a/core/httphandler/maxconnshandler.go b/ngin/handler/maxconnshandler.go similarity index 84% rename from core/httphandler/maxconnshandler.go rename to ngin/handler/maxconnshandler.go index c96c53ef..8c43c2e9 100644 --- a/core/httphandler/maxconnshandler.go +++ b/ngin/handler/maxconnshandler.go @@ -1,11 +1,11 @@ -package httphandler +package handler import ( "net/http" - "zero/core/httplog" "zero/core/logx" "zero/core/syncx" + "zero/ngin/internal" ) func MaxConns(n int) func(http.Handler) http.Handler { @@ -28,7 +28,7 @@ func MaxConns(n int) func(http.Handler) http.Handler { next.ServeHTTP(w, r) } else { - httplog.Errorf(r, "Concurrent connections over %d, rejected with code %d", + internal.Errorf(r, "Concurrent connections over %d, rejected with code %d", n, http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable) } diff --git a/core/httphandler/maxconnshandler_test.go b/ngin/handler/maxconnshandler_test.go similarity index 98% rename from core/httphandler/maxconnshandler_test.go rename to ngin/handler/maxconnshandler_test.go index 979cb15b..40a331c3 100644 --- a/core/httphandler/maxconnshandler_test.go +++ b/ngin/handler/maxconnshandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "io/ioutil" diff --git a/core/httphandler/metrichandler.go b/ngin/handler/metrichandler.go similarity index 95% rename from core/httphandler/metrichandler.go rename to ngin/handler/metrichandler.go index 61ffb9fb..ab4e4995 100644 --- a/core/httphandler/metrichandler.go +++ b/ngin/handler/metrichandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/metrichandler_test.go b/ngin/handler/metrichandler_test.go similarity index 96% rename from core/httphandler/metrichandler_test.go rename to ngin/handler/metrichandler_test.go index 1ad50360..3e8db856 100644 --- a/core/httphandler/metrichandler_test.go +++ b/ngin/handler/metrichandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/prommetrichandler.go b/ngin/handler/prommetrichandler.go similarity index 91% rename from core/httphandler/prommetrichandler.go rename to ngin/handler/prommetrichandler.go index f15fa3d8..66ce88ad 100644 --- a/core/httphandler/prommetrichandler.go +++ b/ngin/handler/prommetrichandler.go @@ -1,13 +1,13 @@ -package httphandler +package handler import ( "net/http" "strconv" "time" - "zero/core/httphandler/internal" "zero/core/metric" "zero/core/timex" + "zero/ngin/internal/security" ) const serverNamespace = "http_server" @@ -35,7 +35,7 @@ func PromMetricHandler(path string) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { startTime := timex.Now() - cw := &internal.WithCodeResponseWriter{Writer: w} + cw := &security.WithCodeResponseWriter{Writer: w} defer func() { metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), path) metricServerReqCodeTotal.Inc(path, strconv.Itoa(cw.Code)) diff --git a/core/httphandler/prommetrichandler_test.go b/ngin/handler/prommetrichandler_test.go similarity index 96% rename from core/httphandler/prommetrichandler_test.go rename to ngin/handler/prommetrichandler_test.go index 156419ea..2db66220 100644 --- a/core/httphandler/prommetrichandler_test.go +++ b/ngin/handler/prommetrichandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/recoverhandler.go b/ngin/handler/recoverhandler.go similarity index 75% rename from core/httphandler/recoverhandler.go rename to ngin/handler/recoverhandler.go index aca99c86..fed8ba02 100644 --- a/core/httphandler/recoverhandler.go +++ b/ngin/handler/recoverhandler.go @@ -1,18 +1,18 @@ -package httphandler +package handler import ( "fmt" "net/http" "runtime/debug" - "zero/core/httplog" + "zero/ngin/internal" ) func RecoverHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { if result := recover(); result != nil { - httplog.Error(r, fmt.Sprintf("%v\n%s", result, debug.Stack())) + internal.Error(r, fmt.Sprintf("%v\n%s", result, debug.Stack())) w.WriteHeader(http.StatusInternalServerError) } }() diff --git a/core/httphandler/recoverhandler_test.go b/ngin/handler/recoverhandler_test.go similarity index 97% rename from core/httphandler/recoverhandler_test.go rename to ngin/handler/recoverhandler_test.go index 1281bdec..1ea491f8 100644 --- a/core/httphandler/recoverhandler_test.go +++ b/ngin/handler/recoverhandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "io/ioutil" diff --git a/core/httphandler/sheddinghandler.go b/ngin/handler/sheddinghandler.go similarity index 85% rename from core/httphandler/sheddinghandler.go rename to ngin/handler/sheddinghandler.go index 6fac2c20..918cba23 100644 --- a/core/httphandler/sheddinghandler.go +++ b/ngin/handler/sheddinghandler.go @@ -1,14 +1,14 @@ -package httphandler +package handler import ( "net/http" "sync" - "zero/core/httphandler/internal" - "zero/core/httpx" "zero/core/load" "zero/core/logx" "zero/core/stat" + "zero/ngin/internal" + "zero/ngin/internal/security" ) const serviceType = "api" @@ -35,12 +35,12 @@ func SheddingHandler(shedder load.Shedder, metrics *stat.Metrics) func(http.Hand metrics.AddDrop() sheddingStat.IncrementDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } - cw := &internal.WithCodeResponseWriter{Writer: w} + cw := &security.WithCodeResponseWriter{Writer: w} defer func() { if cw.Code == http.StatusServiceUnavailable { promise.Fail() diff --git a/core/httphandler/sheddinghandler_test.go b/ngin/handler/sheddinghandler_test.go similarity index 99% rename from core/httphandler/sheddinghandler_test.go rename to ngin/handler/sheddinghandler_test.go index 9eb741f7..415e4540 100644 --- a/core/httphandler/sheddinghandler_test.go +++ b/ngin/handler/sheddinghandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "io/ioutil" diff --git a/core/httphandler/timeouthandler.go b/ngin/handler/timeouthandler.go similarity index 93% rename from core/httphandler/timeouthandler.go rename to ngin/handler/timeouthandler.go index f57d5ead..c8f0941d 100644 --- a/core/httphandler/timeouthandler.go +++ b/ngin/handler/timeouthandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/timeouthandler_test.go b/ngin/handler/timeouthandler_test.go similarity index 98% rename from core/httphandler/timeouthandler_test.go rename to ngin/handler/timeouthandler_test.go index 4f4d086c..e1a7ecd7 100644 --- a/core/httphandler/timeouthandler_test.go +++ b/ngin/handler/timeouthandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "io/ioutil" diff --git a/core/httphandler/tracinghandler.go b/ngin/handler/tracinghandler.go similarity index 96% rename from core/httphandler/tracinghandler.go rename to ngin/handler/tracinghandler.go index 49456660..ed45ac17 100644 --- a/core/httphandler/tracinghandler.go +++ b/ngin/handler/tracinghandler.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httphandler/tracinghandler_test.go b/ngin/handler/tracinghandler_test.go similarity index 96% rename from core/httphandler/tracinghandler_test.go rename to ngin/handler/tracinghandler_test.go index f1261f02..9b389179 100644 --- a/core/httphandler/tracinghandler_test.go +++ b/ngin/handler/tracinghandler_test.go @@ -1,4 +1,4 @@ -package httphandler +package handler import ( "net/http" diff --git a/core/httpx/constants.go b/ngin/httpx/constants.go similarity index 100% rename from core/httpx/constants.go rename to ngin/httpx/constants.go diff --git a/core/httpx/requests.go b/ngin/httpx/requests.go similarity index 86% rename from core/httpx/requests.go rename to ngin/httpx/requests.go index b2cb9623..ab581a37 100644 --- a/core/httpx/requests.go +++ b/ngin/httpx/requests.go @@ -1,18 +1,16 @@ package httpx import ( - "errors" "io" "net/http" "strings" - "zero/core/httprouter" "zero/core/mapping" + "zero/ngin/internal/context" ) const ( multipartFormData = "multipart/form-data" - xForwardFor = "X-Forward-For" formKey = "form" pathKey = "path" emptyJson = "{}" @@ -23,21 +21,10 @@ const ( ) var ( - ErrBodylessRequest = errors.New("not a POST|PUT|PATCH request") - formUnmarshaler = mapping.NewUnmarshaler(formKey, mapping.WithStringValues()) pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues()) ) -// Returns the peer address, supports X-Forward-For -func GetRemoteAddr(r *http.Request) string { - v := r.Header.Get(xForwardFor) - if len(v) > 0 { - return v - } - return r.RemoteAddr -} - func Parse(r *http.Request, v interface{}) error { if err := ParsePath(r, v); err != nil { return err @@ -110,7 +97,7 @@ func ParseJsonBody(r *http.Request, v interface{}) error { // Parses the symbols reside in url path. // Like http://localhost/bag/:name func ParsePath(r *http.Request, v interface{}) error { - vars := httprouter.Vars(r) + vars := context.Vars(r) m := make(map[string]interface{}, len(vars)) for k, v := range vars { m[k] = v diff --git a/core/httpx/requests_test.go b/ngin/httpx/requests_test.go similarity index 94% rename from core/httpx/requests_test.go rename to ngin/httpx/requests_test.go index 379f5508..756b0b10 100644 --- a/core/httpx/requests_test.go +++ b/ngin/httpx/requests_test.go @@ -10,7 +10,7 @@ import ( "strings" "testing" - "zero/core/httprouter" + "zero/ngin/internal/router" "github.com/stretchr/testify/assert" ) @@ -20,15 +20,6 @@ const ( contentLength = "Content-Length" ) -func TestGetRemoteAddr(t *testing.T) { - host := "8.8.8.8" - r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader("")) - assert.Nil(t, err) - - r.Header.Set(xForwardFor, host) - assert.Equal(t, host, GetRemoteAddr(r)) -} - func TestParseForm(t *testing.T) { var v struct { Name string `form:"name"` @@ -135,8 +126,8 @@ func TestParseSlice(t *testing.T) { assert.Nil(t, err) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") - router := httprouter.NewPatRouter() - err = router.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rt := router.NewPatRouter() + err = rt.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v := struct { Names []string `form:"names"` }{} @@ -150,7 +141,7 @@ func TestParseSlice(t *testing.T) { assert.Nil(t, err) rr := httptest.NewRecorder() - router.ServeHTTP(rr, r) + rt.ServeHTTP(rr, r) } func TestParseJsonPost(t *testing.T) { @@ -159,7 +150,7 @@ func TestParseJsonPost(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func( w http.ResponseWriter, r *http.Request) { v := struct { @@ -191,7 +182,7 @@ func TestParseJsonPostWithIntSlice(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func( w http.ResponseWriter, r *http.Request) { v := struct { @@ -219,7 +210,7 @@ func TestParseJsonPostError(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -247,7 +238,7 @@ func TestParseJsonPostInvalidRequest(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -269,7 +260,7 @@ func TestParseJsonPostRequired(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -292,7 +283,7 @@ func TestParsePath(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -317,7 +308,7 @@ func TestParsePathRequired(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -338,7 +329,7 @@ func TestParseQuery(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -363,7 +354,7 @@ func TestParseQueryRequired(t *testing.T) { r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v := struct { Nickname string `form:"nickname"` @@ -383,7 +374,7 @@ func TestParseOptional(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -424,7 +415,7 @@ func TestParseNestedInRequestEmpty(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -463,7 +454,7 @@ func TestParsePtrInRequest(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -494,7 +485,7 @@ func TestParsePtrInRequestEmpty(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/kevin", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -511,7 +502,7 @@ func TestParseQueryOptional(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -536,7 +527,7 @@ func TestParse(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -574,7 +565,7 @@ func TestParseWrappedRequest(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -606,7 +597,7 @@ func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -639,7 +630,7 @@ func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodHead, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -671,7 +662,7 @@ func TestParseWrappedRequestPtr(t *testing.T) { } ) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var v WrappedRequest @@ -694,7 +685,7 @@ func TestParseWithAll(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, ApplicationJson) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v := struct { Name string `path:"name"` @@ -725,7 +716,7 @@ func TestParseWithAllUtf8(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, applicationJsonWithUtf8) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -756,7 +747,7 @@ func TestParseWithMissingForm(t *testing.T) { bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`)) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -783,7 +774,7 @@ func TestParseWithMissingAllForms(t *testing.T) { bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`)) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -809,7 +800,7 @@ func TestParseWithMissingJson(t *testing.T) { bytes.NewBufferString(`{"location": "shanghai"}`)) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -835,7 +826,7 @@ func TestParseWithMissingAllJsons(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -862,7 +853,7 @@ func TestParseWithMissingPath(t *testing.T) { bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`)) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -889,7 +880,7 @@ func TestParseWithMissingAllPaths(t *testing.T) { bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`)) assert.Nil(t, err) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -916,7 +907,7 @@ func TestParseGetWithContentLengthHeader(t *testing.T) { r.Header.Set(ContentType, ApplicationJson) r.Header.Set(contentLength, "1024") - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -943,7 +934,7 @@ func TestParseJsonPostWithTypeMismatch(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, applicationJsonWithUtf8) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { @@ -969,7 +960,7 @@ func TestParseJsonPostWithInt2String(t *testing.T) { assert.Nil(t, err) r.Header.Set(ContentType, applicationJsonWithUtf8) - router := httprouter.NewPatRouter() + router := router.NewPatRouter() err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { v := struct { diff --git a/core/httpx/responses.go b/ngin/httpx/responses.go similarity index 100% rename from core/httpx/responses.go rename to ngin/httpx/responses.go diff --git a/core/httpx/responses_test.go b/ngin/httpx/responses_test.go similarity index 100% rename from core/httpx/responses_test.go rename to ngin/httpx/responses_test.go diff --git a/ngin/internal/context/params.go b/ngin/internal/context/params.go new file mode 100644 index 00000000..b1f9ecf5 --- /dev/null +++ b/ngin/internal/context/params.go @@ -0,0 +1,21 @@ +package context + +import ( + "context" + "net/http" +) + +const pathVars = "pathVars" + +func Vars(r *http.Request) map[string]string { + vars, ok := r.Context().Value(pathVars).(map[string]string) + if ok { + return vars + } + + return nil +} + +func WithPathVars(r *http.Request, params map[string]string) *http.Request { + return r.WithContext(context.WithValue(r.Context(), pathVars, params)) +} diff --git a/core/httplog/log.go b/ngin/internal/log.go similarity index 93% rename from core/httplog/log.go rename to ngin/internal/log.go index 76ac50ed..80d308a8 100644 --- a/core/httplog/log.go +++ b/ngin/internal/log.go @@ -1,4 +1,4 @@ -package httplog +package internal import ( "bytes" @@ -6,7 +6,6 @@ import ( "net/http" "sync" - "zero/core/httpx" "zero/core/logx" ) @@ -80,5 +79,5 @@ func formatf(r *http.Request, format string, v ...interface{}) string { } func formatWithReq(r *http.Request, v string) string { - return fmt.Sprintf("(%s - %s) %s", r.RequestURI, httpx.GetRemoteAddr(r), v) + return fmt.Sprintf("(%s - %s) %s", r.RequestURI, GetRemoteAddr(r), v) } diff --git a/core/httplog/log_test.go b/ngin/internal/log_test.go similarity index 98% rename from core/httplog/log_test.go rename to ngin/internal/log_test.go index 98d1858d..82d6f296 100644 --- a/core/httplog/log_test.go +++ b/ngin/internal/log_test.go @@ -1,4 +1,4 @@ -package httplog +package internal import ( "context" diff --git a/core/httprouter/patrouter.go b/ngin/internal/router/patrouter.go similarity index 87% rename from core/httprouter/patrouter.go rename to ngin/internal/router/patrouter.go index c8728d17..007e23a9 100644 --- a/core/httprouter/patrouter.go +++ b/ngin/internal/router/patrouter.go @@ -1,18 +1,17 @@ -package httprouter +package router import ( - "context" "net/http" "path" "strings" "zero/core/search" + "zero/ngin/internal/context" ) const ( allowHeader = "Allow" allowMethodSeparator = ", " - pathVars = "pathVars" ) type PatRouter struct { @@ -50,7 +49,7 @@ func (pr *PatRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { if tree, ok := pr.trees[r.Method]; ok { if result, ok := tree.Search(reqPath); ok { if len(result.Params) > 0 { - r = r.WithContext(context.WithValue(r.Context(), pathVars, result.Params)) + r = context.WithPathVars(r, result.Params) } result.Item.(http.Handler).ServeHTTP(w, r) return @@ -98,15 +97,6 @@ func (pr *PatRouter) methodNotAllowed(method, path string) (string, bool) { } } -func Vars(r *http.Request) map[string]string { - vars, ok := r.Context().Value(pathVars).(map[string]string) - if ok { - return vars - } - - return nil -} - func validMethod(method string) bool { return method == http.MethodDelete || method == http.MethodGet || method == http.MethodHead || method == http.MethodOptions || diff --git a/core/httprouter/patrouter_test.go b/ngin/internal/router/patrouter_test.go similarity index 95% rename from core/httprouter/patrouter_test.go rename to ngin/internal/router/patrouter_test.go index cd075afa..aada9340 100644 --- a/core/httprouter/patrouter_test.go +++ b/ngin/internal/router/patrouter_test.go @@ -1,10 +1,12 @@ -package httprouter +package router import ( "net/http" "testing" "github.com/stretchr/testify/assert" + + "zero/ngin/internal/context" ) type mockedResponseWriter struct { @@ -78,12 +80,12 @@ func TestPatRouter(t *testing.T) { router := NewPatRouter() err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { routed = true - assert.Equal(t, 1, len(Vars(r))) + assert.Equal(t, 1, len(context.Vars(r))) })) assert.Nil(t, err) err = router.Handle(test.method, "/a/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { routed = true - assert.Nil(t, Vars(r)) + assert.Nil(t, context.Vars(r)) })) assert.Nil(t, err) err = router.Handle(test.method, "/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/core/httprouter/router.go b/ngin/internal/router/router.go similarity index 95% rename from core/httprouter/router.go rename to ngin/internal/router/router.go index d641c139..cbf1c3ec 100644 --- a/core/httprouter/router.go +++ b/ngin/internal/router/router.go @@ -1,4 +1,4 @@ -package httprouter +package router import ( "errors" diff --git a/core/httphandler/internal/contentsecurity.go b/ngin/internal/security/contentsecurity.go similarity index 98% rename from core/httphandler/internal/contentsecurity.go rename to ngin/internal/security/contentsecurity.go index e6f97944..d10ee336 100644 --- a/core/httphandler/internal/contentsecurity.go +++ b/ngin/internal/security/contentsecurity.go @@ -1,4 +1,4 @@ -package internal +package security import ( "crypto/sha256" @@ -13,9 +13,9 @@ import ( "time" "zero/core/codec" - "zero/core/httpx" "zero/core/iox" "zero/core/logx" + "zero/ngin/httpx" ) const ( diff --git a/core/httphandler/internal/withcoderesponsewriter.go b/ngin/internal/security/withcoderesponsewriter.go similarity index 95% rename from core/httphandler/internal/withcoderesponsewriter.go rename to ngin/internal/security/withcoderesponsewriter.go index 5631cc22..41d61fd9 100644 --- a/core/httphandler/internal/withcoderesponsewriter.go +++ b/ngin/internal/security/withcoderesponsewriter.go @@ -1,4 +1,4 @@ -package internal +package security import "net/http" diff --git a/core/httpserver/server.go b/ngin/internal/server.go similarity index 98% rename from core/httpserver/server.go rename to ngin/internal/server.go index 9bf431c3..3b72741d 100644 --- a/core/httpserver/server.go +++ b/ngin/internal/server.go @@ -1,4 +1,4 @@ -package httpserver +package internal import ( "crypto/tls" diff --git a/core/httpserver/starter.go b/ngin/internal/starter.go similarity index 91% rename from core/httpserver/starter.go rename to ngin/internal/starter.go index 2cea7362..7f0b9703 100644 --- a/core/httpserver/starter.go +++ b/ngin/internal/starter.go @@ -1,4 +1,4 @@ -package httpserver +package internal import ( "context" diff --git a/core/httpsecurity/tokenparser.go b/ngin/internal/tokenparser.go similarity index 99% rename from core/httpsecurity/tokenparser.go rename to ngin/internal/tokenparser.go index 32cb11d7..17f5f024 100644 --- a/core/httpsecurity/tokenparser.go +++ b/ngin/internal/tokenparser.go @@ -1,4 +1,4 @@ -package httpsecurity +package internal import ( "net/http" diff --git a/core/httpsecurity/tokenparser_test.go b/ngin/internal/tokenparser_test.go similarity index 99% rename from core/httpsecurity/tokenparser_test.go rename to ngin/internal/tokenparser_test.go index edeb74cb..4cb7a406 100644 --- a/core/httpsecurity/tokenparser_test.go +++ b/ngin/internal/tokenparser_test.go @@ -1,4 +1,4 @@ -package httpsecurity +package internal import ( "net/http" diff --git a/ngin/internal/util.go b/ngin/internal/util.go new file mode 100644 index 00000000..5e7c2e2b --- /dev/null +++ b/ngin/internal/util.go @@ -0,0 +1,14 @@ +package internal + +import "net/http" + +const xForwardFor = "X-Forward-For" + +// Returns the peer address, supports X-Forward-For +func GetRemoteAddr(r *http.Request) string { + v := r.Header.Get(xForwardFor) + if len(v) > 0 { + return v + } + return r.RemoteAddr +} diff --git a/ngin/internal/util_test.go b/ngin/internal/util_test.go new file mode 100644 index 00000000..96594454 --- /dev/null +++ b/ngin/internal/util_test.go @@ -0,0 +1,19 @@ +package internal + +import ( + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetRemoteAddr(t *testing.T) { + host := "8.8.8.8" + r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader("")) + assert.Nil(t, err) + + r.Header.Set(xForwardFor, host) + assert.Equal(t, host, GetRemoteAddr(r)) +} + diff --git a/ngin/ngin.go b/ngin/ngin.go index efbe5b78..33103acd 100644 --- a/ngin/ngin.go +++ b/ngin/ngin.go @@ -4,9 +4,9 @@ import ( "log" "net/http" - "zero/core/httphandler" - "zero/core/httprouter" "zero/core/logx" + "zero/ngin/handler" + "zero/ngin/internal/router" ) type ( @@ -124,7 +124,7 @@ func WithPriority() RouteOption { } } -func WithRouter(router httprouter.Router) RunOption { +func WithRouter(router router.Router) RunOption { return func(engine *Engine) { engine.opts.start = func(srv *server) error { return srv.StartWithRouter(router) @@ -141,13 +141,13 @@ func WithSignature(signature SignatureConf) RouteOption { } } -func WithUnauthorizedCallback(callback httphandler.UnauthorizedCallback) RunOption { +func WithUnauthorizedCallback(callback handler.UnauthorizedCallback) RunOption { return func(engine *Engine) { engine.srv.SetUnauthorizedCallback(callback) } } -func WithUnsignedCallback(callback httphandler.UnsignedCallback) RunOption { +func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption { return func(engine *Engine) { engine.srv.SetUnsignedCallback(callback) } diff --git a/ngin/ngin_test.go b/ngin/ngin_test.go index 3834c951..eb0cbf86 100644 --- a/ngin/ngin_test.go +++ b/ngin/ngin_test.go @@ -7,15 +7,15 @@ import ( "net/http/httptest" "testing" - "zero/core/httprouter" - "zero/core/httpx" + "zero/ngin/httpx" + router2 "zero/ngin/internal/router" "github.com/stretchr/testify/assert" ) func TestWithMiddleware(t *testing.T) { m := make(map[string]string) - router := httprouter.NewPatRouter() + router := router2.NewPatRouter() handler := func(w http.ResponseWriter, r *http.Request) { var v struct { Nickname string `form:"nickname"` diff --git a/ngin/server.go b/ngin/server.go index 6caec1d6..339971ed 100644 --- a/ngin/server.go +++ b/ngin/server.go @@ -7,11 +7,11 @@ import ( "time" "zero/core/codec" - "zero/core/httphandler" - "zero/core/httprouter" - "zero/core/httpserver" "zero/core/load" "zero/core/stat" + "zero/ngin/handler" + "zero/ngin/internal" + "zero/ngin/internal/router" "github.com/justinas/alice" ) @@ -27,8 +27,8 @@ type ( server struct { conf NgConf routes []featuredRoutes - unauthorizedCallback httphandler.UnauthorizedCallback - unsignedCallback httphandler.UnsignedCallback + unauthorizedCallback handler.UnauthorizedCallback + unsignedCallback handler.UnsignedCallback middlewares []Middleware shedder load.Shedder priorityShedder load.Shedder @@ -52,43 +52,43 @@ func (s *server) AddRoutes(r featuredRoutes) { s.routes = append(s.routes, r) } -func (s *server) SetUnauthorizedCallback(callback httphandler.UnauthorizedCallback) { +func (s *server) SetUnauthorizedCallback(callback handler.UnauthorizedCallback) { s.unauthorizedCallback = callback } -func (s *server) SetUnsignedCallback(callback httphandler.UnsignedCallback) { +func (s *server) SetUnsignedCallback(callback handler.UnsignedCallback) { s.unsignedCallback = callback } func (s *server) Start() error { - return s.StartWithRouter(httprouter.NewPatRouter()) + return s.StartWithRouter(router.NewPatRouter()) } -func (s *server) StartWithRouter(router httprouter.Router) error { +func (s *server) StartWithRouter(router router.Router) error { if err := s.bindRoutes(router); err != nil { return err } - return httpserver.StartHttp(s.conf.Host, s.conf.Port, router) + return internal.StartHttp(s.conf.Host, s.conf.Port, router) } func (s *server) appendAuthHandler(fr featuredRoutes, chain alice.Chain, verifier func(alice.Chain) alice.Chain) alice.Chain { if fr.jwt.enabled { if len(fr.jwt.prevSecret) == 0 { - chain = chain.Append(httphandler.Authorize(fr.jwt.secret, - httphandler.WithUnauthorizedCallback(s.unauthorizedCallback))) + chain = chain.Append(handler.Authorize(fr.jwt.secret, + handler.WithUnauthorizedCallback(s.unauthorizedCallback))) } else { - chain = chain.Append(httphandler.Authorize(fr.jwt.secret, - httphandler.WithPrevSecret(fr.jwt.prevSecret), - httphandler.WithUnauthorizedCallback(s.unauthorizedCallback))) + chain = chain.Append(handler.Authorize(fr.jwt.secret, + handler.WithPrevSecret(fr.jwt.prevSecret), + handler.WithUnauthorizedCallback(s.unauthorizedCallback))) } } return verifier(chain) } -func (s *server) bindFeaturedRoutes(router httprouter.Router, fr featuredRoutes, metrics *stat.Metrics) error { +func (s *server) bindFeaturedRoutes(router router.Router, fr featuredRoutes, metrics *stat.Metrics) error { verifier, err := s.signatureVerifier(fr.signature) if err != nil { return err @@ -103,20 +103,20 @@ func (s *server) bindFeaturedRoutes(router httprouter.Router, fr featuredRoutes, return nil } -func (s *server) bindRoute(fr featuredRoutes, router httprouter.Router, metrics *stat.Metrics, +func (s *server) bindRoute(fr featuredRoutes, router router.Router, metrics *stat.Metrics, route Route, verifier func(chain alice.Chain) alice.Chain) error { chain := alice.New( - httphandler.TracingHandler, + handler.TracingHandler, s.getLogHandler(), - httphandler.MaxConns(s.conf.MaxConns), - httphandler.BreakerHandler(route.Method, route.Path, metrics), - httphandler.SheddingHandler(s.getShedder(fr.priority), metrics), - httphandler.TimeoutHandler(time.Duration(s.conf.Timeout)*time.Millisecond), - httphandler.RecoverHandler, - httphandler.MetricHandler(metrics), - httphandler.PromMetricHandler(route.Path), - httphandler.MaxBytesHandler(s.conf.MaxBytes), - httphandler.GunzipHandler, + handler.MaxConns(s.conf.MaxConns), + handler.BreakerHandler(route.Method, route.Path, metrics), + handler.SheddingHandler(s.getShedder(fr.priority), metrics), + handler.TimeoutHandler(time.Duration(s.conf.Timeout)*time.Millisecond), + handler.RecoverHandler, + handler.MetricHandler(metrics), + handler.PromMetricHandler(route.Path), + handler.MaxBytesHandler(s.conf.MaxBytes), + handler.GunzipHandler, ) chain = s.appendAuthHandler(fr, chain, verifier) @@ -128,7 +128,7 @@ func (s *server) bindRoute(fr featuredRoutes, router httprouter.Router, metrics return router.Handle(route.Method, route.Path, handle) } -func (s *server) bindRoutes(router httprouter.Router) error { +func (s *server) bindRoutes(router router.Router) error { metrics := s.createMetrics() for _, fr := range s.routes { @@ -154,9 +154,9 @@ func (s *server) createMetrics() *stat.Metrics { func (s *server) getLogHandler() func(http.Handler) http.Handler { if s.conf.Verbose { - return httphandler.DetailedLogHandler + return handler.DetailedLogHandler } else { - return httphandler.LogHandler + return handler.LogHandler } } @@ -198,10 +198,10 @@ func (s *server) signatureVerifier(signature signatureSetting) (func(chain alice return func(chain alice.Chain) alice.Chain { if s.unsignedCallback != nil { - return chain.Append(httphandler.ContentSecurityHandler( + return chain.Append(handler.ContentSecurityHandler( decrypters, signature.Expiry, signature.Strict, s.unsignedCallback)) } else { - return chain.Append(httphandler.ContentSecurityHandler( + return chain.Append(handler.ContentSecurityHandler( decrypters, signature.Expiry, signature.Strict)) } }, nil