2020-10-23 21:53:45 +08:00
|
|
|
package gogen
|
|
|
|
|
|
|
|
import (
|
2022-04-04 13:12:05 +08:00
|
|
|
_ "embed"
|
2020-10-23 21:53:45 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"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"
|
2020-10-23 21:53:45 +08:00
|
|
|
)
|
|
|
|
|
2022-04-04 13:12:05 +08:00
|
|
|
//go:embed middleware.tpl
|
|
|
|
var middlewareImplementCode string
|
2020-10-23 21:53:45 +08:00
|
|
|
|
2020-11-24 15:11:18 +08:00
|
|
|
func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
|
2021-04-15 19:49:17 +08:00
|
|
|
middlewares := getMiddleware(api)
|
2020-10-23 21:53:45 +08:00
|
|
|
for _, item := range middlewares {
|
2020-11-24 15:11:18 +08:00
|
|
|
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
|
2021-01-09 00:17:23 +08:00
|
|
|
filename, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
|
2020-11-24 15:11:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-23 21:53:45 +08:00
|
|
|
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",
|
2022-01-18 10:45:05 +08:00
|
|
|
category: category,
|
|
|
|
templateFile: middlewareImplementCodeFile,
|
2021-01-09 00:17:23 +08:00
|
|
|
builtinTemplate: middlewareImplementCode,
|
|
|
|
data: map[string]string{
|
|
|
|
"name": strings.Title(name),
|
|
|
|
},
|
2020-10-23 21:53:45 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-11-07 21:25:52 +08:00
|
|
|
return err
|
2020-10-23 21:53:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 00:17:23 +08:00
|
|
|
|
2020-10-23 21:53:45 +08:00
|
|
|
return nil
|
|
|
|
}
|