From 57020b32648404ecc9bf8712e43c7677a6a2f712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmetcan=20=C3=96ZCAN?= Date: Wed, 11 Sep 2024 11:00:19 +0300 Subject: [PATCH] feat(fiber-route): Add fiber route --- example/fiberroute/config.yaml | 0 example/fiberroute/main.go | 56 ++++++++++++++++++++++++++++++++++ modules/server/route/fiber.go | 29 ++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 example/fiberroute/config.yaml create mode 100644 example/fiberroute/main.go create mode 100644 modules/server/route/fiber.go diff --git a/example/fiberroute/config.yaml b/example/fiberroute/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/example/fiberroute/main.go b/example/fiberroute/main.go new file mode 100644 index 0000000..e3c86c8 --- /dev/null +++ b/example/fiberroute/main.go @@ -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), + } +} diff --git a/modules/server/route/fiber.go b/modules/server/route/fiber.go new file mode 100644 index 0000000..f851b3b --- /dev/null +++ b/modules/server/route/fiber.go @@ -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) +}