手把手开发Admin 系列三(自定义模板篇)
go-zero:go-zero.dev/cn
账号 密码 备注 demo 123456 演示账号
main
ark-admin自定义了main模板(位置:dev/goctl/1.4.0/api/main.tpl)
在main中增加了自定义错误处理
package main
import (
"flag"
"fmt"
"net/http"
"ark-admin-zero/common/errorx"
{{.importPackages}}
"github.com/zeromicro/go-zero/rest/httpx"
)
var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
// 自定义错误
httpx.SetErrorHandler(func(err error) (int, interface{}) {
switch e := err.(type) {
case *errorx.CodeError:
return http.StatusOK, e.Data()
default:
return http.StatusInternalServerError, nil
}
})
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
handler
为了引入go-playground/validatoropen,ark-admin自定义了handler(位置:dev/goctl/1.4.0/api/handler.tpl)
在main中增加了参数校验和中文错误提示
package {{.PkgName}}
import (
{{if .HasRequest}}"errors"{{end}}
"net/http"
{{if .HasRequest}}"reflect"{{end}}
{{if .HasRequest}}"ark-admin-zero/common/errorx"{{end}}
"ark-admin-zero/common/response"
{{.ImportPackages}}
{{if .HasRequest}}
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
translations "github.com/go-playground/validator/v10/translations/zh"{{end}}
"github.com/zeromicro/go-zero/rest/httpx"
)
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, errorx.NewHandlerError(errorx.ParamErrorCode, err.Error()))
return
}
validate := validator.New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := fld.Tag.Get("label")
return name
})
trans, _ := ut.New(zh.New()).GetTranslator("zh")
validateErr := translations.RegisterDefaultTranslations(validate, trans)
if validateErr = validate.StructCtx(r.Context(), req); validateErr != nil {
for _, err := range validateErr.(validator.ValidationErrors) {
httpx.Error(w, errorx.NewHandlerError(errorx.ParamErrorCode, errors.New(err.Translate(trans)).Error()))
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)
return
}
{{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}
}
}
解析
由于自定义了goctl模板,所以在使用goctl生成api时,请指定goctl模板路径为:dev/goctl
例如(在项目根目录下执行)
goctl api go -api app/core/cmd/api/core.api -dir app/core/cmd/api -home ./dev/goctl
自定义main模板后生成的main方法如下
package main
import (
"flag"
"fmt"
"net/http"
"ark-admin-zero/app/core/cmd/api/internal/config"
"ark-admin-zero/app/core/cmd/api/internal/handler"
"ark-admin-zero/app/core/cmd/api/internal/svc"
"ark-admin-zero/common/errorx"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
)
var configFile = flag.String("f", "etc/core-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
// 自定义错误
httpx.SetErrorHandler(func(err error) (int, interface{}) {
switch e := err.(type) {
case *errorx.CodeError:
return http.StatusOK, e.Data()
default:
return http.StatusInternalServerError, nil
}
})
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
以登录接口为例,自定义handler模板后生成的LoginHandler如下
package user
import (
"errors"
"net/http"
"reflect"
"ark-admin-zero/app/core/cmd/api/internal/logic/user"
"ark-admin-zero/app/core/cmd/api/internal/svc"
"ark-admin-zero/app/core/cmd/api/internal/types"
"ark-admin-zero/common/errorx"
"ark-admin-zero/common/response"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
translations "github.com/go-playground/validator/v10/translations/zh"
"github.com/zeromicro/go-zero/rest/httpx"
)
func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, errorx.NewHandlerError(errorx.ParamErrorCode, err.Error()))
return
}
validate := validator.New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := fld.Tag.Get("label")
return name
})
trans, _ := ut.New(zh.New()).GetTranslator("zh")
validateErr := translations.RegisterDefaultTranslations(validate, trans)
if validateErr = validate.StructCtx(r.Context(), req); validateErr != nil {
for _, err := range validateErr.(validator.ValidationErrors) {
httpx.Error(w, errorx.NewHandlerError(errorx.ParamErrorCode, errors.New(err.Translate(trans)).Error()))
return
}
}
l := user.NewLoginLogic(r.Context(), svcCtx)
resp, err := l.Login(&req, r)
if err != nil {
httpx.Error(w, err)
return
}
response.Response(w, resp, err)
}
}
转载自:https://juejin.cn/post/7138250680849924104