Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fiber routes #35

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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