cauto 1 năm trước cách đây
mục cha
commit
766c294a60

+ 2 - 3
internal/cmd/cmd.go

@@ -7,7 +7,7 @@ import (
 	"github.com/gogf/gf/v2/net/goai"
 	"github.com/gogf/gf/v2/os/gcmd"
 	"go-gpt/internal/consts"
-	"go-gpt/internal/controller"
+	"go-gpt/router"
 )
 
 var (
@@ -19,8 +19,7 @@ var (
 			s := g.Server()
 
 			s.Group("/", func(group *ghttp.RouterGroup) {
-				group.Bind(controller.Hello)
-				//router.BindController(group)
+				router.BindController(group)
 			})
 
 			enhanceOpenAPIDoc(s)

+ 2 - 2
internal/controller/hello.go

@@ -21,8 +21,6 @@ type cHello struct{}
 
 func (c *cHello) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error) {
 	re := g.RequestFromCtx(ctx)
-	//g.RequestFromCtx(ctx).Response.Add("Content-Type", "application/json")
-	//g.RequestFromCtx(ctx).Header.Add("Transfer-Encoding", "chunked")
 	re.Response.Header().Set("Transfer-Encoding", "chunked")
 	re.Response.Header().Set("Content-Type", "application/json")
 	w := re.Response.Writer
@@ -53,6 +51,8 @@ func (c *cHello) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes,
 			}
 			re.Response.WriteHeader(200)
 			w.Flush()
+		} else {
+			re.Exit()
 		}
 	})
 

+ 226 - 0
resource/log/server/2023-03-30.log

@@ -343,3 +343,229 @@ Stack:
 ----------|--------|------------|-----------------------------------------------------------------|--------------------
 
 2023-03-30 16:50:41.718 [INFO] pid[27169]: all servers shutdown
