-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Trendyol/feat/fiber-routes
Feat/fiber routes
- Loading branch information
Showing
7 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters