genetc.go 1.3 KB

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