http

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

type Middleware func(http.Handler) http.Handler

Middleware 定义标准 HTTP 中间件类型。 中间件接收下一个处理器并返回包装后的处理器,可在请求前后执行自定义逻辑。

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain 将多个中间件组合为单个中间件,按入参顺序执行。 常用于构建可复用的中间件组。

type Option

type Option func(*Server)

func WithDriver

func WithDriver(d Driver) Option

func WithMiddleware

func WithMiddleware(middlewares ...Middleware) Option

WithMiddleware 通过选项设置中间件,允许在创建 Server 时直接传入中间件。

func WithTLS

func WithTLS(certFile, keyFile string) Option

WithTLS 从证书和私钥文件加载 TLS 配置,启用 HTTPS。

func WithTLSConfig

func WithTLSConfig(c *tls.Config) Option

WithTLSConfig 设置 TLS 配置,启用 HTTPS。

type Server

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

func NewServer

func NewServer(addr string, opts ...Option) *Server

NewServer 创建一个 HTTP 服务器实例。

func (*Server) Addr

func (s *Server) Addr() string

Addr 返回服务器的监听地址。

func (*Server) DELETE

func (s *Server) DELETE(path string, handler http.HandlerFunc)

DELETE 注册 DELETE 请求路由。

func (*Server) Endpoint

func (s *Server) Endpoint() string

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

func (s *Server) PUT(path string, handler http.HandlerFunc)

PUT 注册 PUT 请求路由。

func (*Server) Start

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

Start 启动 HTTP 服务器。 Server 自行创建 net.Listener 以获取实际绑定地址,并用 TLS 包装(若已配置)。 若未通过 WithDriver 设置驱动,将返回错误。

func (*Server) Stop

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

Stop 停止 HTTP 服务器。

func (*Server) Use

func (s *Server) Use(middlewares ...Middleware)

Use 注册全局中间件,对所有通过 Handle 注册的路由生效。 中间件按注册顺序执行:先注册的中间件最先被调用。 必须在 Handle 之前调用。

Directories

Path Synopsis
binding module
driver
chi module
gin module
std module
middleware
codec module
cors module
crypto module
logging module
ratelimit module
recovery module
requestid module
timeout module
tracing module

Jump to

Keyboard shortcuts

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