config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package config
  2. import (
  3. "encoding/json"
  4. "gogs.tyduyong.com/duyong/dy-pkg/app/flagsets"
  5. "gogs.tyduyong.com/duyong/dy-pkg/app/options"
  6. )
  7. type Config struct {
  8. ServerRunOptions *options.ServerRunOptions `json:"server" mapstructure:"server"`
  9. InsecureServeOptions *options.InsecureServeOptions `json:"insecure" mapstructure:"insecure"`
  10. SecureServingOptions *options.SecureServingOptions `json:"secure" mapstructure:"secure"`
  11. PGSQLOptions *options.PGSQLOptions `json:"pgsql" mapstructure:"pgsql"`
  12. LogOptions *options.LogOptions `json:"log" mapstructure:"log"`
  13. JwtOptions *options.JwtOptions `json:"jwt" mapstructure:"jwt"`
  14. FeatureOptions *options.FeatureOptions `json:"feature" mapstructure:"feature"`
  15. GRPCOptions *options.GRPCOptions `json:"grpc" mapstructure:"grpc"`
  16. }
  17. func NewConfig() *Config {
  18. o := Config{
  19. ServerRunOptions: options.NewServerRunOptions(),
  20. InsecureServeOptions: options.NewInsecureServingOptions(),
  21. SecureServingOptions: options.NewSecureServingOptions(),
  22. PGSQLOptions: options.NewPGSQLOptions(),
  23. JwtOptions: options.NewJwtOptions(),
  24. FeatureOptions: options.NewFeatureOptions(),
  25. LogOptions: options.NewLogOptions(),
  26. GRPCOptions: options.NewGRPCOptions(),
  27. }
  28. return &o
  29. }
  30. func (o *Config) Flags() (fss flagsets.NamedFlagSets) {
  31. o.ServerRunOptions.AddFlags(fss.FlagSet("server run"))
  32. o.InsecureServeOptions.AddFlags(fss.FlagSet("insecure server"))
  33. o.PGSQLOptions.AddFlags(fss.FlagSet("mysql"))
  34. o.JwtOptions.AddFlags(fss.FlagSet("jwt"))
  35. o.FeatureOptions.AddFlags(fss.FlagSet("features"))
  36. o.LogOptions.AddFlags(fss.FlagSet("logs"))
  37. return fss
  38. }
  39. func (o *Config) Validate() []error {
  40. var errs []error
  41. errs = append(errs, o.ServerRunOptions.Validate()...)
  42. errs = append(errs, o.InsecureServeOptions.Validate()...)
  43. errs = append(errs, o.PGSQLOptions.Validate()...)
  44. errs = append(errs, o.JwtOptions.Validate()...)
  45. errs = append(errs, o.FeatureOptions.Validate()...)
  46. errs = append(errs, o.LogOptions.Validate()...)
  47. return errs
  48. }
  49. func (o *Config) String() string {
  50. data, _ := json.Marshal(o)
  51. return string(data)
  52. }
  53. // Complete set default Options.
  54. func (o *Config) Complete() error {
  55. return nil
  56. }