Skip to content

Commit

Permalink
feat(fiber-route): Add fiber route
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetcanozcan committed Sep 11, 2024
1 parent c7c956b commit 57020b3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 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),
}
}
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)
}

0 comments on commit 57020b3

Please sign in to comment.