diff --git a/example/server-with-newrelic/main.go b/example/server-with-newrelic/main.go index ca04375..d7d4264 100644 --- a/example/server-with-newrelic/main.go +++ b/example/server-with-newrelic/main.go @@ -16,7 +16,6 @@ import ( otelserver "github.com/Trendyol/chaki/modules/otel/server" "github.com/Trendyol/chaki/modules/server" "github.com/Trendyol/chaki/modules/server/controller" - "github.com/Trendyol/chaki/modules/server/middlewares" "github.com/Trendyol/chaki/modules/server/route" "github.com/Trendyol/chaki/modules/swagger" ) @@ -43,8 +42,6 @@ func main() { ) app.Provide( - middlewares.ErrHandler, - newFooRepository, newHelloController, ) diff --git a/example/server-with-otel/main.go b/example/server-with-otel/main.go index 491633f..389d7d3 100644 --- a/example/server-with-otel/main.go +++ b/example/server-with-otel/main.go @@ -9,7 +9,6 @@ import ( otelserver "github.com/Trendyol/chaki/modules/otel/server" "github.com/Trendyol/chaki/modules/server" "github.com/Trendyol/chaki/modules/server/controller" - "github.com/Trendyol/chaki/modules/server/middlewares" "github.com/Trendyol/chaki/modules/server/route" ) @@ -27,7 +26,6 @@ func main() { ) app.Provide( - middlewares.ErrHandler, newHelloController, ) diff --git a/example/server-with-postgres/main.go b/example/server-with-postgres/main.go index 7eea5b4..154a9d0 100644 --- a/example/server-with-postgres/main.go +++ b/example/server-with-postgres/main.go @@ -11,7 +11,6 @@ import ( otelserver "github.com/Trendyol/chaki/modules/otel/server" "github.com/Trendyol/chaki/modules/server" "github.com/Trendyol/chaki/modules/server/controller" - "github.com/Trendyol/chaki/modules/server/middlewares" "github.com/Trendyol/chaki/modules/server/route" "github.com/Trendyol/chaki/modules/swagger" ) @@ -32,8 +31,6 @@ func main() { ) app.Provide( - middlewares.ErrHandler, - newFooRepository, newHelloController, ) diff --git a/example/server/main.go b/example/server/main.go index 51595fb..124662a 100644 --- a/example/server/main.go +++ b/example/server/main.go @@ -3,7 +3,6 @@ package main import ( "github.com/Trendyol/chaki" "github.com/Trendyol/chaki/modules/server" - "github.com/Trendyol/chaki/modules/server/middlewares" "github.com/Trendyol/chaki/modules/swagger" ) @@ -16,8 +15,6 @@ func main() { ) app.Provide( - middlewares.ErrHandler, - // Controller NewHelloController, diff --git a/modules/server/module.go b/modules/server/module.go index 5a11afe..576b5bf 100644 --- a/modules/server/module.go +++ b/modules/server/module.go @@ -1,6 +1,7 @@ package server import ( + "github.com/Trendyol/chaki" "github.com/Trendyol/chaki/as" "github.com/Trendyol/chaki/module" "github.com/Trendyol/chaki/modules/server/common" @@ -19,10 +20,15 @@ var ( asMiddlewareGroup = as.Struct[common.MiddlewareGroup]("middlewaregroups") ) -func Module() *module.Module { +func Module(option ...Option) *module.Module { + opts := buildOptions(option...) + m := module.New("server") m.Provide( + // provide options + chaki.Valuer(opts), + // create registries asController.Handler(parseControllers), diff --git a/modules/server/option.go b/modules/server/option.go new file mode 100644 index 0000000..829af16 --- /dev/null +++ b/modules/server/option.go @@ -0,0 +1,38 @@ +package server + +import ( + "github.com/Trendyol/chaki/modules/server/middlewares" + "github.com/gofiber/fiber/v2" +) + +type options struct { + errHandler fiber.ErrorHandler +} + +type Option interface { + Apply(*options) +} + +type withFunc func(*options) + +func (wf withFunc) Apply(opts *options) { + wf(opts) +} + +func WithErrorHandler(errHandler fiber.ErrorHandler) Option { + return withFunc(func(o *options) { + o.errHandler = errHandler + }) +} + +func buildOptions(option ...Option) *options { + opts := &options{ + errHandler: middlewares.ErrHandler(), + } + + for _, o := range option { + o.Apply(opts) + } + + return opts +} diff --git a/modules/server/server.go b/modules/server/server.go index dd2560c..cd2b9d2 100644 --- a/modules/server/server.go +++ b/modules/server/server.go @@ -81,7 +81,7 @@ func defaultFiber( mws []fiber.Handler, wrappers []common.FiberAppWrapper, groups []common.MiddlewareGroup, - eh fiber.ErrorHandler, + opts *options, ) *fiber.App { setDefaultFiberConfigs(cfg) serverCfg := cfg.Of("server") @@ -91,7 +91,7 @@ func defaultFiber( ReadBufferSize: serverCfg.GetInt("readbuffersize"), ReadTimeout: serverCfg.GetDuration("readtimeout"), WriteTimeout: serverCfg.GetDuration("writetimeout"), - ErrorHandler: eh, + ErrorHandler: opts.errHandler, }) if serverCfg.Exists("cors") {