first commit
This commit is contained in:
28
template/api/handler.tpl
Normal file
28
template/api/handler.tpl
Normal file
@@ -0,0 +1,28 @@
|
||||
package {{.PkgName}}
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
{{if .HasRequest}}"github.com/zeromicro/go-zero/rest/httpx"{{end}}
|
||||
{{.ImportPackages}}
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
{{if .HasDoc}}{{.Doc}}{{end}}
|
||||
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
{{if .HasRequest}}var req types.{{.RequestType}}
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
|
||||
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
{{if .HasResp}}xhttp.JsonBaseResponseCtx(r.Context(), w, resp){{else}}xhttp.JsonBaseResponseCtx(r.Context(), w, nil){{end}}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user