|
|
@ -19,6 +19,7 @@ const (
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"zero/rest/httpx"
|
|
|
|
{{.importPackages}}
|
|
|
|
{{.importPackages}}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -34,14 +35,17 @@ func {{.handlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
`
|
|
|
|
`
|
|
|
|
parseRequestTemplate = `var req {{.requestType}}
|
|
|
|
parseRequestTemplate = `var req {{.requestType}}
|
|
|
|
if err := httpx.Parse(r, &req); err != nil {
|
|
|
|
if err := httpx.Parse(r, &req); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
httpx.Error(w, err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
`
|
|
|
|
hasRespTemplate = `
|
|
|
|
hasRespTemplate = `
|
|
|
|
{{.logicResponse}} l.{{.callee}}({{.req}})
|
|
|
|
{{.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 logicResponse = ""
|
|
|
|
var writeResponse = "nil, nil"
|
|
|
|
var writeResponse = "nil, nil"
|
|
|
|
|
|
|
|
var respWriter = `httpx.WriteJson(w, http.StatusOK, resp)`
|
|
|
|
if len(route.ResponseType.Name) > 0 {
|
|
|
|
if len(route.ResponseType.Name) > 0 {
|
|
|
|
logicResponse = "resp, err :="
|
|
|
|
logicResponse = "resp, err :="
|
|
|
|
writeResponse = "resp, err"
|
|
|
|
writeResponse = "resp, err"
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
logicResponse = "err :="
|
|
|
|
logicResponse = "err :="
|
|
|
|
writeResponse = "nil, err"
|
|
|
|
writeResponse = "nil, err"
|
|
|
|
|
|
|
|
respWriter = `httpx.Ok(w)`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var logicBodyBuilder strings.Builder
|
|
|
|
var logicBodyBuilder strings.Builder
|
|
|
|
t := template.Must(template.New("hasRespTemplate").Parse(hasRespTemplate))
|
|
|
|
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,
|
|
|
|
"req": req,
|
|
|
|
"logicResponse": logicResponse,
|
|
|
|
"logicResponse": logicResponse,
|
|
|
|
"writeResponse": writeResponse,
|
|
|
|
"writeResponse": writeResponse,
|
|
|
|
|
|
|
|
"respWriter": respWriter,
|
|
|
|
}); err != nil {
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
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 {
|
|
|
|
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
|
|
|
|
var imports []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)))
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir)))
|
|
|
|
if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 {
|
|
|
|
if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 {
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir)))
|
|
|
|
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir)))
|
|
|
|