From 82d04d612ba178dc980a6d0dba649ed99c567a3d Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 6 Oct 2021 14:22:58 +0800 Subject: [PATCH] fix openapi specification generating; improve handler type --- net/ghttp/ghttp.go | 38 +++++++++++---------- net/ghttp/ghttp_request_middleware.go | 8 ++--- net/ghttp/ghttp_server.go | 12 +++---- net/ghttp/ghttp_server_openapi.go | 4 +++ net/ghttp/ghttp_server_router.go | 10 +++--- net/ghttp/ghttp_server_router_hook.go | 2 +- net/ghttp/ghttp_server_router_middleware.go | 4 +-- net/ghttp/ghttp_server_router_serve.go | 14 ++++---- net/ghttp/ghttp_server_service_handler.go | 2 +- net/ghttp/ghttp_server_service_object.go | 8 ++--- 10 files changed, 54 insertions(+), 48 deletions(-) diff --git a/net/ghttp/ghttp.go b/net/ghttp/ghttp.go index 3d13d70e5..04c092bff 100644 --- a/net/ghttp/ghttp.go +++ b/net/ghttp/ghttp.go @@ -52,7 +52,7 @@ type ( Server string // Server name. Address string // Listening address. Domain string // Bound domain. - Type int // Router type. + Type string // Router type. Middleware string // Bound middleware. Method string // Handler method name. Route string // Route URI. @@ -75,7 +75,7 @@ type ( handlerItem struct { Id int // Unique handler item id mark. Name string // Handler name, which is automatically retrieved from runtime stack when registered. - Type int // Handler type: object/handler/controller/middleware/hook. + Type string // Handler type: object/handler/middleware/hook. Info handlerFuncInfo // Handler function information. InitFunc HandlerFunc // Initialization function when request enters the object (only available for object register type). ShutFunc HandlerFunc // Shutdown function when request leaves out the object (only available for object register type). @@ -111,22 +111,24 @@ const ( ServerStatusRunning = 1 DefaultServerName = "default" DefaultDomainName = "default" - supportedHttpMethods = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE" - defaultMethod = "ALL" - handlerTypeHandler = 1 - handlerTypeObject = 2 - handlerTypeController = 3 - handlerTypeMiddleware = 4 - handlerTypeHook = 5 - exceptionExit = "exit" - exceptionExitAll = "exit_all" - exceptionExitHook = "exit_hook" - routeCacheDuration = time.Hour - methodNameInit = "Init" - methodNameShut = "Shut" - methodNameExit = "Exit" - ctxKeyForRequest = "gHttpRequestObject" - swaggerUIPackedPath = "/goframe/swaggerui" + HandlerTypeHandler = "handler" + HandlerTypeObject = "object" + HandlerTypeMiddleware = "middleware" + HandlerTypeHook = "hook" +) + +const ( + supportedHttpMethods = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE" + defaultMethod = "ALL" + exceptionExit = "exit" + exceptionExitAll = "exit_all" + exceptionExitHook = "exit_hook" + routeCacheDuration = time.Hour + methodNameInit = "Init" + methodNameShut = "Shut" + methodNameExit = "Exit" + ctxKeyForRequest = "gHttpRequestObject" + swaggerUIPackedPath = "/goframe/swaggerui" ) var ( diff --git a/net/ghttp/ghttp_request_middleware.go b/net/ghttp/ghttp_request_middleware.go index e1ad211a0..f5f8e42be 100644 --- a/net/ghttp/ghttp_request_middleware.go +++ b/net/ghttp/ghttp_request_middleware.go @@ -35,7 +35,7 @@ func (m *middleware) Next() { } item = m.request.handlers[m.handlerIndex] // Filter the HOOK handlers, which are designed to be called in another standalone procedure. - if item.Handler.Type == handlerTypeHook { + if item.Handler.Type == HandlerTypeHook { m.handlerIndex++ continue } @@ -60,7 +60,7 @@ func (m *middleware) Next() { switch item.Handler.Type { // Service object. - case handlerTypeObject: + case HandlerTypeObject: m.served = true if m.request.IsExited() { break @@ -80,7 +80,7 @@ func (m *middleware) Next() { } // Service handler. - case handlerTypeHandler: + case HandlerTypeHandler: m.served = true if m.request.IsExited() { break @@ -90,7 +90,7 @@ func (m *middleware) Next() { }) // Global middleware array. - case handlerTypeMiddleware: + case HandlerTypeMiddleware: niceCallFunc(func() { item.Handler.Info.Func(m.request) }) diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index d3e846787..a684909f5 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -326,10 +326,10 @@ func (s *Server) GetRoutes() []RouterItem { Handler: registeredItem.Handler, } switch item.Handler.Type { - case handlerTypeController, handlerTypeObject, handlerTypeHandler: + case HandlerTypeObject, HandlerTypeHandler: item.IsServiceHandler = true - case handlerTypeMiddleware: + case HandlerTypeMiddleware: item.Middleware = "GLOBAL MIDDLEWARE" } if len(item.Handler.Middleware) > 0 { @@ -351,9 +351,9 @@ func (s *Server) GetRoutes() []RouterItem { if r = strings.Compare(item1.Domain, item2.Domain); r == 0 { if r = strings.Compare(item1.Route, item2.Route); r == 0 { if r = strings.Compare(item1.Method, item2.Method); r == 0 { - if item1.Handler.Type == handlerTypeMiddleware && item2.Handler.Type != handlerTypeMiddleware { + if item1.Handler.Type == HandlerTypeMiddleware && item2.Handler.Type != HandlerTypeMiddleware { return -1 - } else if item1.Handler.Type == handlerTypeMiddleware && item2.Handler.Type == handlerTypeMiddleware { + } else if item1.Handler.Type == HandlerTypeMiddleware && item2.Handler.Type == HandlerTypeMiddleware { return 1 } else if r = strings.Compare(item1.Middleware, item2.Middleware); r == 0 { r = item2.Priority - item1.Priority @@ -390,9 +390,9 @@ func (s *Server) Run() { // Remove plugins. if len(s.plugins) > 0 { for _, p := range s.plugins { - intlog.Printf(context.TODO(), `remove plugin: %s`, p.Name()) + intlog.Printf(ctx, `remove plugin: %s`, p.Name()) if err := p.Remove(); err != nil { - intlog.Errorf(context.TODO(), "%+v", err) + intlog.Errorf(ctx, "%+v", err) } } } diff --git a/net/ghttp/ghttp_server_openapi.go b/net/ghttp/ghttp_server_openapi.go index 15f37a3e6..b6aa796f5 100644 --- a/net/ghttp/ghttp_server_openapi.go +++ b/net/ghttp/ghttp_server_openapi.go @@ -22,6 +22,10 @@ func (s *Server) initOpenApi() { method string ) for _, item := range s.GetRoutes() { + switch item.Type { + case HandlerTypeMiddleware, HandlerTypeHook: + continue + } method = item.Method if gstr.Equal(method, defaultMethod) { method = "POST" diff --git a/net/ghttp/ghttp_server_router.go b/net/ghttp/ghttp_server_router.go index e8c39f6b4..9869703e6 100644 --- a/net/ghttp/ghttp_server_router.go +++ b/net/ghttp/ghttp_server_router.go @@ -87,7 +87,7 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle routerKey := s.routerMapKey(handler.HookName, method, uri, domain) if !s.config.RouteOverWrite { switch handler.Type { - case handlerTypeHandler, handlerTypeObject, handlerTypeController: + case HandlerTypeHandler, HandlerTypeObject: if item, ok := s.routesMap[routerKey]; ok { s.Logger().Fatalf( ctx, @@ -200,7 +200,7 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle Handler: handler, } switch handler.Type { - case handlerTypeHandler, handlerTypeObject, handlerTypeController: + case HandlerTypeHandler, HandlerTypeObject: // Overwrite the route. s.routesMap[routerKey] = []registeredRouteItem{routeItem} default: @@ -219,11 +219,11 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle // 3. Route type: {xxx} > :xxx > *xxx. func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerItem) bool { // If they're all type of middleware, the priority is according their registered sequence. - if newItem.Type == handlerTypeMiddleware && oldItem.Type == handlerTypeMiddleware { + if newItem.Type == HandlerTypeMiddleware && oldItem.Type == HandlerTypeMiddleware { return false } // The middleware has the most high priority. - if newItem.Type == handlerTypeMiddleware && oldItem.Type != handlerTypeMiddleware { + if newItem.Type == HandlerTypeMiddleware && oldItem.Type != HandlerTypeMiddleware { return true } // URI: The deeper the higher (simply check the count of char '/' in the URI). @@ -324,7 +324,7 @@ func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerIte // If they have different router type, // the new router item has more priority than the other one. - if newItem.Type == handlerTypeHandler || newItem.Type == handlerTypeObject || newItem.Type == handlerTypeController { + if newItem.Type == HandlerTypeHandler || newItem.Type == HandlerTypeObject { return true } diff --git a/net/ghttp/ghttp_server_router_hook.go b/net/ghttp/ghttp_server_router_hook.go index 892c62947..bb9226d45 100644 --- a/net/ghttp/ghttp_server_router_hook.go +++ b/net/ghttp/ghttp_server_router_hook.go @@ -20,7 +20,7 @@ func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFun func (s *Server) doBindHookHandler(ctx context.Context, pattern string, hook string, handler HandlerFunc, source string) { s.setHandler(ctx, pattern, &handlerItem{ - Type: handlerTypeHook, + Type: HandlerTypeHook, Name: gdebug.FuncPath(handler), Info: handlerFuncInfo{ Func: handler, diff --git a/net/ghttp/ghttp_server_router_middleware.go b/net/ghttp/ghttp_server_router_middleware.go index 1c0ba1ae4..973d37bb0 100644 --- a/net/ghttp/ghttp_server_router_middleware.go +++ b/net/ghttp/ghttp_server_router_middleware.go @@ -27,7 +27,7 @@ func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) { ) for _, handler := range handlers { s.setHandler(ctx, pattern, &handlerItem{ - Type: handlerTypeMiddleware, + Type: HandlerTypeMiddleware, Name: gdebug.FuncPath(handler), Info: handlerFuncInfo{ Func: handler, @@ -46,7 +46,7 @@ func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) { ) for _, handler := range handlers { s.setHandler(ctx, defaultMiddlewarePattern, &handlerItem{ - Type: handlerTypeMiddleware, + Type: HandlerTypeMiddleware, Name: gdebug.FuncPath(handler), Info: handlerFuncInfo{ Func: handler, diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 9ca95702d..25e88b4db 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -159,7 +159,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han // Serving handler can only be added to the handler array just once. if hasServe { switch item.Type { - case handlerTypeHandler, handlerTypeObject, handlerTypeController: + case HandlerTypeHandler, HandlerTypeObject: continue } } @@ -180,14 +180,14 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han } switch item.Type { // The serving handler can be only added just once. - case handlerTypeHandler, handlerTypeObject, handlerTypeController: + case HandlerTypeHandler, HandlerTypeObject: hasServe = true parsedItemList.PushBack(parsedItem) // The middleware is inserted before the serving handler. // If there are multiple middleware, they're inserted into the result list by their registering order. // The middleware is also executed by their registered order. - case handlerTypeMiddleware: + case HandlerTypeMiddleware: if lastMiddlewareElem == nil { lastMiddlewareElem = parsedItemList.PushFront(parsedItem) } else { @@ -195,12 +195,12 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han } // HOOK handler, just push it back to the list. - case handlerTypeHook: + case HandlerTypeHook: hasHook = true parsedItemList.PushBack(parsedItem) default: - panic(gerror.NewCodef(gcode.CodeInternalError, `invalid handler type %d`, item.Type)) + panic(gerror.NewCodef(gcode.CodeInternalError, `invalid handler type %s`, item.Type)) } } } @@ -221,7 +221,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (item *handlerItem) MarshalJSON() ([]byte, error) { switch item.Type { - case handlerTypeHook: + case HandlerTypeHook: return json.Marshal( fmt.Sprintf( `%s %s:%s (%s)`, @@ -231,7 +231,7 @@ func (item *handlerItem) MarshalJSON() ([]byte, error) { item.HookName, ), ) - case handlerTypeMiddleware: + case HandlerTypeMiddleware: return json.Marshal( fmt.Sprintf( `%s %s:%s (MIDDLEWARE)`, diff --git a/net/ghttp/ghttp_server_service_handler.go b/net/ghttp/ghttp_server_service_handler.go index c307c59dd..0bd489cdf 100644 --- a/net/ghttp/ghttp_server_service_handler.go +++ b/net/ghttp/ghttp_server_service_handler.go @@ -39,7 +39,7 @@ func (s *Server) BindHandler(pattern string, handler interface{}) { func (s *Server) doBindHandler(ctx context.Context, pattern string, funcInfo handlerFuncInfo, middleware []HandlerFunc, source string) { s.setHandler(ctx, pattern, &handlerItem{ Name: gdebug.FuncPath(funcInfo.Func), - Type: handlerTypeHandler, + Type: HandlerTypeHandler, Info: funcInfo, Middleware: middleware, Source: source, diff --git a/net/ghttp/ghttp_server_service_object.go b/net/ghttp/ghttp_server_service_object.go index 606774894..b3a502ac0 100644 --- a/net/ghttp/ghttp_server_service_object.go +++ b/net/ghttp/ghttp_server_service_object.go @@ -115,7 +115,7 @@ func (s *Server) doBindObject(ctx context.Context, pattern string, object interf key := s.mergeBuildInNameToPattern(pattern, structName, methodName, true) m[key] = &handlerItem{ Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName), - Type: handlerTypeObject, + Type: HandlerTypeObject, Info: funcInfo, InitFunc: initFunc, ShutFunc: shutFunc, @@ -137,7 +137,7 @@ func (s *Server) doBindObject(ctx context.Context, pattern string, object interf } m[k] = &handlerItem{ Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName), - Type: handlerTypeObject, + Type: HandlerTypeObject, Info: funcInfo, InitFunc: initFunc, ShutFunc: shutFunc, @@ -197,7 +197,7 @@ func (s *Server) doBindObjectMethod(ctx context.Context, pattern string, object key := s.mergeBuildInNameToPattern(pattern, structName, methodName, false) m[key] = &handlerItem{ Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName), - Type: handlerTypeObject, + Type: HandlerTypeObject, Info: funcInfo, InitFunc: initFunc, ShutFunc: shutFunc, @@ -251,7 +251,7 @@ func (s *Server) doBindObjectRest(ctx context.Context, pattern string, object in key := s.mergeBuildInNameToPattern(methodName+":"+pattern, structName, methodName, false) m[key] = &handlerItem{ Name: fmt.Sprintf(`%s.%s.%s`, pkgPath, objName, methodName), - Type: handlerTypeObject, + Type: HandlerTypeObject, Info: funcInfo, InitFunc: initFunc, ShutFunc: shutFunc,