feat: use go:embed to embed templates (#1756)

This commit is contained in:
Kevin Wan
2022-04-04 13:12:05 +08:00
committed by GitHub
parent 58a0b17451
commit faad6e27e3
60 changed files with 739 additions and 691 deletions

View File

@@ -1,6 +1,7 @@
package gogen
import (
_ "embed"
"fmt"
"path"
"strings"
@@ -14,36 +15,10 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/vars"
)
const (
defaultLogicPackage = "logic"
handlerTemplate = `package {{.PkgName}}
const defaultLogicPackage = "logic"
import (
"net/http"
{{if .After1_1_10}}"github.com/zeromicro/go-zero/rest/httpx"{{end}}
{{.ImportPackages}}
)
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 {
httpx.Error(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 {
httpx.Error(w, err)
} else {
{{if .HasResp}}httpx.OkJson(w, resp){{else}}httpx.Ok(w){{end}}
}
}
}
`
)
//go:embed handler.tpl
var handlerTemplate string
type handlerInfo struct {
PkgName string