go-zero/tools/goctl/api/gogen/genmiddleware.go

44 lines
1.1 KiB
Go
Raw Normal View History

package gogen
import (
_ "embed"
"strings"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)
//go:embed middleware.tpl
var middlewareImplementCode string
func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
2021-04-15 19:49:17 +08:00
middlewares := getMiddleware(api)
for _, item := range middlewares {
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
2021-01-09 00:17:23 +08:00
filename, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
if err != nil {
return err
}
name := strings.TrimSuffix(item, "Middleware") + "Middleware"
2021-01-09 00:17:23 +08:00
err = genFile(fileGenConfig{
dir: dir,
subdir: middlewareDir,
filename: filename + ".go",
templateName: "contextTemplate",
category: category,
templateFile: middlewareImplementCodeFile,
2021-01-09 00:17:23 +08:00
builtinTemplate: middlewareImplementCode,
data: map[string]string{
"name": strings.Title(name),
},
})
if err != nil {
return err
}
}
2021-01-09 00:17:23 +08:00
return nil
}