From 1bbe30a78ee4d3f5c6e15baba17719f99afa3e07 Mon Sep 17 00:00:00 2001 From: ispiroglu Date: Fri, 20 Sep 2024 23:05:02 +0300 Subject: [PATCH] refactor: make logging on clients accessible from config --- .../client-server-with-otel/client/client.go | 2 +- example/client-server/client/client.go | 2 +- example/client-server/client/main.go | 3 +++ modules/client/driver.go | 19 +++++++++++++++++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/example/client-server-with-otel/client/client.go b/example/client-server-with-otel/client/client.go index 3cda1ee..dcc2fb4 100644 --- a/example/client-server-with-otel/client/client.go +++ b/example/client-server-with-otel/client/client.go @@ -14,7 +14,7 @@ type exampleClient struct { func newClient(f *client.Factory) *exampleClient { return &exampleClient{ - Base: f.Get("example-client", client.DefaultErrDecoder), + Base: f.Get("example-client"), } } diff --git a/example/client-server/client/client.go b/example/client-server/client/client.go index 3cda1ee..dcc2fb4 100644 --- a/example/client-server/client/client.go +++ b/example/client-server/client/client.go @@ -14,7 +14,7 @@ type exampleClient struct { func newClient(f *client.Factory) *exampleClient { return &exampleClient{ - Base: f.Get("example-client", client.DefaultErrDecoder), + Base: f.Get("example-client"), } } diff --git a/example/client-server/client/main.go b/example/client-server/client/main.go index a124d2b..c879e02 100644 --- a/example/client-server/client/main.go +++ b/example/client-server/client/main.go @@ -9,6 +9,7 @@ import ( "github.com/Trendyol/chaki/modules/server" "github.com/Trendyol/chaki/modules/server/controller" "github.com/Trendyol/chaki/modules/server/route" + "github.com/Trendyol/chaki/modules/swagger" ) func main() { @@ -17,6 +18,8 @@ func main() { app.Use( client.Module(), server.Module(), + + swagger.Module(), ) app.Provide( diff --git a/modules/client/driver.go b/modules/client/driver.go index 5dd5d53..84dd113 100644 --- a/modules/client/driver.go +++ b/modules/client/driver.go @@ -15,9 +15,16 @@ type driverBuilder struct { } func newDriverBuilder(cfg *config.Config) *driverBuilder { + setDefaults(cfg) + + d := resty.New(). + SetBaseURL(cfg.GetString("baseurl")). + SetTimeout(cfg.GetDuration("timeout")). + // Debug mode provides a logging, but it's not in the same format with our logger. + SetDebug(cfg.GetBool("debug")) return &driverBuilder{ cfg: cfg, - d: resty.New().SetBaseURL(cfg.GetString("baseurl")), + d: d, } } @@ -32,7 +39,9 @@ func (b *driverBuilder) AddUpdaters(wrappers ...DriverWrapper) *driverBuilder { } func (b *driverBuilder) build() *resty.Client { - b.useLogging() + if b.cfg.GetBool("logging") { + b.useLogging() + } for _, upd := range b.updaters { b.d = upd(b.d) @@ -68,3 +77,9 @@ func (b *driverBuilder) useLogging() { return nil }) } + +func setDefaults(cfg *config.Config) { + cfg.SetDefault("timeout", "5s") + cfg.SetDefault("debug", false) + cfg.SetDefault("logging", false) +}