genconfig.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package gogen
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/zeromicro/go-zero/tools/goctl/api/spec"
  6. "github.com/zeromicro/go-zero/tools/goctl/config"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  8. "github.com/zeromicro/go-zero/tools/goctl/vars"
  9. )
  10. const (
  11. configFile = "config"
  12. configTemplate = `package config
  13. import {{.authImport}}
  14. type Config struct {
  15. rest.RestConf
  16. {{.auth}}
  17. {{.jwtTrans}}
  18. }
  19. `
  20. jwtTemplate = ` struct {
  21. AccessSecret string
  22. AccessExpire int64
  23. }
  24. `
  25. jwtTransTemplate = ` struct {
  26. Secret string
  27. PrevSecret string
  28. }
  29. `
  30. )
  31. func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  32. filename, err := format.FileNamingFormat(cfg.NamingFormat, configFile)
  33. if err != nil {
  34. return err
  35. }
  36. authNames := getAuths(api)
  37. var auths []string
  38. for _, item := range authNames {
  39. auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
  40. }
  41. jwtTransNames := getJwtTrans(api)
  42. var jwtTransList []string
  43. for _, item := range jwtTransNames {
  44. jwtTransList = append(jwtTransList, fmt.Sprintf("%s %s", item, jwtTransTemplate))
  45. }
  46. authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
  47. return genFile(fileGenConfig{
  48. dir: dir,
  49. subdir: configDir,
  50. filename: filename + ".go",
  51. templateName: "configTemplate",
  52. category: category,
  53. templateFile: configTemplateFile,
  54. builtinTemplate: configTemplate,
  55. data: map[string]string{
  56. "authImport": authImportStr,
  57. "auth": strings.Join(auths, "\n"),
  58. "jwtTrans": strings.Join(jwtTransList, "\n"),
  59. },
  60. })
  61. }