+2023-03-30 17:21:26.567 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:21:26.567 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+2023-03-30 17:21:26.567 [INFO] pid[29373]: http server started listening on [:8080]
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:24:26.678 [INFO] pid[29373]: all servers shutdown
+2023-03-30 17:24:28.794 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:24:28.794 [INFO] pid[29577]: http server started listening on [:8080]
+2023-03-30 17:24:28.794 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:25:01.574 [INFO] pid[29577]: all servers shutdown
+2023-03-30 17:25:03.581 [INFO] pid[29645]: http server started listening on [:8080]
+2023-03-30 17:25:03.581 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:25:03.581 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:26:51.323 [INFO] pid[29645]: all servers shutdown
+2023-03-30 17:26:53.487 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:26:53.487 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+2023-03-30 17:26:53.487 [INFO] pid[29796]: http server started listening on [:8080]
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+          |        |            |                                                                 | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.MiddlewareCORS                     
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:28:32.405 [INFO] pid[29796]: all servers shutdown
+2023-03-30 17:28:34.439 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:28:34.439 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+2023-03-30 17:28:34.439 [INFO] pid[29933]: http server started listening on [:8080]
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+          |        |            |                                                                 | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.MiddlewareCORS                     
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:30:37.790 [INFO] pid[29933]: all servers shutdown
+2023-03-30 17:30:39.891 [FATA] invalid handler: go-gpt/internal/controller.(*cHello).Hello defined as "func(context.Context, *v1.HelloReq) error", but "func(*ghttp.Request)" or "func(context.Context, *BizRequest)(*BizResponse, error)" is required
+1. invalid handler: go-gpt/internal/controller.(*cHello).Hello defined as "func(context.Context, *v1.HelloReq) error", but "func(*ghttp.Request)" or "func(context.Context, *BizRequest)(*BizResponse, error)" is required
+   1).  github.com/gogf/gf/v2/net/ghttp.(*Server).checkAndCreateFuncInfo
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_handler.go:152
+   2).  github.com/gogf/gf/v2/net/ghttp.(*Server).doBindObject
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_object.go:132
+   3).  github.com/gogf/gf/v2/net/ghttp.(*RouterGroup).doBindRoutersToServer
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:391
+   4).  github.com/gogf/gf/v2/net/ghttp.(*Server).handlePreBindItems
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:71
+   5).  github.com/gogf/gf/v2/net/ghttp.(*Server).Start
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:140
+   6).  github.com/gogf/gf/v2/net/ghttp.(*Server).Run
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:424
+   7).  go-gpt/internal/cmd.glob..func1
+        /Users/cauto/Desktop/work/go_work/go-gpt/internal/cmd/cmd.go:32
+   8).  github.com/gogf/gf/v2/os/gcmd.(*Command).doRun
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:149
+   9).  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValueError
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:82
+   10). github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValue
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:41
+   11). github.com/gogf/gf/v2/os/gcmd.(*Command).Run
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:35
+   12). main.main
+        /Users/cauto/Desktop/work/go_work/go-gpt/main.go:12
+
+Stack:
+1.  github.com/gogf/gf/v2/net/ghttp.(*Server).doBindObject
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_object.go:134
+2.  github.com/gogf/gf/v2/net/ghttp.(*RouterGroup).doBindRoutersToServer
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:391
+3.  github.com/gogf/gf/v2/net/ghttp.(*Server).handlePreBindItems
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:71
+4.  github.com/gogf/gf/v2/net/ghttp.(*Server).Start
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:140
+5.  github.com/gogf/gf/v2/net/ghttp.(*Server).Run
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:424
+6.  go-gpt/internal/cmd.glob..func1
+    /Users/cauto/Desktop/work/go_work/go-gpt/internal/cmd/cmd.go:32
+7.  github.com/gogf/gf/v2/os/gcmd.(*Command).doRun
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:149
+8.  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValueError
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:82
+9.  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValue
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:41
+10. github.com/gogf/gf/v2/os/gcmd.(*Command).Run
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:35
+11. main.main
+    /Users/cauto/Desktop/work/go_work/go-gpt/main.go:12
+
+2023-03-30 17:31:01.076 [FATA] invalid handler: go-gpt/internal/controller.(*cHello).Hello defined as "func(context.Context) error", but "func(*ghttp.Request)" or "func(context.Context, *BizRequest)(*BizResponse, error)" is required
+1. invalid handler: go-gpt/internal/controller.(*cHello).Hello defined as "func(context.Context) error", but "func(*ghttp.Request)" or "func(context.Context, *BizRequest)(*BizResponse, error)" is required
+   1).  github.com/gogf/gf/v2/net/ghttp.(*Server).checkAndCreateFuncInfo
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_handler.go:152
+   2).  github.com/gogf/gf/v2/net/ghttp.(*Server).doBindObject
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_object.go:132
+   3).  github.com/gogf/gf/v2/net/ghttp.(*RouterGroup).doBindRoutersToServer
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:391
+   4).  github.com/gogf/gf/v2/net/ghttp.(*Server).handlePreBindItems
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:71
+   5).  github.com/gogf/gf/v2/net/ghttp.(*Server).Start
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:140
+   6).  github.com/gogf/gf/v2/net/ghttp.(*Server).Run
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:424
+   7).  go-gpt/internal/cmd.glob..func1
+        /Users/cauto/Desktop/work/go_work/go-gpt/internal/cmd/cmd.go:32
+   8).  github.com/gogf/gf/v2/os/gcmd.(*Command).doRun
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:149
+   9).  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValueError
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:82
+   10). github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValue
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:41
+   11). github.com/gogf/gf/v2/os/gcmd.(*Command).Run
+        /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:35
+   12). main.main
+        /Users/cauto/Desktop/work/go_work/go-gpt/main.go:12
+
+Stack:
+1.  github.com/gogf/gf/v2/net/ghttp.(*Server).doBindObject
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_service_object.go:134
+2.  github.com/gogf/gf/v2/net/ghttp.(*RouterGroup).doBindRoutersToServer
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:391
+3.  github.com/gogf/gf/v2/net/ghttp.(*Server).handlePreBindItems
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server_router_group.go:71
+4.  github.com/gogf/gf/v2/net/ghttp.(*Server).Start
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:140
+5.  github.com/gogf/gf/v2/net/ghttp.(*Server).Run
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/net/ghttp/ghttp_server.go:424
+6.  go-gpt/internal/cmd.glob..func1
+    /Users/cauto/Desktop/work/go_work/go-gpt/internal/cmd/cmd.go:32
+7.  github.com/gogf/gf/v2/os/gcmd.(*Command).doRun
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:149
+8.  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValueError
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:82
+9.  github.com/gogf/gf/v2/os/gcmd.(*Command).RunWithValue
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:41
+10. github.com/gogf/gf/v2/os/gcmd.(*Command).Run
+    /Users/cauto/Desktop/work/gopath/pkg/mod/github.com/gogf/gf/v2@v2.3.1/os/gcmd/gcmd_command_run.go:35
+11. main.main
+    /Users/cauto/Desktop/work/go_work/go-gpt/main.go:12
+
+2023-03-30 17:33:10.576 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:33:10.576 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+2023-03-30 17:33:10.576 [INFO] pid[30308]: http server started listening on [:8080]
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |                MIDDLEWARE                 
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                           
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.(*RateLimitMiddleware).Middleware  
+          |        |            |                                                                 | ghttp.MiddlewareHandlerResponse           
+          |        |            |                                                                 | router.MiddlewareCORS                     
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                         
+----------|--------|------------|-----------------------------------------------------------------|-------------------------------------------
+
+2023-03-30 17:50:11.038 [INFO] pid[30308]: all servers shutdown
+2023-03-30 17:50:13.509 [INFO] swagger ui is serving at address: http://127.0.0.1:8080/swagger/
+2023-03-30 17:50:13.509 [INFO] pid[31336]: http server started listening on [:8080]
+2023-03-30 17:50:13.509 [INFO] openapi specification is serving at address: http://127.0.0.1:8080/api.json
+
+  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |    MIDDLEWARE      
+----------|--------|------------|-----------------------------------------------------------------|--------------------
+  :8080   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE  
+----------|--------|------------|-----------------------------------------------------------------|--------------------
+  :8080   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                    
+----------|--------|------------|-----------------------------------------------------------------|--------------------
+  :8080   | POST   | /hello     | go-gpt/internal/controller.(*cHello).Hello                      |                    
+----------|--------|------------|-----------------------------------------------------------------|--------------------
+  :8080   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE  
+----------|--------|------------|-----------------------------------------------------------------|--------------------
+
+2023-03-30 17:51:20.779 [INFO] pid[31336]: all servers shutdown

