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"