170 lines
2.6 KiB
Markdown
170 lines
2.6 KiB
Markdown
# Documentation
|
|
|
|
## Core Types
|
|
|
|
### Router
|
|
|
|
Main router that implements fasthttp handler.
|
|
|
|
```go
|
|
router := router.New()
|
|
```
|
|
|
|
### Handler
|
|
|
|
Request handler function type.
|
|
|
|
```go
|
|
type Handler func(ctx Ctx, params []string)
|
|
```
|
|
|
|
### Middleware
|
|
|
|
Function type for middleware.
|
|
|
|
```go
|
|
type Middleware func(Handler) Handler
|
|
```
|
|
|
|
### Group
|
|
|
|
Route group with a prefix.
|
|
|
|
```go
|
|
group := router.Group("/api")
|
|
```
|
|
|
|
## Router Methods
|
|
|
|
### New()
|
|
|
|
Creates a new router.
|
|
|
|
```go
|
|
router := router.New()
|
|
```
|
|
|
|
### ServeHTTP(ctx)
|
|
|
|
Handler for fasthttp requests.
|
|
|
|
### Handler()
|
|
|
|
Returns a fasthttp.RequestHandler for use with fasthttp.ListenAndServe.
|
|
|
|
```go
|
|
fasthttp.ListenAndServe(":8080", router.Handler())
|
|
```
|
|
|
|
### Use(mw ...Middleware)
|
|
|
|
Adds global middleware.
|
|
|
|
```go
|
|
router.Use(loggingMiddleware, authMiddleware)
|
|
```
|
|
|
|
### Handle(method, path, handler)
|
|
|
|
Registers a handler for the given method and path.
|
|
|
|
```go
|
|
router.Handle("GET", "/users", listUsersHandler)
|
|
```
|
|
|
|
### HTTP Method Shortcuts
|
|
|
|
```go
|
|
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.
|
|
|
|
```go
|
|
api := router.Group("/api")
|
|
```
|
|
|
|
### WithMiddleware(mw ...Middleware)
|
|
|
|
Applies middleware to the next route registration.
|
|
|
|
```go
|
|
router.WithMiddleware(authMiddleware).Get("/admin", adminHandler)
|
|
```
|
|
|
|
### StandardHandler(handler)
|
|
|
|
Adapts a standard fasthttp.RequestHandler to the router's Handler type.
|
|
|
|
```go
|
|
router.Get("/legacy", router.StandardHandler(legacyHandler))
|
|
```
|
|
|
|
## Group Methods
|
|
|
|
### Use(mw ...Middleware)
|
|
|
|
Adds middleware to the group.
|
|
|
|
```go
|
|
api.Use(apiKeyMiddleware)
|
|
```
|
|
|
|
### Group(prefix)
|
|
|
|
Creates a nested group.
|
|
|
|
```go
|
|
v1 := api.Group("/v1")
|
|
```
|
|
|
|
### HTTP Method Shortcuts
|
|
|
|
```go
|
|
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.
|
|
|
|
```go
|
|
api.WithMiddleware(authMiddleware).Get("/admin", adminHandler)
|
|
```
|
|
|
|
## Path Parameters
|
|
|
|
Dynamic segments in paths are defined using square brackets.
|
|
|
|
```go
|
|
router.Get("/users/[id]", func(ctx router.Ctx, params []string) {
|
|
id := params[0]
|
|
// ...
|
|
})
|
|
```
|
|
|
|
## Wildcards
|
|
|
|
Wildcard segments capture all remaining path segments.
|
|
|
|
```go
|
|
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
|