+ 13 - 0
resource/log/server/access-20230330.log

@@ -19,3 +19,16 @@
 2023-03-30 16:40:05.949 {607bbe6cb62551171c3cfe546d3ef167} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.524, 127.0.0.1, "", "PostmanRuntime/7.31.1"
 2023-03-30 16:46:05.223 {f80830040a2651178e69df382b08f576} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.776, 127.0.0.1, "", "PostmanRuntime/7.31.1"
 2023-03-30 16:47:37.699 {789e47991f2651178f69df38da10bbfe} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.556, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:21:36.339 {e8c70f29fa2751174034f6622079d442} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.970, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:21:46.273 {68f538a5fc2751174134f662424c0730} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.231, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:22:15.210 {30b76c72032851174234f6629a933bec} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 3.955, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:22:59.791 {c82f63d6062851174334f662266eaa62} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 33.974, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:24:39.737 {b08c383f24285117b971bd0c9140d98c} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 7.607, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:25:10.801 {e0f3392b2c2851172797fe4d26703097} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.647, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:26:54.611 {a8c5b96b45285117704edd56e5200bef} 200 "GET http 127.0.0.1:8080 /swagger HTTP/1.1" 0.000, 127.0.0.1, "", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54"
+2023-03-30 17:26:54.720 {584e2f7245285117714edd569febc3ac} 200 "GET http 127.0.0.1:8080 /api.json HTTP/1.1" 0.001, 127.0.0.1, "http://127.0.0.1:8080/swagger/", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54"
+2023-03-30 17:28:42.283 {9834b26a5d2851178cc9544f81f334ed} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.611, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:33:12.108 {408e49509d28511722926506de8efa33} 200 "GET http 127.0.0.1:8080 /swagger HTTP/1.1" 0.001, 127.0.0.1, "", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54"
+2023-03-30 17:33:12.213 {d8f985569d28511723926506a51c616d} 200 "GET http 127.0.0.1:8080 /api.json HTTP/1.1" 0.001, 127.0.0.1, "http://127.0.0.1:8080/swagger/", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54"
+2023-03-30 17:33:30.277 {c09c256fa0285117249265068e449d0f} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.767, 127.0.0.1, "", "PostmanRuntime/7.31.1"
+2023-03-30 17:50:22.972 {b0da9a488c2951174b698f03e42f3f40} 200 "POST http 127.0.0.1:8080 /hello HTTP/1.1" 4.497, 127.0.0.1, "", "PostmanRuntime/7.31.1"

