router

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InfoLoggerMiddleware

func InfoLoggerMiddleware(log *tlog.Logger) gin.HandlerFunc

请求信息日志中间件

会记录 请求时间、请求的ip、请求的url、请求的方法、状态码、响应时间

func InitRouter

func InitRouter(serverName string) *gin.Engine

初始化路由

会遍历所有注册的API,执行SetApi方法

func Register

func Register(serverName string, name string, version string, router Router)
注册一个http服务下的一个路由

如果重复传入会触发panic

示例:

type Apier struct {
}

func (a Apier) SetApi(router *gin.Engine) {
	create(router)
}

func init() {
	router.Register("service","code","v1", &Apier{})
}

func create(router *gin.Engine) {
	router.GET("/create", func(c *gin.Context) {
		c.String(http.StatusOK, "user")
	})
}

实际url为:code/v1/create

Types

type Response

type Response struct {
	Code    int         `json:"code"`    // 业务状态码
	Message string      `json:"message"` // 消息
	Data    interface{} `json:"data"`    // 请求数据
}

type Router

type Router interface {
	// 设置路由
	SetRouter(router *gin.RouterGroup) []gin.IRoutes
	// 设置中间件
	SetMiddleware() []gin.HandlerFunc
}

路由接口

Jump to

Keyboard shortcuts

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