Documentation
¶
Index ¶
- func CORSMiddleware() gin.HandlerFunc
- func ContextFromGin(c *gin.Context) context.Context
- func GetRequestID(c *gin.Context) string
- func GetTraceID(c *gin.Context) string
- func RequestIDMiddleware() gin.HandlerFunc
- func TraceIDMiddleware() gin.HandlerFunc
- type Config
- type Server
- func (s *Server) Addr() string
- func (s *Server) Any(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) DELETE(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) Engine() *gin.Engine
- func (s *Server) GET(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup
- func (s *Server) HEAD(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) IsRunning() bool
- func (s *Server) OPTIONS(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) PATCH(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) POST(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) PUT(relativePath string, handlers ...gin.HandlerFunc)
- func (s *Server) RegisterRoutes(routes func(r *gin.Engine))
- func (s *Server) Run() error
- func (s *Server) RunTLS(certFile, keyFile string) error
- func (s *Server) RunWithGracefulShutdown() error
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) Start() error
- func (s *Server) Use(middleware ...gin.HandlerFunc)
- func (s *Server) WaitForShutdown() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextFromGin ¶
ContextFromGin 从 Gin Context 提取 request context 这个 context 包含了 trace_id 和 request_id,可以用于创建 logger 示例用法:
ctx := httpserver.ContextFromGin(c)
logger := logger.FromContext(ctx)
logger.Info("处理用户请求") // 自动包含 trace_id 和 request_id
func RequestIDMiddleware ¶
func RequestIDMiddleware() gin.HandlerFunc
RequestIDMiddleware 添加 Request ID 的中间件(每个请求唯一)
func TraceIDMiddleware ¶
func TraceIDMiddleware() gin.HandlerFunc
TraceIDMiddleware 添加 Trace ID 的中间件
Types ¶
type Config ¶
type Config struct {
Host string
Port int
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
ShutdownTimeout time.Duration
}
Config 服务器配置
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server HTTP服务器 - 最小化封装
func (*Server) Any ¶
func (s *Server) Any(relativePath string, handlers ...gin.HandlerFunc)
Any 注册所有HTTP方法的便利方法
func (*Server) DELETE ¶
func (s *Server) DELETE(relativePath string, handlers ...gin.HandlerFunc)
DELETE 注册DELETE路由的便利方法
func (*Server) GET ¶
func (s *Server) GET(relativePath string, handlers ...gin.HandlerFunc)
GET 注册GET路由的便利方法
func (*Server) Group ¶
func (s *Server) Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup
Group 创建路由组的便利方法
func (*Server) HEAD ¶
func (s *Server) HEAD(relativePath string, handlers ...gin.HandlerFunc)
HEAD 注册HEAD路由的便利方法
func (*Server) OPTIONS ¶
func (s *Server) OPTIONS(relativePath string, handlers ...gin.HandlerFunc)
OPTIONS 注册OPTIONS路由的便利方法
func (*Server) PATCH ¶
func (s *Server) PATCH(relativePath string, handlers ...gin.HandlerFunc)
PATCH 注册PATCH路由的便利方法
func (*Server) POST ¶
func (s *Server) POST(relativePath string, handlers ...gin.HandlerFunc)
POST 注册POST路由的便利方法
func (*Server) PUT ¶
func (s *Server) PUT(relativePath string, handlers ...gin.HandlerFunc)
PUT 注册PUT路由的便利方法
func (*Server) RegisterRoutes ¶
RegisterRoutes 使用回调函数注册路由(推荐方式)
func (*Server) RunWithGracefulShutdown ¶
RunWithGracefulShutdown 启动服务器并自动处理优雅关闭(阻塞)
func (*Server) WaitForShutdown ¶
WaitForShutdown 等待关闭信号并执行优雅关闭
Click to show internal directories.
Click to hide internal directories.