httpserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware() gin.HandlerFunc

CORSMiddleware CORS 中间件

func ContextFromGin

func ContextFromGin(c *gin.Context) context.Context

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 GetRequestID

func GetRequestID(c *gin.Context) string

GetRequestID 从 context 中获取 request id

func GetTraceID

func GetTraceID(c *gin.Context) string

GetTraceID 从 context 中获取 trace 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 服务器配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type Server

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

Server HTTP服务器 - 最小化封装

func NewServer

func NewServer(config *Config) *Server

NewServer 创建新的HTTP服务器

func (*Server) Addr

func (s *Server) Addr() string

Addr 返回服务器地址

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) Engine

func (s *Server) Engine() *gin.Engine

Engine 返回Gin引擎,用户完全控制

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) IsRunning

func (s *Server) IsRunning() bool

IsRunning 检查服务器是否正在运行

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

func (s *Server) RegisterRoutes(routes func(r *gin.Engine))

RegisterRoutes 使用回调函数注册路由(推荐方式)

func (*Server) Run

func (s *Server) Run() error

Run 启动服务器(阻塞)

func (*Server) RunTLS

func (s *Server) RunTLS(certFile, keyFile string) error

RunTLS 启动HTTPS服务器(阻塞)

func (*Server) RunWithGracefulShutdown

func (s *Server) RunWithGracefulShutdown() error

RunWithGracefulShutdown 启动服务器并自动处理优雅关闭(阻塞)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown 优雅关闭服务器

func (*Server) Start

func (s *Server) Start() error

Start 启动服务器(非阻塞)

func (*Server) Use

func (s *Server) Use(middleware ...gin.HandlerFunc)

Use 添加中间件的便利方法

func (*Server) WaitForShutdown

func (s *Server) WaitForShutdown() error

WaitForShutdown 等待关闭信号并执行优雅关闭

Jump to

Keyboard shortcuts

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