genconfig.go 1.5 KB

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