Skip to content

Commit

Permalink
Merge pull request #35 from Trendyol/feat/fiber-routes
Browse files Browse the repository at this point in the history
Feat/fiber routes
  • Loading branch information
ahmetcanozcan authored Oct 8, 2024
2 parents a3086af + 6571d06 commit 3320894
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 15 deletions.
Empty file added example/fiberroute/config.yaml
Empty file.
56 changes: 56 additions & 0 deletions example/fiberroute/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"

"github.com/Trendyol/chaki"
"github.com/Trendyol/chaki/logger"
"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"
"github.com/gofiber/fiber/v2"
)

func main() {
app := chaki.New()

app.WithOption(
chaki.WithConfigPath("config.yaml"),
)

app.Use(
server.Module(),
swagger.Module(),
)

app.Provide(
newController,
)

logger.Fatal(app.Start())
}

type greetRequest struct {
Name string `param:"name" validate:"required"`
}

type fooController struct {
controller.Controller
}

func newController() controller.Controller {
return &fooController{
Controller: controller.New("foo-controller").SetPrefix("/foo"),
}
}

func (ct *fooController) greet(c *fiber.Ctx) error {
return c.JSON(fmt.Sprintf("Hello %s", c.Params("name")))
}

func (ct *fooController) Routes() []route.Route {
return []route.Route{
route.FiberGet[greetRequest, string]("/:name", ct.greet),
}
}
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
29 changes: 29 additions & 0 deletions modules/server/route/fiber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package route

import (
"github.com/gofiber/fiber/v2"
)

func NewFiber[Req, Res any](method, path string, h fiber.Handler) Route {
return newRoute[Req, Res](method, path, h)
}

func FiberGet[Req, Res any](path string, h fiber.Handler) Route {
return NewFiber[Req, Res](fiber.MethodGet, path, h)
}

func FiberPost[Req, Res any](path string, h fiber.Handler) Route {
return NewFiber[Req, Res](fiber.MethodPost, path, h)
}

func FiberDelete[Req, Res any](path string, h fiber.Handler) Route {
return NewFiber[Req, Res](fiber.MethodDelete, path, h)
}

func FiberPatch[Req, Res any](path string, h fiber.Handler) Route {
return NewFiber[Req, Res](fiber.MethodPatch, path, h)
}

func FiberPut[Req, Res any](path string, h fiber.Handler) Route {
return NewFiber[Req, Res](fiber.MethodPut, path, h)
}
14 changes: 8 additions & 6 deletions modules/server/route/routes.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package route

import "net/http"
import (
"github.com/gofiber/fiber/v2"
)

func Get[Req, Res any](path string, h HandlerFunc[Req, Res], ds ...int) Route {
return New[Req, Res](http.MethodGet, path, h, ds...)
return New(fiber.MethodGet, path, h, ds...)
}

func Post[Req, Res any](path string, h HandlerFunc[Req, Res], ds ...int) Route {
return New[Req, Res](http.MethodPost, path, h, ds...)
return New(fiber.MethodPost, path, h, ds...)
}

func Patch[Req, Res any](path string, h HandlerFunc[Req, Res], ds ...int) Route {
return New[Req, Res](http.MethodPatch, path, h, ds...)
return New(fiber.MethodPatch, path, h, ds...)
}

func Put[Req, Res any](path string, h HandlerFunc[Req, Res], ds ...int) Route {
return New[Req, Res](http.MethodPut, path, h, ds...)
return New(fiber.MethodPut, path, h, ds...)
}

func Delete[Req, Res any](path string, h HandlerFunc[Req, Res], ds ...int) Route {
return New[Req, Res](http.MethodDelete, path, h, ds...)
return New(fiber.MethodDelete, path, h, ds...)
}
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 3320894

Please sign in to comment.