gin

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestIDKey = "request_id"
	RawDataKey   = "_yuan_RawData"
)

Variables

This section is empty.

Functions

func Cors added in v0.0.2

func Cors() gin.HandlerFunc

Cors 返回标准 CORS 中间件。

func GetGinContext added in v0.1.3

func GetGinContext(ctx context.Context) *gin.Context

GetGinContext 从 context 中提取 *gin.Context。

func GetLog added in v0.1.3

func GetLog(ctx context.Context) *log.SugaredLogger

GetLog 从 context 中获取或创建请求级别的 logger。

func GetRawData added in v0.1.3

func GetRawData(ctx context.Context) []byte

GetRawData 从 context 中读取并缓存原始请求 body。

func GetRequestID added in v0.1.3

func GetRequestID(ctx context.Context) string

GetRequestID 从 context 中获取或生成 request_id。

func NewIPLimiter added in v0.0.2

func NewIPLimiter(r limiter.Limit, b int) func(next Handler) Handler

NewIPLimiter 创建基于客户端 IP 的令牌桶限流 Hook。

func NewLog added in v0.0.2

func NewLog(appName string, showParams bool) func(next Handler) Handler

NewLog 创建请求日志 Hook,记录请求信息、参数、响应及耗时。

func RegisterController added in v0.0.2

func RegisterController(router IRoutes, ctls []any, hooks ...Hook)

RegisterController 扫描 ctls 的导出方法并注册为 GET/POST 路由。 方法签名要求: func(context.Context) (any, error)

func RenderJSON added in v0.1.3

func RenderJSON(ctx context.Context, data *Return)

RenderJSON 将 Return 响应信封写入 context 中关联的 gin.Context。

func SetGinContext added in v0.1.3

func SetGinContext(ctx context.Context, c *gin.Context) context.Context

SetGinContext 将 *gin.Context 存入 context 并返回新的 context。

Types

type Code

type Code interface {
	Code() int64
}

type Engine

type Engine = gin.Engine

type Gin

type Gin struct {
	// contains filtered or unexported fields
}

func NewGinServer

func NewGinServer() *Gin

func (*Gin) AdaptHandler added in v0.1.5

func (s *Gin) AdaptHandler(h Handler, hooks ...Hook) gin.HandlerFunc

AdaptHandler 将 Handler 适配为 gin.HandlerFunc,自动合并全局 hooks 与调用时传入的 hooks。

hooks 逆序包装,全局 hooks(s.hooks)在外层,调用时 hooks 在内层,与 RegisterController 行为一致。

func (*Gin) AddHook added in v0.0.2

func (s *Gin) AddHook(hooks ...Hook)

func (*Gin) GetEngine added in v0.0.2

func (s *Gin) GetEngine() *Engine

func (*Gin) Host

func (s *Gin) Host(host string) *Gin

func (*Gin) RegisterController added in v0.0.2

func (s *Gin) RegisterController(relativePath string, ctls []any, hooks ...Hook)

func (*Gin) Restart added in v0.0.2

func (s *Gin) Restart(ctx context.Context) error

func (*Gin) Start

func (s *Gin) Start(ctx context.Context) error

func (*Gin) Stop

func (s *Gin) Stop(ctx context.Context) error

type H added in v0.0.2

type H = gin.H

type Handler

type Handler func(context.Context) (any, error)

Handler 是业务处理函数的统一签名,接收 context.Context。 通过 GetGinContext(ctx) 获取原生 *gin.Context 进行参数绑定等操作。

type HandlerFunc

type HandlerFunc = gin.HandlerFunc

type Hook added in v0.0.2

type Hook func(Handler) Handler

type IRoutes added in v0.0.2

type IRoutes = gin.IRoutes

type Return

type Return struct {
	Ret       int64  `json:"ret"`
	Msg       string `json:"msg"`
	Data      any    `json:"data,omitempty"`
	RequestID string `json:"request_id,omitempty"`
	Timestamp int64  `json:"timestamp,omitempty"`
}

Directories

Path Synopsis
Package proto 提供 protoc-gen-yuan 生成的代码所需的运行时适配函数。
Package proto 提供 protoc-gen-yuan 生成的代码所需的运行时适配函数。

Jump to

Keyboard shortcuts

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