diff --git a/aws/configAws/models.go b/aws/configAws/models.go index 57c2fdbd..26c8ab86 100644 --- a/aws/configAws/models.go +++ b/aws/configAws/models.go @@ -31,7 +31,7 @@ import ( "net" "net/url" - moncfg "github.com/nabbar/golib/monitor/types" + libreq "github.com/nabbar/golib/request" sdkaws "github.com/aws/aws-sdk-go-v2/aws" libval "github.com/go-playground/validator/v10" @@ -47,9 +47,9 @@ type Model struct { } type ModelStatus struct { - Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"` - HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"` - Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"` + Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"` + HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"` + Monitor libreq.OptionsHealth `json:"health" yaml:"health" toml:"health" mapstructure:"health" validate:"required,dive"` } type awsModel struct { diff --git a/aws/configCustom/models.go b/aws/configCustom/models.go index 0036bd3d..1be0b6a9 100644 --- a/aws/configCustom/models.go +++ b/aws/configCustom/models.go @@ -32,7 +32,7 @@ import ( "net/url" "strings" - moncfg "github.com/nabbar/golib/monitor/types" + libreq "github.com/nabbar/golib/request" sdkaws "github.com/aws/aws-sdk-go-v2/aws" libval "github.com/go-playground/validator/v10" @@ -50,9 +50,9 @@ type Model struct { } type ModelStatus struct { - Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"` - HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"` - Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"` + Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"` + HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"` + Monitor libreq.OptionsHealth `json:"health" yaml:"health" toml:"health" mapstructure:"health" validate:"required,dive"` } type awsModel struct { diff --git a/config/components/aws/client.go b/config/components/aws/client.go index 48e58860..9451c954 100644 --- a/config/components/aws/client.go +++ b/config/components/aws/client.go @@ -29,6 +29,9 @@ package aws import ( "net/http" + libhtc "github.com/nabbar/golib/httpcli" + libreq "github.com/nabbar/golib/request" + libaws "github.com/nabbar/golib/aws" libtls "github.com/nabbar/golib/certificates" cfgtps "github.com/nabbar/golib/config/types" @@ -172,36 +175,78 @@ func (o *componentAws) _runFct(fct func(cpt cfgtps.Component) liberr.Error) libe } func (o *componentAws) _runCli() liberr.Error { - var prt = ErrorComponentReload + var ( + e error + + err liberr.Error + cli libaws.AWS + cfg libaws.Config + mon *libreq.OptionsHealth + htc *libhtc.Options + opt *libreq.Options + req libreq.Request + prt = ErrorComponentReload + ) if !o.IsStarted() { prt = ErrorComponentStart } - if cfg, mon, htc, err := o._getConfig(); err != nil { + if cfg, mon, htc, err = o._getConfig(); err != nil { return prt.Error(err) - } else if cli, er := libaws.New(o.x.GetContext(), cfg, o._getHttpClient()); er != nil { - return prt.Error(er) - } else { + } else if cli, err = libaws.New(o.x.GetContext(), cfg, o._getHttpClient()); err != nil { + return prt.Error(err) + } + + if htc != nil { + o.RegisterHTTPClient(func() *http.Client { + if c, e := htc.GetClient(o._getTLS(), ""); e == nil { + return c + } + + return &http.Client{} + }) + } + + if mon != nil && mon.Enable { + opt = &libreq.Options{ + Endpoint: "", + HttpClient: *htc, + Auth: libreq.OptionsAuth{}, + Health: *mon, + } + + opt.SetDefaultTLS(o._getTLS) + opt.SetDefaultLog(o.getLogger) + o.m.Lock() - o.a = cli + req = o.r o.m.Unlock() - if htc != nil { - o.RegisterHTTPClient(func() *http.Client { - if c, e := htc.GetClient(o._getTLS(), ""); e == nil { - return c - } - - return &http.Client{} - }) + if req != nil { + req.RegisterDefaultLogger(o.getLogger) + if req, e = opt.Update(o.x.GetContext, req); e != nil { + return prt.ErrorParent(e) + } + } else if req, e = libreq.New(o.x.GetContext, opt); e != nil { + return prt.ErrorParent(e) } - if e := o._registerMonitor(mon, cfg); e != nil { + o.m.Lock() + o.r = req + o.m.Unlock() + } + + if mon != nil { + if e = o._registerMonitor(mon, cfg); e != nil { return prt.ErrorParent(e) } } + o.m.Lock() + o.a = cli + o.m.Unlock() + return nil } diff --git a/config/components/aws/config.go b/config/components/aws/config.go index adfb1b61..1e1ff09b 100644 --- a/config/components/aws/config.go +++ b/config/components/aws/config.go @@ -32,7 +32,7 @@ import ( cfgcus "github.com/nabbar/golib/aws/configCustom" liberr "github.com/nabbar/golib/errors" libhtc "github.com/nabbar/golib/httpcli" - libmon "github.com/nabbar/golib/monitor/types" + libreq "github.com/nabbar/golib/request" spfcbr "github.com/spf13/cobra" spfvpr "github.com/spf13/viper" ) @@ -111,12 +111,12 @@ func (o *componentAws) RegisterFlag(Command *spfcbr.Command) error { return nil } -func (o *componentAws) _getConfig() (libaws.Config, *libmon.Config, *libhtc.Options, liberr.Error) { +func (o *componentAws) _getConfig() (libaws.Config, *libreq.OptionsHealth, *libhtc.Options, liberr.Error) { var ( key string cfg libaws.Config flg = o._getFlagUpdate() - mon *libmon.Config + mon *libreq.OptionsHealth htc *libhtc.Options vpr *spfvpr.Viper err liberr.Error @@ -194,6 +194,18 @@ func (o *componentAws) _getConfig() (libaws.Config, *libmon.Config, *libhtc.Opti return nil, nil, nil, ErrorConfigInvalid.Error(err) } + if mon != nil { + if err = mon.Validate(); err != nil { + return nil, nil, nil, ErrorConfigInvalid.Error(err) + } + } + + if htc != nil { + if err = htc.Validate(); err != nil { + return nil, nil, nil, ErrorConfigInvalid.Error(err) + } + } + return cfg, mon, htc, nil } diff --git a/config/components/aws/default.go b/config/components/aws/default.go index c80b0da0..5ef06c55 100644 --- a/config/components/aws/default.go +++ b/config/components/aws/default.go @@ -46,7 +46,7 @@ var _defaultConfigStandard = []byte(`{ var _defaultConfigStandardWithStatus = []byte(`{ "config":` + string(DefaultConfigStandard(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `, "http-client":` + string(libhtc.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `, - "monitor":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + ` + "health":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + ` }`) var _defaultConfigCustom = []byte(`{ @@ -60,7 +60,7 @@ var _defaultConfigCustom = []byte(`{ var _defaultConfigCustomWithStatus = []byte(`{ "config":` + string(DefaultConfigCustom(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `, "http-client":` + string(libhtc.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `, - "monitor":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + ` + "health":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + ` }`) var _defaultConfig = _defaultConfigCustom diff --git a/config/components/aws/model.go b/config/components/aws/model.go index cd34a0dc..3568264c 100644 --- a/config/components/aws/model.go +++ b/config/components/aws/model.go @@ -30,6 +30,8 @@ import ( "net/http" "sync" + libreq "github.com/nabbar/golib/request" + libaws "github.com/nabbar/golib/aws" libtls "github.com/nabbar/golib/certificates" libctx "github.com/nabbar/golib/context" @@ -45,6 +47,7 @@ type componentAws struct { c func() *http.Client t libtls.FctTLSDefault a libaws.AWS + r libreq.Request } func (o *componentAws) RegisterHTTPClient(fct func() *http.Client) { diff --git a/config/components/aws/monitor.go b/config/components/aws/monitor.go index 7d4dd442..d76c07d0 100644 --- a/config/components/aws/monitor.go +++ b/config/components/aws/monitor.go @@ -31,6 +31,8 @@ import ( "fmt" "runtime" + libreq "github.com/nabbar/golib/request" + libaws "github.com/nabbar/golib/aws" libmon "github.com/nabbar/golib/monitor" moninf "github.com/nabbar/golib/monitor/info" @@ -61,7 +63,7 @@ func (o *componentAws) _getMonitorPool() montps.Pool { } } -func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) error { +func (o *componentAws) _registerMonitor(opt *libreq.OptionsHealth, aws libaws.Config) error { var ( e error key = o._getKey() @@ -75,7 +77,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e return nil } else if len(key) < 1 { return ErrorComponentNotInitialized.Error(nil) - } else if cfg == nil { + } else if opt == nil { return nil } else if aws == nil { return ErrorConfigInvalid.Error(nil) @@ -89,6 +91,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e res["date"] = vrs.GetDate() res["endpoint"] = aws.GetEndpoint().Host res["region"] = aws.GetRegion() + res["health"] = o._getEndpoint(opt, aws) } if inf, e = moninf.New(defaultNameMonitor); e != nil { @@ -110,7 +113,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e } } - if e = mon.SetConfig(o.x.GetContext, *cfg); e != nil { + if e = mon.SetConfig(o.x.GetContext, opt.Monitor); e != nil { return e } @@ -125,6 +128,17 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e return nil } +func (o *componentAws) _getEndpoint(opt *libreq.OptionsHealth, aws libaws.Config) string { + o.m.RLock() + defer o.m.RUnlock() + + if o.r != nil && len(opt.Endpoint) > 0 { + return opt.Endpoint + } else { + return aws.GetEndpoint().Host + } +} + func (o *componentAws) _newMonitor(inf montps.Info) (montps.Monitor, error) { if c, e := libmon.New(o.x.GetContext, inf); e != nil { return nil, e @@ -172,7 +186,9 @@ func (o *componentAws) HealthCheck(ctx context.Context) error { if !o.IsStarted() { return fmt.Errorf("component not started") - } else { + } else if o.r == nil { return o.a.Config().Check(ctx) + } else { + return o.r.HealthCheck(ctx) } } diff --git a/go.mod b/go.mod index 4b41a0d2..da16a0de 100644 --- a/go.mod +++ b/go.mod @@ -162,7 +162,7 @@ require ( github.com/nats-io/nuid v1.0.1 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/paulmach/orb v0.9.0 // indirect - github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/pelletier/go-toml/v2 v2.0.7 // indirect github.com/pierrec/lz4/v4 v4.1.17 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pkg/term v1.2.0-beta.2 // indirect diff --git a/request/interface.go b/request/interface.go index f44156c6..a1cf8ac4 100644 --- a/request/interface.go +++ b/request/interface.go @@ -117,6 +117,7 @@ type Request interface { DoParse(model interface{}, validStatus ...int) liberr.Error Monitor(ctx context.Context, vrs libver.Version) (montps.Monitor, error) + HealthCheck(ctx context.Context) error } func New(ctx libctx.FuncContext, opt *Options) (Request, error) { diff --git a/request/options.go b/request/options.go index 777d074c..4e9abe1a 100644 --- a/request/options.go +++ b/request/options.go @@ -60,7 +60,7 @@ type OptionsAuth struct { type OptionsHealth struct { Enable bool `json:"enable" yaml:"enable" toml:"enable" mapstructure:"enable"` - Endpoint string `json:"endpoint" yaml:"endpoint" toml:"endpoint" mapstructure:"endpoint" validate:"required,url"` + Endpoint string `json:"endpoint" yaml:"endpoint" toml:"endpoint" mapstructure:"endpoint" validate:"url"` Auth OptionsAuth `json:"auth" yaml:"auth" toml:"auth" mapstructure:"auth" validate:"required,dive"` Result OptionsHealthResult `json:"result" yaml:"result" toml:"result" mapstructure:"result" validate:"required,dive"` Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"` @@ -104,6 +104,27 @@ func (o *Options) Validate() liberr.Error { return e } +func (o *OptionsHealth) Validate() liberr.Error { + var e = ErrorValidatorError.Error(nil) + + if err := libval.New().Struct(o); err != nil { + if er, ok := err.(*libval.InvalidValidationError); ok { + e.AddParent(er) + } + + for _, er := range err.(libval.ValidationErrors) { + //nolint #goerr113 + e.AddParent(fmt.Errorf("config field '%s' is not validated by constraint '%s'", er.Namespace(), er.ActualTag())) + } + } + + if !e.HasParent() { + e = nil + } + + return e +} + func (o *Options) defaultTLS() libtls.TLSConfig { if o.tls != nil { return o.tls()