Skip to content

Commit

Permalink
feat(fiber-cfg) Add fiber config wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetcanozcan committed Sep 11, 2024
1 parent 57020b3 commit 6571d06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 3 additions & 2 deletions modules/server/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type (
MiddlewareGroup = []fiber.Handler
FiberAppWrapper = wrapper.Wrapper[*fiber.App]
MiddlewareGroup = []fiber.Handler
FiberAppWrapper = wrapper.Wrapper[*fiber.App]
FiberConfigWrapper = wrapper.Wrapper[fiber.Config]
)
16 changes: 11 additions & 5 deletions modules/server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
)

var (
asController = as.Interface[controller.Controller]("servercontrollers")
asMiddleware = as.Struct[fiber.Handler]("servermiddleware")
asValidationRule = as.Interface[validation.Rule]("validationrules")
asFiberAppWrapper = as.Struct[common.FiberAppWrapper]("fiberappwrappers")
asMiddlewareGroup = as.Struct[common.MiddlewareGroup]("middlewaregroups")
asController = as.Interface[controller.Controller]("servercontrollers")
asMiddleware = as.Struct[fiber.Handler]("servermiddleware")
asValidationRule = as.Interface[validation.Rule]("validationrules")
asFiberAppWrapper = as.Struct[common.FiberAppWrapper]("fiberappwrappers")
asMiddlewareGroup = as.Struct[common.MiddlewareGroup]("middlewaregroups")
asFiberConfigWrapper = as.Struct[common.FiberConfigWrapper]("fiberconfigwrappers")
)

func Module(option ...Option) *module.Module {
Expand All @@ -43,6 +44,7 @@ func Module(option ...Option) *module.Module {

asFiberAppWrapper.Grouper(),
asMiddlewareGroup.Grouper(),
asFiberConfigWrapper.Grouper(),

// server
defaultFiber,
Expand Down Expand Up @@ -75,6 +77,10 @@ func Module(option ...Option) *module.Module {
Match: asMiddlewareGroup.Match,
Wrap: asMiddlewareGroup.Value,
},
module.ProvideHook{
Match: asFiberConfigWrapper.Match,
Wrap: asFiberConfigWrapper.Value,
},
)

return m
Expand Down
11 changes: 9 additions & 2 deletions modules/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,25 @@ func defaultFiber(
mws []fiber.Handler,
wrappers []common.FiberAppWrapper,
groups []common.MiddlewareGroup,
configWrappers []common.FiberConfigWrapper,
opts *options,
) *fiber.App {
setDefaultFiberConfigs(cfg)
serverCfg := cfg.Of("server")

app := fiber.New(fiber.Config{
fibercfg := fiber.Config{
BodyLimit: serverCfg.GetInt("bodylimit"),
ReadBufferSize: serverCfg.GetInt("readbuffersize"),
ReadTimeout: serverCfg.GetDuration("readtimeout"),
WriteTimeout: serverCfg.GetDuration("writetimeout"),
ErrorHandler: opts.errHandler,
})
}

for _, fwrapper := range configWrappers {
fibercfg = fwrapper(fibercfg)
}

app := fiber.New(fibercfg)

if serverCfg.Exists("cors") {
app.Use(middlewares.CORSMiddleware(serverCfg.Of("cors")))
Expand Down

0 comments on commit 6571d06

Please sign in to comment.