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 diff --git a/rest/httpx/responses.go b/rest/httpx/responses.go index d822640a..c57f76df 100644 --- a/rest/httpx/responses.go +++ b/rest/httpx/responses.go @@ -7,6 +7,14 @@ import ( "zero/core/logx" ) +func Error(w http.ResponseWriter, err error) { + http.Error(w, err.Error(), http.StatusBadRequest) +} + +func Ok(w http.ResponseWriter) { + 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 deleted file mode 100644 index 5b258216..00000000 --- a/tools/goctl/api/demo/config/config.go +++ /dev/null @@ -1,7 +0,0 @@ -package config - -import "zero/rest" - -type Config struct { - rest.RestConf -} diff --git a/tools/goctl/api/demo/demo.go b/tools/goctl/api/demo/demo.go deleted file mode 100644 index 1ef5f194..00000000 --- a/tools/goctl/api/demo/demo.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "flag" - - "zero/core/conf" - "zero/rest" - "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 := rest.MustNewServer(c.RestConf) - 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 6dfaa748..00000000 --- a/tools/goctl/api/demo/handler/getuserhandler.go +++ /dev/null @@ -1,33 +0,0 @@ -package handler - -import ( - "net/http" - - "zero/rest/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 0f37a57b..00000000 --- a/tools/goctl/api/demo/handler/handlers.go +++ /dev/null @@ -1,17 +0,0 @@ -package handler - -import ( - "net/http" - - "zero/rest" -) - -func RegisterHandlers(engine *rest.Server) { - engine.AddRoutes([]rest.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 { -} diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 128dbfbd..57ea4e41 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}} + } ` ) @@ -67,14 +71,16 @@ 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 { 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..75221edd 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}} } ` @@ -78,13 +77,13 @@ func genRoutes(dir string, api *spec.ApiSpec) error { },`, r.method, r.path, r.handler) } - jwt := "" + var jwt string 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 := "" + var signature string 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"