2.6 KiB
2.6 KiB
Documentation
Core Types
Router
Main router that implements fasthttp handler.
router := router.New()
Handler
Request handler function type.
type Handler func(ctx Ctx, params []string)
Middleware
Function type for middleware.
type Middleware func(Handler) Handler
Group
Route group with a prefix.
group := router.Group("/api")
Router Methods
New()
Creates a new router.
router := router.New()
ServeHTTP(ctx)
Handler for fasthttp requests.
Handler()
Returns a fasthttp.RequestHandler for use with fasthttp.ListenAndServe.
fasthttp.ListenAndServe(":8080", router.Handler())
Use(mw ...Middleware)
Adds global middleware.
router.Use(loggingMiddleware, authMiddleware)
Handle(method, path, handler)
Registers a handler for the given method and path.
router.Handle("GET", "/users", listUsersHandler)
HTTP Method Shortcuts
router.Get("/users", listUsersHandler)
router.Post("/users", createUserHandler)
router.Put("/users/[id]", updateUserHandler)
router.Patch("/users/[id]", patchUserHandler)
router.Delete("/users/[id]", deleteUserHandler)
Group(prefix)
Creates a route group with prefix.
api := router.Group("/api")
WithMiddleware(mw ...Middleware)
Applies middleware to the next route registration.
router.WithMiddleware(authMiddleware).Get("/admin", adminHandler)
StandardHandler(handler)
Adapts a standard fasthttp.RequestHandler to the router's Handler type.
router.Get("/legacy", router.StandardHandler(legacyHandler))
Group Methods
Use(mw ...Middleware)
Adds middleware to the group.
api.Use(apiKeyMiddleware)
Group(prefix)
Creates a nested group.
v1 := api.Group("/v1")
HTTP Method Shortcuts
api.Get("/users", listUsersHandler)
api.Post("/users", createUserHandler)
api.Put("/users/[id]", updateUserHandler)
api.Patch("/users/[id]", patchUserHandler)
api.Delete("/users/[id]", deleteUserHandler)
WithMiddleware(mw ...Middleware)
Applies middleware to the next route registration in this group.
api.WithMiddleware(authMiddleware).Get("/admin", adminHandler)
Path Parameters
Dynamic segments in paths are defined using square brackets.
router.Get("/users/[id]", func(ctx router.Ctx, params []string) {
id := params[0]
// ...
})
Wildcards
Wildcard segments capture all remaining path segments.
router.Get("/files/*path", func(ctx router.Ctx, params []string) {
path := params[0]
// ...
})
Notes:
- Wildcards must be the last segment in a path
- Only one wildcard is allowed per path