router

package
v0.0.0-...-0729139 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 8 Imported by: 0

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

func Crud[T orm.Model](base gin.IRouter, relativePath string, options ...CrudOption) gin.IRouter

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

func CreateNested[P orm.Model, N orm.Model](field string) CrudOption

CreateNested add a POST route to the group for creating a nested model:

POST /:parentIdParam/field

func CrudNested

func CrudNested[P orm.Model, T orm.Model](field string) CrudOption

CrudNested = GetNested + CreateNested + DeleteNested

func DeleteNested

func DeleteNested[P orm.Model, T orm.Model](field string) CrudOption

DeleteNested add a DELETE route to the group for deleting a nested model:

DELETE /:parentIdParam/field/:childIdParam

func GetNested

func GetNested[P orm.Model, N orm.Model](field string) CrudOption

GetNested add a GET route to the group for querying a nested model:

GET /:parentIdParam/field

type RouterOption

type RouterOption func(router gin.IRouter) gin.IRouter

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL