From b2800a336b9fc5892ac51dbed1198b5e78d77fb1 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Wed, 13 Nov 2019 23:21:05 +0900 Subject: [PATCH] ing --- go.mod | 3 +- go.sum | 13 ++-- pkg/loafer/app/annotation/server.go | 8 ++- pkg/loafer/app/app.go | 64 ++++++++++++++++---- pkg/loafer/web/annotation/request-mapping.go | 8 ++- pkg/loafer/web/annotation/rest-handler.go | 8 ++- pkg/loafer/web/annotation/service.go | 8 ++- pkg/modules/user/user-handler.go | 12 ++-- pkg/modules/user/user-service.go | 9 ++- pkg/server/server.go | 10 +-- 10 files changed, 100 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index eb4f37b..9ae62c0 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module git.loafle.net/totopia/server go 1.13 require ( - git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da + git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b + git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d github.com/buaazp/fasthttprouter v0.1.1 github.com/valyala/fasthttp v1.6.0 ) diff --git a/go.sum b/go.sum index f7ba572..da773da 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,12 @@ -git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da h1:g0syubB28a2puIRlb/wbKLYSs+5FwszAe0xbixqn8eY= -git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da/go.mod h1:k0EtoXnmtf/Qdh1ZLTsR18Zi5hNiGan9fa4/LiNW5z8= -git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762 h1:H1u26DfvdLJLe7lyjGrAZP0zzmDIjZkSQuo2Oc34Vug= -git.loafle.net/commons/util-go v0.0.0-20191105144744-e18f27e20762/go.mod h1:YgLbLOUN6ODD9ZPrpUWIjWVADlwGGCDmNZRy6yExbR0= +git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk= +git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b h1:pWkpKOtuHNIgUFiEpoaX8WpL/7O26nwCEAbsf8UHlHk= +git.loafle.net/loafer/annotation-go v0.0.0-20191113141909-a254c5695d3b/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk= +git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d h1:PG6EhlgwPTAADqlvjuytXcurtyQH/rzxTA6s06yyhlY= +git.loafle.net/loafer/di-go v0.0.0-20191113141126-d08fa9dbe72d/go.mod h1:dccC+qAAeeWATVEZ8acCRFvnSGbwxCasnmwez7cCkrY= +git.loafle.net/loafer/util-go v0.0.0-20191112142134-9a567d18b779 h1:TcBAz07pghDjiwGLgWrPVbovK4i8gnROGqFIwzxzarA= +git.loafle.net/loafer/util-go v0.0.0-20191112142134-9a567d18b779/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU= +git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d h1:ESDbDHHzH2Ysq+thQrO/OQtyDkVhzNzshjn0SJIqa0g= +git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU= github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q= github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM= github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= diff --git a/pkg/loafer/app/annotation/server.go b/pkg/loafer/app/annotation/server.go index 1dbb928..1998180 100644 --- a/pkg/loafer/app/annotation/server.go +++ b/pkg/loafer/app/annotation/server.go @@ -3,17 +3,19 @@ package annotation import ( "reflect" - cda "git.loafle.net/commons/di-go/annotation" + "git.loafle.net/loafer/annotation-go" ) var ServerAnnotationType = reflect.TypeOf((*ServerAnnotation)(nil)) func init() { - cda.RegisterAnnotation(ServerAnnotationType) + if err := annotation.Register(ServerAnnotationType); nil != err { + panic(err) + } } type ServerAnnotation struct { - cda.TypeAnnotation `@annotation:"@app:Server"` + annotation.TypeAnnotation `@annotation:"@app:Server"` RootDir string `json:"rootDir"` Mount map[string][]string `json:"mount"` diff --git a/pkg/loafer/app/app.go b/pkg/loafer/app/app.go index 791371f..3abc26c 100644 --- a/pkg/loafer/app/app.go +++ b/pkg/loafer/app/app.go @@ -1,17 +1,23 @@ package app import ( + "fmt" "log" "reflect" - cdr "git.loafle.net/commons/di-go/registry" + "git.loafle.net/loafer/di-go" appAnnotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation" webAnnotation "git.loafle.net/totopia/server/pkg/loafer/web/annotation" ) +type MethodMapping struct { + Method string + ParamKeys []string +} + func Run(t reflect.Type) { - taServer := cdr.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType) + taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType) if nil == taServer { log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name()) return @@ -19,21 +25,57 @@ func Run(t reflect.Type) { aServer := taServer.(*appAnnotation.ServerAnnotation) log.Printf("%s %d", t.Elem().Name(), aServer.HTTPPort) - restHandlers, err := cdr.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType) + restHandlers, err := di.GetInstancesByAnnotationType(webAnnotation.RestHandlerAnnotationType) if nil != err { log.Printf("[%v]", err) return } for _, restHandler := range restHandlers { - t := reflect.TypeOf(restHandler) - ta := cdr.GetTypeAnnotation(t, webAnnotation.RestHandlerAnnotationType) - if nil == ta { - log.Printf("Service[%s] is not RESTService, use @RESTService", t.Elem().Name()) - continue - } - log.Printf("%s %v", t.Elem().Name(), ta) - + parseRestHandler(restHandler) + parseRequestMapping(restHandler) } } + +func parseRestHandler(restHandler interface{}) { + t := reflect.TypeOf(restHandler) + ta := di.GetTypeAnnotation(t, webAnnotation.RestHandlerAnnotationType) + if nil == ta { + log.Printf("Service[%s] is not RESTService, use @RESTService", t.Elem().Name()) + return + } + log.Printf("%s %v", t.Elem().Name(), ta) +} + +func parseRequestMapping(restHandler interface{}) { + t := reflect.TypeOf(restHandler) + mas := di.GetMethodAnnotations(t, webAnnotation.RequestMappingAnnotationType) + if nil == mas || 0 == len(mas) { + return + } + + methodMapping := make(map[string]map[string]*MethodMapping) + + for methodName, v := range mas { + ma := v.(*webAnnotation.RequestMappingAnnotation) + mm, ok := methodMapping[ma.Method] + if !ok { + mm = make(map[string]*MethodMapping) + methodMapping[ma.Method] = mm + } + + _, ok = mm[ma.Entry] + if ok { + log.Printf("Mapping of method[%s], entry[%s] is exist already", ma.Method, ma.Entry) + continue + } + + mm[ma.Entry] = &MethodMapping{ + Method: fmt.Sprintf("%s.%s", t.Elem().Name(), methodName), + ParamKeys: ma.Params, + } + } + + log.Printf("%s %v", t.Elem().Name(), methodMapping) +} diff --git a/pkg/loafer/web/annotation/request-mapping.go b/pkg/loafer/web/annotation/request-mapping.go index 95d2d45..e8be645 100644 --- a/pkg/loafer/web/annotation/request-mapping.go +++ b/pkg/loafer/web/annotation/request-mapping.go @@ -3,17 +3,19 @@ package annotation import ( "reflect" - cda "git.loafle.net/commons/di-go/annotation" + "git.loafle.net/loafer/annotation-go" ) var RequestMappingAnnotationType = reflect.TypeOf((*RequestMappingAnnotation)(nil)) func init() { - cda.RegisterAnnotation(RequestMappingAnnotationType) + if err := annotation.Register(RequestMappingAnnotationType); nil != err { + panic(err) + } } type RequestMappingAnnotation struct { - cda.TypeAnnotation `@annotation:"@web:RequestMapping"` + annotation.TypeAnnotation `@annotation:"@web:RequestMapping"` Method string `json:"method"` Entry string `json:"entry"` diff --git a/pkg/loafer/web/annotation/rest-handler.go b/pkg/loafer/web/annotation/rest-handler.go index d5f6e58..7603248 100644 --- a/pkg/loafer/web/annotation/rest-handler.go +++ b/pkg/loafer/web/annotation/rest-handler.go @@ -3,17 +3,19 @@ package annotation import ( "reflect" - cda "git.loafle.net/commons/di-go/annotation" + "git.loafle.net/loafer/annotation-go" ) var RestHandlerAnnotationType = reflect.TypeOf((*RestHandlerAnnotation)(nil)) func init() { - cda.RegisterAnnotation(RestHandlerAnnotationType) + if err := annotation.Register(RestHandlerAnnotationType); nil != err { + panic(err) + } } type RestHandlerAnnotation struct { - cda.TypeAnnotation `@annotation:"@web:RestHandler"` + annotation.TypeAnnotation `@annotation:"@web:RestHandler"` Entry string `json:"entry"` } diff --git a/pkg/loafer/web/annotation/service.go b/pkg/loafer/web/annotation/service.go index 64ca1da..e16bff6 100644 --- a/pkg/loafer/web/annotation/service.go +++ b/pkg/loafer/web/annotation/service.go @@ -3,17 +3,19 @@ package annotation import ( "reflect" - cda "git.loafle.net/commons/di-go/annotation" + "git.loafle.net/loafer/annotation-go" ) var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil)) func init() { - cda.RegisterAnnotation(ServiceAnnotationType) + if err := annotation.Register(ServiceAnnotationType); nil != err { + panic(err) + } } type ServiceAnnotation struct { - cda.TypeAnnotation `@annotation:"@web:Service"` + annotation.TypeAnnotation `@annotation:"@web:Service"` Name string `json:"name"` } diff --git a/pkg/modules/user/user-handler.go b/pkg/modules/user/user-handler.go index 5416995..7c56444 100644 --- a/pkg/modules/user/user-handler.go +++ b/pkg/modules/user/user-handler.go @@ -6,8 +6,8 @@ import ( "github.com/valyala/fasthttp" - cda "git.loafle.net/commons/di-go/annotation" - cdr "git.loafle.net/commons/di-go/registry" + "git.loafle.net/loafer/annotation-go" + "git.loafle.net/loafer/di-go" // For annotation _ "git.loafle.net/totopia/server/pkg/loafer/web/annotation" @@ -16,14 +16,14 @@ import ( var UserHandlerType = reflect.TypeOf((*UserHandler)(nil)) func init() { - cdr.RegisterType(UserHandlerType) + di.RegisterType(UserHandlerType) } type UserHandler struct { - cda.TypeAnnotation `annotation:"@web:RestHandler(\"entry\": \"/users\")"` + annotation.TypeAnnotation `annotation:"@web:RestHandler(\"entry\": \"/users\")"` - _List cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"params\": [])"` - _Detail cda.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"entry\": \"/:id\", \"params\"=[\"userId\"])"` + _List annotation.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"params\": [])"` + _Detail annotation.MethodAnnotation `annotation:"@web:RequestMapping(\"method\": \"GET\", \"entry\": \"/:id\", \"params\":[\"userId\"])"` UserService *UserService `annotation:"@Inject()"` } diff --git a/pkg/modules/user/user-service.go b/pkg/modules/user/user-service.go index 683f08d..3e0eceb 100644 --- a/pkg/modules/user/user-service.go +++ b/pkg/modules/user/user-service.go @@ -4,21 +4,20 @@ import ( "log" "reflect" - cda "git.loafle.net/commons/di-go/annotation" - cdr "git.loafle.net/commons/di-go/registry" - // For annotation + "git.loafle.net/loafer/annotation-go" + "git.loafle.net/loafer/di-go" _ "git.loafle.net/totopia/server/pkg/loafer/web/annotation" ) var UserServiceType = reflect.TypeOf((*UserService)(nil)) func init() { - cdr.RegisterType(UserServiceType) + di.RegisterType(UserServiceType) } type UserService struct { - cda.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"` + annotation.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"` } func (us *UserService) FindAll() { diff --git a/pkg/server/server.go b/pkg/server/server.go index e2d72f4..4a3d70e 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -3,8 +3,8 @@ package server import ( "reflect" - cda "git.loafle.net/commons/di-go/annotation" - cdr "git.loafle.net/commons/di-go/registry" + "git.loafle.net/loafer/annotation-go" + "git.loafle.net/loafer/di-go" // For annotation _ "git.loafle.net/totopia/server/pkg/loafer/app/annotation" @@ -16,9 +16,11 @@ import ( var ServerType = reflect.TypeOf((*Server)(nil)) func init() { - cdr.RegisterType(ServerType) + if err := di.RegisterType(ServerType); nil != err { + panic(err) + } } type Server struct { - cda.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"` + annotation.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"` }