+ 92 - 3
router/router.go

@@ -1,10 +1,98 @@
 package router
 
 import (
+	"github.com/gogf/gf/v2/errors/gcode"
+	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/net/ghttp"
+	"github.com/gogf/gf/v2/os/glog"
 	"go-gpt/internal/controller"
+	"net/http"
+	"sync"
+	"time"
 )
 
+type DefaultHandlerResponse struct {
+	Code    int         `json:"code"`
+	Message string      `json:"message"`
+	Data    interface{} `json:"data"`
+}
+
+// MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
+func MiddlewareHandlerResponse(r *ghttp.Request) {
+	r.Middleware.Next()
+	// There's custom buffer content, it then exits current handler.
+	if r.Response.BufferLength() > 0 {
+		return
+	}
+
+	var (
+		msg  string
+		err  = r.GetError()
+		res  = r.GetHandlerResponse()
+		code = gerror.Code(err)
+	)
+	if err != nil {
+		if code == gcode.CodeNil {
+			code = gcode.CodeInternalError
+		}
+		msg = err.Error()
+	} else {
+		if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
+			msg = http.StatusText(r.Response.Status)
+			switch r.Response.Status {
+			case http.StatusNotFound:
+				code = gcode.CodeNotFound
+			case http.StatusForbidden:
+				code = gcode.CodeNotAuthorized
+			default:
+				code = gcode.CodeUnknown
+			}
+			// It creates error as it can be retrieved by other middlewares.
+			err = gerror.NewCode(code, msg)
+			r.SetError(err)
+		} else {
+			code = gcode.CodeOK
+		}
+	}
+
+	r.Response.WriteJson(DefaultHandlerResponse{
+		Code:    code.Code(),
+		Message: msg,
+		Data:    res,
+	})
+}
+
+type RateLimitMiddleware struct {
+	sync.Mutex
+	Counter map[string]int64
+}
+
+func NewRateLimitMiddleware() *RateLimitMiddleware {
+	return &RateLimitMiddleware{Counter: make(map[string]int64)}
+}
+func (m *RateLimitMiddleware) Middleware(r *ghttp.Request) {
+	ip := r.GetClientIp()
+	m.Lock()
+	defer m.Unlock()
+
+	now := time.Now().Unix()
+	count, ok := m.Counter[ip]
+	if !ok {
+		count = 1
+		m.Counter[ip] = now
+	} else {
+		if now-m.Counter[ip] < 1 {
+			r.Response.WriteStatusExit(429, "Too Many Requests")
+		} else {
+			m.Counter[ip] = now
+			count++
+		}
+	}
+
+	glog.Debugf(r.Context(), "%s:%d", ip, count)
+
+	r.Middleware.Next()
+}
 func MiddlewareCORS(r *ghttp.Request) {
 
 	corsOptions := r.Response.DefaultCORSOptions()
@@ -16,10 +104,11 @@ func MiddlewareCORS(r *ghttp.Request) {
 	r.Middleware.Next()
 }
 func BindController(group *ghttp.RouterGroup) {
-	//DomeRouter(group)
-
+	DomeRouter(group)
 	group.Group("/api/v1", func(group *ghttp.RouterGroup) {
-		group.Middleware(ghttp.MiddlewareHandlerResponse, MiddlewareCORS)
+		vv := NewRateLimitMiddleware()
+		group.Middleware(ghttp.MiddlewareHandlerResponse, MiddlewareCORS, vv.Middleware)
+
 	})
 }