From 29086dfa027988b687229e0b26d01875b0281277 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Tue, 12 Nov 2019 00:00:04 +0900 Subject: [PATCH] ing --- .gitignore | 1 + cmd/server/main.go | 6 +-- go.mod | 1 + go.sum | 4 ++ pkg/loafer/app/annotation/server.go | 23 +++++++++++ pkg/loafer/app/app.go | 21 ++++++++++ pkg/loafer/web/annotation/request-mapping.go | 21 ++++++++++ pkg/loafer/web/annotation/rest-handler.go | 19 +++++++++ pkg/loafer/web/annotation/service.go | 19 +++++++++ pkg/modules/user/handlers/users.go | 15 ------- pkg/modules/user/user-handler.go | 41 ++++++++++++++++++++ pkg/modules/user/user-service.go | 26 +++++++++++++ pkg/server/server.go | 21 ++++++++++ 13 files changed, 200 insertions(+), 18 deletions(-) create mode 100644 .gitignore create mode 100644 pkg/loafer/app/annotation/server.go create mode 100644 pkg/loafer/app/app.go create mode 100644 pkg/loafer/web/annotation/request-mapping.go create mode 100644 pkg/loafer/web/annotation/rest-handler.go create mode 100644 pkg/loafer/web/annotation/service.go delete mode 100644 pkg/modules/user/handlers/users.go create mode 100644 pkg/modules/user/user-handler.go create mode 100644 pkg/modules/user/user-service.go create mode 100644 pkg/server/server.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e40a4e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__debug_bin \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go index 488e440..0897f39 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -6,14 +6,14 @@ import ( "github.com/buaazp/fasthttprouter" "github.com/valyala/fasthttp" - userHandlers "git.loafle.net/totopia/server/pkg/modules/user/handlers" + app "git.loafle.net/totopia/server/pkg/loafer/app" + "git.loafle.net/totopia/server/pkg/server" ) func main() { router := fasthttprouter.New() - router.GET("/user/users", userHandlers.GetUsers) - router.POST("/user/users", userHandlers.PostUsers) + app.Run(server.ServerType) log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler)) } diff --git a/go.mod b/go.mod index 6d46576..eb4f37b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.loafle.net/totopia/server go 1.13 require ( + git.loafle.net/commons/di-go v0.0.0-20191111144101-27dd1f6347da github.com/buaazp/fasthttprouter v0.1.1 github.com/valyala/fasthttp v1.6.0 ) diff --git a/go.sum b/go.sum index d77eaa0..f7ba572 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +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= 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 new file mode 100644 index 0000000..1dbb928 --- /dev/null +++ b/pkg/loafer/app/annotation/server.go @@ -0,0 +1,23 @@ +package annotation + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" +) + +var ServerAnnotationType = reflect.TypeOf((*ServerAnnotation)(nil)) + +func init() { + cda.RegisterAnnotation(ServerAnnotationType) +} + +type ServerAnnotation struct { + cda.TypeAnnotation `@annotation:"@app:Server"` + + RootDir string `json:"rootDir"` + Mount map[string][]string `json:"mount"` + UploadDir string `json:"uploadDir"` + HTTPPort int16 `json:"httpPort"` + HTTPSPort int16 `json:"httpsPort"` +} diff --git a/pkg/loafer/app/app.go b/pkg/loafer/app/app.go new file mode 100644 index 0000000..1a1e6f0 --- /dev/null +++ b/pkg/loafer/app/app.go @@ -0,0 +1,21 @@ +package app + +import ( + "log" + "reflect" + + cdr "git.loafle.net/commons/di-go/registry" + + annotation "git.loafle.net/totopia/server/pkg/loafer/app/annotation" +) + +func Run(t reflect.Type) { + ta := cdr.GetTypeAnnotation(t, annotation.ServerAnnotationType) + if nil == ta { + log.Printf("[%s] is not Server, use @app:Server", t.Elem().Name()) + return + } + vta := ta.(*annotation.ServerAnnotation) + log.Printf("%s %d", t.Elem().Name(), vta.HTTPPort) + +} diff --git a/pkg/loafer/web/annotation/request-mapping.go b/pkg/loafer/web/annotation/request-mapping.go new file mode 100644 index 0000000..95d2d45 --- /dev/null +++ b/pkg/loafer/web/annotation/request-mapping.go @@ -0,0 +1,21 @@ +package annotation + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" +) + +var RequestMappingAnnotationType = reflect.TypeOf((*RequestMappingAnnotation)(nil)) + +func init() { + cda.RegisterAnnotation(RequestMappingAnnotationType) +} + +type RequestMappingAnnotation struct { + cda.TypeAnnotation `@annotation:"@web:RequestMapping"` + + Method string `json:"method"` + Entry string `json:"entry"` + Params []string `json:"params"` +} diff --git a/pkg/loafer/web/annotation/rest-handler.go b/pkg/loafer/web/annotation/rest-handler.go new file mode 100644 index 0000000..d5f6e58 --- /dev/null +++ b/pkg/loafer/web/annotation/rest-handler.go @@ -0,0 +1,19 @@ +package annotation + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" +) + +var RestHandlerAnnotationType = reflect.TypeOf((*RestHandlerAnnotation)(nil)) + +func init() { + cda.RegisterAnnotation(RestHandlerAnnotationType) +} + +type RestHandlerAnnotation struct { + cda.TypeAnnotation `@annotation:"@web:RestHandler"` + + Entry string `json:"entry"` +} diff --git a/pkg/loafer/web/annotation/service.go b/pkg/loafer/web/annotation/service.go new file mode 100644 index 0000000..64ca1da --- /dev/null +++ b/pkg/loafer/web/annotation/service.go @@ -0,0 +1,19 @@ +package annotation + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" +) + +var ServiceAnnotationType = reflect.TypeOf((*ServiceAnnotation)(nil)) + +func init() { + cda.RegisterAnnotation(ServiceAnnotationType) +} + +type ServiceAnnotation struct { + cda.TypeAnnotation `@annotation:"@web:Service"` + + Name string `json:"name"` +} diff --git a/pkg/modules/user/handlers/users.go b/pkg/modules/user/handlers/users.go deleted file mode 100644 index 276b4b6..0000000 --- a/pkg/modules/user/handlers/users.go +++ /dev/null @@ -1,15 +0,0 @@ -package users - -import ( - "fmt" - - "github.com/valyala/fasthttp" -) - -func GetUsers(ctx *fasthttp.RequestCtx) { - fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name")) -} - -func PostUsers(ctx *fasthttp.RequestCtx) { - fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name")) -} diff --git a/pkg/modules/user/user-handler.go b/pkg/modules/user/user-handler.go new file mode 100644 index 0000000..5416995 --- /dev/null +++ b/pkg/modules/user/user-handler.go @@ -0,0 +1,41 @@ +package user + +import ( + "fmt" + "reflect" + + "github.com/valyala/fasthttp" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + + // For annotation + _ "git.loafle.net/totopia/server/pkg/loafer/web/annotation" +) + +var UserHandlerType = reflect.TypeOf((*UserHandler)(nil)) + +func init() { + cdr.RegisterType(UserHandlerType) +} + +type UserHandler struct { + cda.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\"])"` + + UserService *UserService `annotation:"@Inject()"` +} + +func (us *UserHandler) List(ctx *fasthttp.RequestCtx) error { + fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name")) + + return nil +} + +func (us *UserHandler) Detail(ctx *fasthttp.RequestCtx, userId string) error { + fmt.Fprintf(ctx, "hello, %s!\n", ctx.UserValue("name")) + + return nil +} diff --git a/pkg/modules/user/user-service.go b/pkg/modules/user/user-service.go new file mode 100644 index 0000000..683f08d --- /dev/null +++ b/pkg/modules/user/user-service.go @@ -0,0 +1,26 @@ +package user + +import ( + "log" + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + + // For annotation + _ "git.loafle.net/totopia/server/pkg/loafer/web/annotation" +) + +var UserServiceType = reflect.TypeOf((*UserService)(nil)) + +func init() { + cdr.RegisterType(UserServiceType) +} + +type UserService struct { + cda.TypeAnnotation `annotation:"@web:Service(\"name\": \"/userService\")"` +} + +func (us *UserService) FindAll() { + log.Printf("UserService FindAll") +} diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..83924d8 --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,21 @@ +package server + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + + // For annotation + _ "git.loafle.net/totopia/server/pkg/loafer/app/annotation" +) + +var ServerType = reflect.TypeOf((*Server)(nil)) + +func init() { + cdr.RegisterType(ServerType) +} + +type Server struct { + cda.TypeAnnotation `annotation:"@app:Server(\"httpPort\": 8080)"` +}