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

66 lines
1.5 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gogen
import (
2022-08-06 17:48:59 +08:00
_ "embed"
2020-08-08 15:16:59 +08:00
"fmt"
"strings"
2020-07-29 17:11:41 +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"
"github.com/zeromicro/go-zero/tools/goctl/vars"
2020-07-29 17:11:41 +08:00
)
const (
2022-08-06 17:48:59 +08:00
configFile = "config"
jwtTemplate = ` struct {
AccessSecret string
AccessExpire int64
}
`
jwtTransTemplate = ` struct {
Secret string
PrevSecret string
}
2020-07-29 17:11:41 +08:00
`
)
2022-08-06 17:48:59 +08:00
//go:embed config.tpl
var configTemplate string
func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
filename, err := format.FileNamingFormat(cfg.NamingFormat, configFile)
if err != nil {
return err
}
2021-04-15 19:49:17 +08:00
authNames := getAuths(api)
var auths []string
for _, item := range authNames {
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
}
jwtTransNames := getJwtTrans(api)
var jwtTransList []string
for _, item := range jwtTransNames {
jwtTransList = append(jwtTransList, fmt.Sprintf("%s %s", item, jwtTransTemplate))
}
2021-04-15 19:49:17 +08:00
authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
2020-10-15 16:36:49 +08:00
2021-01-09 00:17:23 +08:00
return genFile(fileGenConfig{
dir: dir,
subdir: configDir,
filename: filename + ".go",
templateName: "configTemplate",
category: category,
templateFile: configTemplateFile,
builtinTemplate: configTemplate,
data: map[string]string{
"authImport": authImportStr,
"auth": strings.Join(auths, "\n"),
"jwtTrans": strings.Join(jwtTransList, "\n"),
2021-01-09 00:17:23 +08:00
},
2020-07-29 17:11:41 +08:00
})
}