diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index 61e4a67e..238efa24 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/tal-tech/go-zero/core/mapping" - "github.com/tal-tech/go-zero/rest/internal/context" + "github.com/tal-tech/go-zero/rest/pathvar" ) const ( @@ -119,7 +119,7 @@ func ParseJsonBody(r *http.Request, v interface{}) error { // ParsePath parses the symbols reside in url path. // Like http://localhost/bag/:name func ParsePath(r *http.Request, v interface{}) error { - vars := context.Vars(r) + vars := pathvar.Vars(r) m := make(map[string]interface{}, len(vars)) for k, v := range vars { m[k] = v diff --git a/rest/internal/context/params.go b/rest/pathvar/params.go similarity index 65% rename from rest/internal/context/params.go rename to rest/pathvar/params.go index 825d3089..a224951c 100644 --- a/rest/internal/context/params.go +++ b/rest/pathvar/params.go @@ -1,4 +1,4 @@ -package context +package pathvar import ( "context" @@ -17,13 +17,13 @@ func Vars(r *http.Request) map[string]string { return nil } -// WithPathVars writes params into given r and returns a new http.Request. -func WithPathVars(r *http.Request, params map[string]string) *http.Request { +// WithVars writes params into given r and returns a new http.Request. +func WithVars(r *http.Request, params map[string]string) *http.Request { return r.WithContext(context.WithValue(r.Context(), pathVars, params)) } type contextKey string func (c contextKey) String() string { - return "rest/internal/context key: " + string(c) + return "rest/pathvar/context key: " + string(c) } diff --git a/rest/internal/context/params_test.go b/rest/pathvar/params_test.go similarity index 97% rename from rest/internal/context/params_test.go rename to rest/pathvar/params_test.go index c26ffd40..24c60a1e 100644 --- a/rest/internal/context/params_test.go +++ b/rest/pathvar/params_test.go @@ -1,4 +1,4 @@ -package context +package pathvar import ( "context" diff --git a/rest/router/patrouter.go b/rest/router/patrouter.go index 7c15b1f9..da70c789 100644 --- a/rest/router/patrouter.go +++ b/rest/router/patrouter.go @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/core/search" "github.com/tal-tech/go-zero/rest/httpx" - "github.com/tal-tech/go-zero/rest/internal/context" + "github.com/tal-tech/go-zero/rest/pathvar" ) const ( @@ -61,7 +61,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 = context.WithPathVars(r, result.Params) + r = pathvar.WithVars(r, result.Params) } result.Item.(http.Handler).ServeHTTP(w, r) return diff --git a/rest/router/patrouter_test.go b/rest/router/patrouter_test.go index 1a952b4b..fcd0b43f 100644 --- a/rest/router/patrouter_test.go +++ b/rest/router/patrouter_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/rest/httpx" - "github.com/tal-tech/go-zero/rest/internal/context" + "github.com/tal-tech/go-zero/rest/pathvar" ) const ( @@ -107,12 +107,12 @@ func TestPatRouter(t *testing.T) { router := NewRouter() err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { routed = true - assert.Equal(t, 1, len(context.Vars(r))) + assert.Equal(t, 1, len(pathvar.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, context.Vars(r)) + assert.Nil(t, pathvar.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/rest/server.go b/rest/server.go index 7370e50f..fa366aab 100644 --- a/rest/server.go +++ b/rest/server.go @@ -29,12 +29,12 @@ type ( // Be aware that later RunOption might overwrite previous one that write the same option. // The process will exit if error occurs. func MustNewServer(c RestConf, opts ...RunOption) *Server { - engine, err := NewServer(c, opts...) + server, err := NewServer(c, opts...) if err != nil { log.Fatal(err) } - return engine + return server } // NewServer returns a server with given config of c and options defined in opts. @@ -61,36 +61,36 @@ func NewServer(c RestConf, opts ...RunOption) (*Server, error) { } // AddRoutes add given routes into the Server. -func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) { +func (s *Server) AddRoutes(rs []Route, opts ...RouteOption) { r := featuredRoutes{ routes: rs, } for _, opt := range opts { opt(&r) } - e.ngin.AddRoutes(r) + s.ngin.AddRoutes(r) } // AddRoute adds given route into the Server. -func (e *Server) AddRoute(r Route, opts ...RouteOption) { - e.AddRoutes([]Route{r}, opts...) +func (s *Server) AddRoute(r Route, opts ...RouteOption) { + s.AddRoutes([]Route{r}, opts...) } // Start starts the Server. // Graceful shutdown is enabled by default. // Use proc.SetTimeToForceQuit to customize the graceful shutdown period. -func (e *Server) Start() { - handleError(e.opts.start(e.ngin)) +func (s *Server) Start() { + handleError(s.opts.start(s.ngin)) } // Stop stops the Server. -func (e *Server) Stop() { +func (s *Server) Stop() { logx.Close() } // Use adds the given middleware in the Server. -func (e *Server) Use(middleware Middleware) { - e.ngin.use(middleware) +func (s *Server) Use(middleware Middleware) { + s.ngin.use(middleware) } // ToMiddleware converts the given handler to a Middleware.