Documentation
¶
Index ¶
- Variables
- type Driver
- type Middleware
- type Option
- type Server
- func (s *Server) Addr() string
- func (s *Server) DELETE(path string, handler http.HandlerFunc)
- func (s *Server) Endpoint() string
- func (s *Server) GET(path string, handler http.HandlerFunc)
- func (s *Server) HEAD(path string, handler http.HandlerFunc)
- func (s *Server) Handle(method, path string, handler http.HandlerFunc)
- func (s *Server) OPTIONS(path string, handler http.HandlerFunc)
- func (s *Server) PATCH(path string, handler http.HandlerFunc)
- func (s *Server) POST(path string, handler http.HandlerFunc)
- func (s *Server) PUT(path string, handler http.HandlerFunc)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- func (s *Server) Use(middlewares ...Middleware)
Constants ¶
This section is empty.
Variables ¶
var ErrNoDriver = errors.New("http: no driver set, use WithDriver() to specify one (e.g. std.NewDriver(), gin.NewDriver())")
ErrNoDriver 表示未设置 HTTP 服务器驱动。
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver interface {
Handle(method, path string, handler http.HandlerFunc)
Start(ctx context.Context, ln net.Listener) error
Stop(ctx context.Context) error
}
Driver 定义 HTTP 服务器驱动接口,由具体的框架(如 gin、fiber 等)实现。 注意:Start 接收的是由 Server 创建好的 net.Listener,而非原始地址字符串。 这使得 Server 能够获取实际绑定地址(用于 :0 随机端口场景)并统一处理 TLS。
type Middleware ¶
Middleware 定义标准 HTTP 中间件类型。 中间件接收下一个处理器并返回包装后的处理器,可在请求前后执行自定义逻辑。
func Chain ¶
func Chain(middlewares ...Middleware) Middleware
Chain 将多个中间件组合为单个中间件,按入参顺序执行。 常用于构建可复用的中间件组。
type Option ¶
type Option func(*Server)
func WithDriver ¶
func WithMiddleware ¶
func WithMiddleware(middlewares ...Middleware) Option
WithMiddleware 通过选项设置中间件,允许在创建 Server 时直接传入中间件。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) DELETE ¶
func (s *Server) DELETE(path string, handler http.HandlerFunc)
DELETE 注册 DELETE 请求路由。
func (*Server) Endpoint ¶
Endpoint 返回服务器的访问地址。 服务器已启动时返回实际监听地址(用于 :0 随机端口场景), 否则返回配置地址。自动根据 TLS 配置选择 http/https scheme, 并规范 IPv6 地址格式。
func (*Server) GET ¶
func (s *Server) GET(path string, handler http.HandlerFunc)
GET 注册 GET 请求路由,等价于 Handle(http.MethodGet, ...).
func (*Server) HEAD ¶
func (s *Server) HEAD(path string, handler http.HandlerFunc)
HEAD 注册 HEAD 请求路由。
func (*Server) Handle ¶
func (s *Server) Handle(method, path string, handler http.HandlerFunc)
Handle 注册路由处理器,将请求转发到具体的驱动实现。 注册前会应用已添加的中间件链。 若未通过 WithDriver 设置驱动,将 panic。
func (*Server) OPTIONS ¶
func (s *Server) OPTIONS(path string, handler http.HandlerFunc)
OPTIONS 注册 OPTIONS 请求路由。
func (*Server) PATCH ¶
func (s *Server) PATCH(path string, handler http.HandlerFunc)
PATCH 注册 PATCH 请求路由。
func (*Server) POST ¶
func (s *Server) POST(path string, handler http.HandlerFunc)
POST 注册 POST 请求路由。
func (*Server) Start ¶
Start 启动 HTTP 服务器。 Server 自行创建 net.Listener 以获取实际绑定地址,并用 TLS 包装(若已配置)。 若未通过 WithDriver 设置驱动,将返回错误。
func (*Server) Use ¶
func (s *Server) Use(middlewares ...Middleware)
Use 注册全局中间件,对所有通过 Handle 注册的路由生效。 中间件按注册顺序执行:先注册的中间件最先被调用。 必须在 Handle 之前调用。