From cf8e0a118f75c0ebef98456cd6759b47f23965b1 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 31 Jul 2020 17:03:19 +0800 Subject: [PATCH 1/5] refactor ngin to rest --- rest/httpx/responses.go | 9 +++++++++ tools/goctl/api/demo/config/config.go | 4 ++-- tools/goctl/api/demo/demo.go | 4 ++-- .../goctl/api/demo/handler/getuserhandler.go | 2 +- tools/goctl/api/demo/handler/handlers.go | 6 +++--- tools/goctl/api/gogen/gen.go | 5 ++--- tools/goctl/api/gogen/genhandlers.go | 19 ++++++++++--------- tools/goctl/api/gogen/genmain.go | 2 +- tools/goctl/api/gogen/genroutes.go | 18 +++--------------- tools/goctl/vars/settings.go | 2 +- 10 files changed, 34 insertions(+), 37 deletions(-) diff --git a/rest/httpx/responses.go b/rest/httpx/responses.go index d822640a..7d2ce1e5 100644 --- a/rest/httpx/responses.go +++ b/rest/httpx/responses.go @@ -7,6 +7,15 @@ import ( "zero/core/logx" ) +func Error(w http.ResponseWriter, err error) { + http.Error(w, err.Error(), http.StatusBadRequest) +} + +func Ok(w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(http.StatusOK) +} + func OkJson(w http.ResponseWriter, v interface{}) { WriteJson(w, http.StatusOK, v) } diff --git a/tools/goctl/api/demo/config/config.go b/tools/goctl/api/demo/config/config.go index 5b258216..5beaa249 100644 --- a/tools/goctl/api/demo/config/config.go +++ b/tools/goctl/api/demo/config/config.go @@ -1,7 +1,7 @@ package config -import "zero/rest" +import "zero/ngin" type Config struct { - rest.RestConf + ngin.NgConf } diff --git a/tools/goctl/api/demo/demo.go b/tools/goctl/api/demo/demo.go index 1ef5f194..3c203a08 100644 --- a/tools/goctl/api/demo/demo.go +++ b/tools/goctl/api/demo/demo.go @@ -4,7 +4,7 @@ import ( "flag" "zero/core/conf" - "zero/rest" + "zero/ngin" "zero/tools/goctl/api/demo/config" "zero/tools/goctl/api/demo/handler" ) @@ -17,7 +17,7 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - engine := rest.MustNewServer(c.RestConf) + engine := ngin.MustNewEngine(c.NgConf) defer engine.Stop() handler.RegisterHandlers(engine) diff --git a/tools/goctl/api/demo/handler/getuserhandler.go b/tools/goctl/api/demo/handler/getuserhandler.go index 6dfaa748..1baa717c 100644 --- a/tools/goctl/api/demo/handler/getuserhandler.go +++ b/tools/goctl/api/demo/handler/getuserhandler.go @@ -3,7 +3,7 @@ package handler import ( "net/http" - "zero/rest/httpx" + "zero/ngin/httpx" ) type ( diff --git a/tools/goctl/api/demo/handler/handlers.go b/tools/goctl/api/demo/handler/handlers.go index 0f37a57b..24e71299 100644 --- a/tools/goctl/api/demo/handler/handlers.go +++ b/tools/goctl/api/demo/handler/handlers.go @@ -3,11 +3,11 @@ package handler import ( "net/http" - "zero/rest" + "zero/ngin" ) -func RegisterHandlers(engine *rest.Server) { - engine.AddRoutes([]rest.Route{ +func RegisterHandlers(engine *ngin.Engine) { + engine.AddRoutes([]ngin.Route{ { Method: http.MethodGet, Path: "/", diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 14adbf29..493b0c10 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -12,14 +12,13 @@ import ( "sync" "time" + "github.com/logrusorgru/aurora" + "github.com/urfave/cli" "zero/core/lang" apiformat "zero/tools/goctl/api/format" "zero/tools/goctl/api/parser" apiutil "zero/tools/goctl/api/util" "zero/tools/goctl/util" - - "github.com/logrusorgru/aurora" - "github.com/urfave/cli" ) const tmpFile = "%s-%d" diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 128dbfbd..894aeb3c 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -19,6 +19,7 @@ const ( import ( "net/http" + "zero/rest/httpx" {{.importPackages}} ) @@ -34,14 +35,17 @@ func {{.handlerName}}(ctx *svc.ServiceContext) http.HandlerFunc { ` parseRequestTemplate = `var req {{.requestType}} if err := httpx.Parse(r, &req); err != nil { - logx.Error(err) - http.Error(w, err.Error(), http.StatusBadRequest) + httpx.Error(w, err) return } ` hasRespTemplate = ` {{.logicResponse}} l.{{.callee}}({{.req}}) - // TODO write data to response + if err != nil { + httpx.Error(w, err) + } else { + {{.respWriter}} + } ` ) @@ -69,12 +73,14 @@ func genHandler(dir string, group spec.Group, route spec.Route) error { } var logicResponse = "" var writeResponse = "nil, nil" + var respWriter = `httpx.WriteJson(w, http.StatusOK, resp)` if len(route.ResponseType.Name) > 0 { logicResponse = "resp, err :=" writeResponse = "resp, err" } else { logicResponse = "err :=" writeResponse = "nil, err" + respWriter = `httpx.Ok(w)` } var logicBodyBuilder strings.Builder t := template.Must(template.New("hasRespTemplate").Parse(hasRespTemplate)) @@ -83,6 +89,7 @@ func genHandler(dir string, group spec.Group, route spec.Route) error { "req": req, "logicResponse": logicResponse, "writeResponse": writeResponse, + "respWriter": respWriter, }); err != nil { return err } @@ -155,12 +162,6 @@ func genHandlers(dir string, api *spec.ApiSpec) error { func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string { var imports []string - if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 { - imports = append(imports, "\"zero/core/httpx\"") - } - if len(route.RequestType.Name) > 0 { - imports = append(imports, "\"zero/core/logx\"") - } imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir))) if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 { imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir))) diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index cfc29fa1..6dc1b07b 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -30,7 +30,7 @@ func main() { ctx := svc.NewServiceContext(c) - engine := rest.MustNewEngine(c.RestConf) + engine := rest.MustNewServer(c.RestConf) defer engine.Stop() handler.RegisterHandlers(engine, ctx) diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 79c40b93..8897768d 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -2,7 +2,6 @@ package gogen import ( "bytes" - "errors" "fmt" "path" "sort" @@ -26,7 +25,7 @@ import ( {{.importPackages}} ) -func RegisterHandlers(engine *rest.Engine, serverCtx *svc.ServiceContext) { +func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) { {{.routesAdditions}} } ` @@ -80,11 +79,11 @@ func genRoutes(dir string, api *spec.ApiSpec) error { } jwt := "" if g.jwtEnabled { - jwt = fmt.Sprintf(", rest.WithJwt(serverCtx.Config.%s.AccessSecret)", g.authName) + jwt = fmt.Sprintf(", ngin.WithJwt(serverCtx.Config.%s.AccessSecret)", g.authName) } signature := "" if g.signatureEnabled { - signature = fmt.Sprintf(", rest.WithSignature(serverCtx.Config.%s.Signature)", g.authName) + signature = fmt.Sprintf(", ngin.WithSignature(serverCtx.Config.%s.Signature)", g.authName) } if err := gt.Execute(&builder, map[string]string{ "routes": strings.TrimSpace(gbuilder.String()), @@ -175,17 +174,6 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) { handler: handler, }) } - - if value, ok := apiutil.GetAnnotationValue(g.Annotations, "server", "jwt"); ok { - groupedRoutes.authName = value - groupedRoutes.jwtEnabled = true - } - if value, ok := apiutil.GetAnnotationValue(g.Annotations, "server", "signature"); ok { - if groupedRoutes.authName != "" && groupedRoutes.authName != value { - return nil, errors.New("auth signature should config same") - } - groupedRoutes.signatureEnabled = true - } routes = append(routes, groupedRoutes) } diff --git a/tools/goctl/vars/settings.go b/tools/goctl/vars/settings.go index 68f472fb..c7b9071f 100644 --- a/tools/goctl/vars/settings.go +++ b/tools/goctl/vars/settings.go @@ -1,3 +1,3 @@ package vars -const ProjectName = "xiao" +const ProjectName = "zero" From 3ea0d8e7f61988e64a7558ad39e5d2f4d9689f24 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 31 Jul 2020 17:05:49 +0800 Subject: [PATCH 2/5] format --- tools/goctl/api/gogen/gen.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 493b0c10..14adbf29 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -12,13 +12,14 @@ import ( "sync" "time" - "github.com/logrusorgru/aurora" - "github.com/urfave/cli" "zero/core/lang" apiformat "zero/tools/goctl/api/format" "zero/tools/goctl/api/parser" apiutil "zero/tools/goctl/api/util" "zero/tools/goctl/util" + + "github.com/logrusorgru/aurora" + "github.com/urfave/cli" ) const tmpFile = "%s-%d" From 2f4505d856a22ca7ac9dc7becedd12ff94eed565 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 31 Jul 2020 17:09:47 +0800 Subject: [PATCH 3/5] format --- tools/goctl/api/demo/config/config.go | 7 ---- tools/goctl/api/demo/demo.go | 25 -------------- tools/goctl/api/demo/etc/user.json | 8 ----- .../goctl/api/demo/handler/getuserhandler.go | 33 ------------------- tools/goctl/api/demo/handler/handlers.go | 17 ---------- tools/goctl/api/demo/svc/servicecontext.go | 4 --- 6 files changed, 94 deletions(-) delete mode 100644 tools/goctl/api/demo/config/config.go delete mode 100644 tools/goctl/api/demo/demo.go delete mode 100644 tools/goctl/api/demo/etc/user.json delete mode 100644 tools/goctl/api/demo/handler/getuserhandler.go delete mode 100644 tools/goctl/api/demo/handler/handlers.go delete mode 100644 tools/goctl/api/demo/svc/servicecontext.go diff --git a/tools/goctl/api/demo/config/config.go b/tools/goctl/api/demo/config/config.go deleted file mode 100644 index 5beaa249..00000000 --- a/tools/goctl/api/demo/config/config.go +++ /dev/null @@ -1,7 +0,0 @@ -package config - -import "zero/ngin" - -type Config struct { - ngin.NgConf -} diff --git a/tools/goctl/api/demo/demo.go b/tools/goctl/api/demo/demo.go deleted file mode 100644 index 3c203a08..00000000 --- a/tools/goctl/api/demo/demo.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "flag" - - "zero/core/conf" - "zero/ngin" - "zero/tools/goctl/api/demo/config" - "zero/tools/goctl/api/demo/handler" -) - -var configFile = flag.String("f", "etc/user.json", "the config file") - -func main() { - flag.Parse() - - var c config.Config - conf.MustLoad(*configFile, &c) - - engine := ngin.MustNewEngine(c.NgConf) - defer engine.Stop() - - handler.RegisterHandlers(engine) - engine.Start() -} diff --git a/tools/goctl/api/demo/etc/user.json b/tools/goctl/api/demo/etc/user.json deleted file mode 100644 index cd52be6f..00000000 --- a/tools/goctl/api/demo/etc/user.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Name": "user", - "Host": "127.0.0.1", - "Port": 3333, - "Log": { - "Mode": "console" - } -} diff --git a/tools/goctl/api/demo/handler/getuserhandler.go b/tools/goctl/api/demo/handler/getuserhandler.go deleted file mode 100644 index 1baa717c..00000000 --- a/tools/goctl/api/demo/handler/getuserhandler.go +++ /dev/null @@ -1,33 +0,0 @@ -package handler - -import ( - "net/http" - - "zero/ngin/httpx" -) - -type ( - request struct { - User string `form:"user,optional"` - } - - response struct { - Code int `json:"code"` - Greet string `json:"greet"` - From string `json:"from,omitempty"` - } -) - -func GreetHandler(w http.ResponseWriter, r *http.Request) { - var req request - err := httpx.Parse(r, &req) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - httpx.OkJson(w, response{ - Code: 0, - Greet: "hello", - }) -} diff --git a/tools/goctl/api/demo/handler/handlers.go b/tools/goctl/api/demo/handler/handlers.go deleted file mode 100644 index 24e71299..00000000 --- a/tools/goctl/api/demo/handler/handlers.go +++ /dev/null @@ -1,17 +0,0 @@ -package handler - -import ( - "net/http" - - "zero/ngin" -) - -func RegisterHandlers(engine *ngin.Engine) { - engine.AddRoutes([]ngin.Route{ - { - Method: http.MethodGet, - Path: "/", - Handler: GreetHandler, - }, - }) -} diff --git a/tools/goctl/api/demo/svc/servicecontext.go b/tools/goctl/api/demo/svc/servicecontext.go deleted file mode 100644 index 51137699..00000000 --- a/tools/goctl/api/demo/svc/servicecontext.go +++ /dev/null @@ -1,4 +0,0 @@ -package svc - -type ServiceContext struct { -} From 44290aa4d6556035accebc5f9f0d89d7c5db9d1f Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 31 Jul 2020 17:11:59 +0800 Subject: [PATCH 4/5] refactor --- rest/httpx/responses.go | 1 - tools/goctl/api/gogen/genhandlers.go | 2 +- tools/goctl/api/gogen/genroutes.go | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/rest/httpx/responses.go b/rest/httpx/responses.go index 7d2ce1e5..c57f76df 100644 --- a/rest/httpx/responses.go +++ b/rest/httpx/responses.go @@ -12,7 +12,6 @@ func Error(w http.ResponseWriter, err error) { } func Ok(w http.ResponseWriter) { - w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) } diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 894aeb3c..57ea4e41 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -71,7 +71,7 @@ func genHandler(dir string, group spec.Group, route spec.Route) error { if len(route.RequestType.Name) == 0 { req = "" } - var logicResponse = "" + var logicResponse string var writeResponse = "nil, nil" var respWriter = `httpx.WriteJson(w, http.StatusOK, resp)` if len(route.ResponseType.Name) > 0 { diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 8897768d..75221edd 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -77,11 +77,11 @@ func genRoutes(dir string, api *spec.ApiSpec) error { },`, r.method, r.path, r.handler) } - jwt := "" + var jwt string if g.jwtEnabled { jwt = fmt.Sprintf(", ngin.WithJwt(serverCtx.Config.%s.AccessSecret)", g.authName) } - signature := "" + var signature string if g.signatureEnabled { signature = fmt.Sprintf(", ngin.WithSignature(serverCtx.Config.%s.Signature)", g.authName) } From 2a2f57fcce76c9958d4695d19ea93bcbfd4d7c93 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 31 Jul 2020 17:22:33 +0800 Subject: [PATCH 5/5] add runner --- .gitlab-ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a0ff831e..ae16716a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,9 @@ analysis: stage: analysis image: golang script: - - go version && go env - - go test -short $(go list ./...) | grep -v "no test" + - go version && go env + - go test -short $(go list ./...) | grep -v "no test" only: - - merge_requests + - merge_requests + tags: + - common