config.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package rpc
  2. import (
  3. "go-common/app/infra/config/conf"
  4. "go-common/app/infra/config/service/v1"
  5. "go-common/app/infra/config/service/v2"
  6. "go-common/library/net/rpc"
  7. "go-common/library/net/rpc/context"
  8. "go-common/app/infra/config/model"
  9. )
  10. // RPC export rpc service
  11. type RPC struct {
  12. s *v1.Service
  13. s2 *v2.Service
  14. }
  15. // New new rpc server.
  16. func New(c *conf.Config, s *v1.Service, s2 *v2.Service) (svr *rpc.Server) {
  17. r := &RPC{s: s, s2: s2}
  18. svr = rpc.NewServer(c.RPCServer)
  19. if err := svr.Register(r); err != nil {
  20. panic(err)
  21. }
  22. return
  23. }
  24. // Ping check connection success.
  25. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  26. return
  27. }
  28. // Push push new config change to config-service
  29. func (r *RPC) Push(c context.Context, a *model.ArgConf, res *struct{}) (err error) {
  30. service := &model.Service{Name: a.App, BuildVersion: a.BuildVer, Version: a.Ver, Env: a.Env}
  31. err = r.s.Push(c, service)
  32. return
  33. }
  34. //SetToken update Token
  35. func (r *RPC) SetToken(c context.Context, a *model.ArgToken, res *struct{}) (err error) {
  36. r.s.SetToken(c, a.App, a.Env, a.Token)
  37. return
  38. }
  39. // PushV4 push new config change to config-service
  40. func (r *RPC) PushV4(c context.Context, a *model.ArgConf, res *struct{}) (err error) {
  41. service := &model.Service{Name: a.App, BuildVersion: a.BuildVer, Version: a.Ver}
  42. err = r.s2.Push(c, service)
  43. return
  44. }
  45. //SetTokenV4 update Token
  46. func (r *RPC) SetTokenV4(c context.Context, a *model.ArgToken, res *struct{}) (err error) {
  47. r.s2.SetToken(a.App, a.Token)
  48. return
  49. }
  50. //Hosts get host list.
  51. func (r *RPC) Hosts(c context.Context, svr string, res *[]*model.Host) (err error) {
  52. *res, err = r.s2.Hosts(c, svr)
  53. return
  54. }
  55. //ClearHost clear host.
  56. func (r *RPC) ClearHost(c context.Context, svr string, res *struct{}) error {
  57. return r.s2.ClearHost(c, svr)
  58. }
  59. // Force push new host config change to config-service
  60. func (r *RPC) Force(c context.Context, a *model.ArgConf, res *struct{}) (err error) {
  61. service := &model.Service{Name: a.App, BuildVersion: a.BuildVer, Version: a.Ver}
  62. err = r.s2.Force(c, service, a.Hosts, a.SType)
  63. return
  64. }