handler.tpl 646 B

1234567891011121314151617181920212223242526
  1. package {{.PkgName}}
  2. import (
  3. "net/http"
  4. "github.com/zeromicro/go-zero/rest/httpx"
  5. {{.ImportPackages}}
  6. )
  7. func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
  8. return func(w http.ResponseWriter, r *http.Request) {
  9. {{if .HasRequest}}var req types.{{.RequestType}}
  10. if err := httpx.Parse(r, &req); err != nil {
  11. httpx.Error(w, err)
  12. return
  13. }
  14. {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
  15. {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
  16. if err != nil {
  17. httpx.Error(w, err)
  18. } else {
  19. {{if .HasResp}}httpx.OkJson(w, resp){{else}}httpx.Ok(w){{end}}
  20. }
  21. }
  22. }