genetc.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  7. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  10. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  11. )
  12. const etcTemplate = `Name: {{.serviceName}}.rpc
  13. ListenOn: 127.0.0.1:8080
  14. Etcd:
  15. Hosts:
  16. - 127.0.0.1:2379
  17. Key: {{.serviceName}}.rpc
  18. `
  19. // GenEtc generates the yaml configuration file of the rpc service,
  20. // including host, port monitoring configuration items and etcd configuration
  21. func (g *DefaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
  22. dir := ctx.GetEtc()
  23. etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
  24. if err != nil {
  25. return err
  26. }
  27. fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
  28. text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
  29. if err != nil {
  30. return err
  31. }
  32. return util.With("etc").Parse(text).SaveTo(map[string]interface{}{
  33. "serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
  34. }, fileName, false)
  35. }