Documentation
¶
Overview ¶
Package router is used to create a gin router and add automatic CRUD routes for your models.
You can call the NewRouter() function to create a gin router with crud preferred middlewares.
Call Crud() function to add a group of CRUD routes for your models.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Crud ¶
Crud add a group of CRUD routes for model T to the base router on relativePath. For example, if the base router handles route to
/base
then the CRUD routes will be:
GET|POST|PUT|DELETE /base/relativePath
where relativePath is recommended to be the plural form of the model name.
Example:
r := gin.Default() Crud[User](r, "/users")
adds the following routes:
GET /users/ GET /users/:UserId POST /users/ PUT /users/:UserId DELETE /users/:UserId
and with options parameters, it's optional to add the following routes:
- GetNested() => GET /users/:UserId/friends
- CreateNested() => POST /users/:UserId/friends
- DeleteNested() => DELETE /users/:UserId/friends/:FriendId
func NewRouter ¶
func NewRouter(options ...RouterOption) *gin.Engine
NewRouter creates a new router (a gin.New() router) with gin.Recovery() middleware, the log.Logger4Gin middleware, the gin_request_id.RequestID() middleware, and addon middlewares indicated by the options parameters.
Types ¶
type CrudOption ¶
type CrudOption func(group *gin.RouterGroup) *gin.RouterGroup
CrudOption is options to construct the router group.
By adding GetNested, CreateNested, DeleteNested to Crud, you can add CRUD routes for a nested model (Parent.Child).
Or use CrudNested to add all three options above.
func CreateNested ¶
CreateNested add a POST route to the group for creating a nested model:
POST /:parentIdParam/field
func CrudNested ¶
CrudNested = GetNested + CreateNested + DeleteNested
func DeleteNested ¶
DeleteNested add a DELETE route to the group for deleting a nested model:
DELETE /:parentIdParam/field/:childIdParam
type RouterOption ¶
RouterOption is an option to construct the router.
func AllowAllCors ¶
func AllowAllCors() RouterOption
AllowAllCors allows all origins, methods and headers. It's useful for dev. And it is not recommended for the production.
func WithMiddleware ¶
func WithMiddleware(middleware ...gin.HandlerFunc) RouterOption
WithMiddleware adds custom middlewares to the router.
func WithRequestID ¶
func WithRequestID() RouterOption
WithRequestID adds the gin_request_id.RequestID() middleware, which adds a request_id in the context for each request. And the request_id will be writen to the X-Request-Id response header.