Explorar o código

export pathvar for user-defined routers (#911)

* refactor

* export pathvar for user-defined routers
Kevin Wan %!s(int64=3) %!d(string=hai) anos
pai
achega
fc04ad7854

+ 2 - 2
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

+ 4 - 4
rest/internal/context/params.go → 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)
 }

+ 1 - 1
rest/internal/context/params_test.go → rest/pathvar/params_test.go

@@ -1,4 +1,4 @@
-package context
+package pathvar
 
 import (
 	"context"

+ 2 - 2
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

+ 3 - 3
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) {

+ 11 - 11
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.