refactor ngin to rest

master
kim 4 years ago
parent b73684d9a6
commit cf8e0a118f

@ -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)
}

@ -1,7 +1,7 @@
package config
import "zero/rest"
import "zero/ngin"
type Config struct {
rest.RestConf
ngin.NgConf
}

@ -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)

@ -3,7 +3,7 @@ package handler
import (
"net/http"
"zero/rest/httpx"
"zero/ngin/httpx"
)
type (

@ -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: "/",

@ -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"

@ -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)))

@ -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)

@ -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)
}

@ -1,3 +1,3 @@
package vars
const ProjectName = "xiao"
const ProjectName = "zero"

Loading…
Cancel
Save