genetc.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package gogen
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  7. "github.com/tal-tech/go-zero/tools/goctl/config"
  8. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  9. )
  10. const (
  11. defaultPort = 8888
  12. etcDir = "etc"
  13. etcTemplate = `Name: {{.serviceName}}
  14. Host: {{.host}}
  15. Port: {{.port}}
  16. `
  17. )
  18. func genEtc(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  19. filename, err := format.FileNamingFormat(cfg.NamingFormat, api.Service.Name)
  20. if err != nil {
  21. return err
  22. }
  23. service := api.Service
  24. host, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "host")
  25. if !ok {
  26. host = "0.0.0.0"
  27. }
  28. port, ok := util.GetAnnotationValue(service.Groups[0].Annotations, "server", "port")
  29. if !ok {
  30. port = strconv.Itoa(defaultPort)
  31. }
  32. return genFile(fileGenConfig{
  33. dir: dir,
  34. subdir: etcDir,
  35. filename: fmt.Sprintf("%s.yaml", filename),
  36. templateName: "etcTemplate",
  37. category: category,
  38. templateFile: etcTemplateFile,
  39. builtinTemplate: etcTemplate,
  40. data: map[string]string{
  41. "serviceName": service.Name,
  42. "host": host,
  43. "port": port,
  44. },
  45. })
  46. }