server

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const Version = "vs/2.1"

Variables

Functions

This section is empty.

Types

type Config

type Config struct {
	Addr                         string        `json:"addr" yaml:"addr"`                                                       // 监听地址
	Port                         int           `json:"port" yaml:"port"`                                                       //用于 Alt-Svc 响应头中的端口,用于防火墙重定向等场景,允许客户端使用与服务器监听端口不同的端口
	DisableGeneralOptionsHandler bool          `json:"disable_general_options_handler" yaml:"disable_general_options_handler"` // 如果为 true,将 "OPTIONS *" 请求传递给 Handler;否则自动响应 200 O
	ReadTimeout                  time.Duration `json:"read_timeout" yaml:"read_timeout"`                                       // 读取超时,零或负值表示无超时
	ReadHeaderTimeout            time.Duration `json:"read_header_timeout" yaml:"read_header_timeout"`                         // 读取请求头超时,零或负值表示无超时
	WriteTimeout                 time.Duration `json:"write_timeout" yaml:"write_timeout"`                                     // 写入超时,零或负值表示无超时
	IdleTimeout                  time.Duration `json:"idle_timeout" yaml:"idle_timeout"`                                       // 在 keep-alive 启用时等待下一个请求的最大时间,如果为零,使用 ReadTimeout 的值
	MaxHeaderBytes               int           `json:"max_header_bytes" yaml:"max_header_bytes"`                               // 服务器解析请求头时读取的最大字节数(包括请求行),如果为零,使用 DefaultMaxHeaderBytes(1MB)
	ServerName                   []string      `json:"server_name" yaml:"server_name"`                                         // 绑定域名

	TLS tlsconfig.TLSConfig `json:"tls" yaml:"tls"` // TLS 配置

	EnableQuickH3      bool              `json:"enable_quick_h3" yaml:"enable_quick_h3"`         // 启用 QUIC HTTP/3
	EnableDatagrams    bool              `json:"enable_datagrams" yaml:"enable_datagrams"`       // 启用 HTTP/3 数据报支持(RFC 9297
	AdditionalSettings map[uint64]uint64 `json:"additional_settings" yaml:"additional_settings"` // 额外的设置,QUIC 包配置
	QUICConfig         QUICConfig        `json:"quic_config" yaml:"quic_config"`                 // QUIC 配置

	Security    SecurityConfig               `ini:"security" json:"security" yaml:"security"` // 安全配置
	Compression middleware.CompressionConfig `json:"compression" yaml:"compression"`          // 压缩配置
	Logger      zaploger.Config              `json:"logger" yaml:"logger"`                    // 日志配置

	Route route.Config `json:"route" yaml:"route"` // 路由配置
}

type Description

type Description[T any] struct {
	Name     string // 路由名称
	Version  string // 路由版本
	CodeKey  string // 路由编码字段
	Method   string
	Path     string
	Code     string // 路由编码
	Metadata *T     // 自定义描述结构
}

func NewDesc

func NewDesc[T any](key, code string, meta *T) Description[T]

type Group

type Group[T any] struct {
	// contains filtered or unexported fields
}

func (*Group[T]) Any

func (g *Group[T]) Any(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) AnyWithHandler

func (g *Group[T]) AnyWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Connect

func (g *Group[T]) Connect(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) ConnectWithHandler

func (g *Group[T]) ConnectWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Delete

func (g *Group[T]) Delete(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) DeleteWithHandler

func (g *Group[T]) DeleteWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Get

func (g *Group[T]) Get(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) GetWithHandler

func (g *Group[T]) GetWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Group

func (g *Group[T]) Group(groupName string) *Group[T]

Group 创建子分组

func (*Group[T]) Head

func (g *Group[T]) Head(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) HeadWithHandler

func (g *Group[T]) HeadWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Options

func (g *Group[T]) Options(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) OptionsWithHandler

func (g *Group[T]) OptionsWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Patch

func (g *Group[T]) Patch(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) PatchWithHandler

func (g *Group[T]) PatchWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Post

func (g *Group[T]) Post(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) PostWithHandler

func (g *Group[T]) PostWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Put

func (g *Group[T]) Put(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) PutWithHandler

func (g *Group[T]) PutWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Trace

func (g *Group[T]) Trace(p string, handler http.HandlerFunc, descriptions ...Description[T])

func (*Group[T]) TraceWithHandler

func (g *Group[T]) TraceWithHandler(p string, handler http.Handler, descriptions ...Description[T])

func (*Group[T]) Use

func (g *Group[T]) Use(middleware ...Middleware) *Group[T]

func (*Group[T]) WS

func (g *Group[T]) WS(p string, handler func(ws *websocket.Conn))

WS 对于WS类型,由于新版本的http server,不支持混合路由。 所以指定成GET

func (*Group[T]) WSWithDescription

func (g *Group[T]) WSWithDescription(p string, handler func(ws *websocket.Conn), description Description[T])

type IPAccessConfig

type IPAccessConfig struct {
	Enable bool            `ini:"enable" json:"enable" yaml:"enable"`
	Allow  *ipmatch.Config `ini:"allow" json:"allow" yaml:"allow"` // 允许的IP
	Deny   *ipmatch.Config `ini:"deny" json:"deny" yaml:"deny"`    // 屏蔽的IP
	Debug  *ipmatch.Config `ini:"debug" json:"debug" yaml:"debug"` // 调试允许的IP
}

type Middleware

type Middleware func(next http.Handler) http.Handler

Middleware 修改为支持 http.Handler

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain 中间件链(兼容 http.Handler)

type QUICConfig

type QUICConfig struct {
	HandshakeIdleTimeout             time.Duration `json:"handshake_idle_timeout" yaml:"handshake_idle_timeout"`                             // 握手阶段的空闲超时
	MaxIdleTimeout                   time.Duration `json:"max_idle_timeout" yaml:"max_idle_timeout"`                                         // 连接建立后的最大空闲超时
	InitialStreamReceiveWindow       uint64        `json:"initial_stream_receive_window" yaml:"initial_stream_receive_window"`               // 初始流接收窗口大小(默认 512KB)
	MaxStreamReceiveWindow           uint64        `json:"max_stream_receive_window" yaml:"max_stream_receive_window"`                       // 最大流接收窗口大小(默认 6MB)
	InitialConnectionReceiveWindow   uint64        `json:"initial_connection_receive_window" yaml:"initial_connection_receive_window"`       // 初始连接接收窗口大小(默认 512KB)
	MaxConnectionReceiveWindow       uint64        `json:"max_connection_receive_window" yaml:"max_connection_receive_window"`               // 最大连接接收窗口大小(默认 15MB)
	MaxIncomingStreams               int64         `json:"max_incoming_streams" yaml:"max_incoming_streams"`                                 // 允许对端同时打开的双向流最大数量
	MaxIncomingUniStreams            int64         `json:"max_incoming_uni_streams" yaml:"max_incoming_uni_streams"`                         // 允许对端同时打开的单向流最大数量
	KeepAlivePeriod                  time.Duration `json:"keep_alive_period" yaml:"keep_alive_period"`                                       // 发送保活数据包的时间间隔
	InitialPacketSize                uint16        `json:"initial_packet_size" yaml:"initial_packet_size"`                                   // 发送数据包的初始大小(也是下限),通常自动进行 PMTU 发现
	DisablePathMTUDiscovery          bool          `json:"disable_path_mtu_discovery" yaml:"disable_path_mtu_discovery"`                     // 禁用路径 MTU 发现(RFC 8899
	Allow0RTT                        bool          `json:"allow_0_rtt" yaml:"allow_0_rtt"`                                                   // 允许接受 0-RTT 连接尝试
	EnableDatagrams                  bool          `json:"enable_datagrams" yaml:"enable_datagrams"`                                         // 启用 QUIC 数据报支持(RFC 9221
	EnableStreamResetPartialDelivery bool          `json:"enable_stream_reset_partial_delivery" yaml:"enable_stream_reset_partial_delivery"` // 启用带部分交付的流重置
}

type RouteType

type RouteType int
const (
	RouteTypeHTTP RouteType = iota
	RouteTypeWebSocket
)

type SecurityConfig

type SecurityConfig struct {
	CORS      cors.Config    `ini:"cors" json:"cors" yaml:"cors"`                   // 跨域配置
	IPAccess  IPAccessConfig `ini:"ip_access" json:"ip_access" yaml:"ip_access"`    // IP访问控制
	DebugPath string         `ini:"debug_path" json:"debug_path" yaml:"debug_path"` // 调试路径,默认debug
}

type Server

type Server[T any] struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, cfg *Config) (*Server[any], error)

noinspection all

func NewGeneric

func NewGeneric[T any](ctx context.Context, cfg *Config) (*Server[T], error)

func (*Server[T]) AddLogHandler

func (s *Server[T]) AddLogHandler(le ...middleware.Logger)

AddLogHandler 添加日志处理

func (*Server[T]) AddRoute

func (s *Server[T]) AddRoute(method, path string, handle http.Handler, cb ...Middleware)

func (*Server[T]) AddRouteFunc

func (s *Server[T]) AddRouteFunc(method, path string, handle http.HandlerFunc, cb ...Middleware)

func (*Server[T]) AddRouteFuncWithDescription

func (s *Server[T]) AddRouteFuncWithDescription(method, path string, handle http.HandlerFunc, description Description[T], cb ...Middleware)

func (*Server[T]) AddRouteWithDescription

func (s *Server[T]) AddRouteWithDescription(method, path string, handle http.Handler, description Description[T], cb ...Middleware)

func (*Server[T]) AddWebsocketRoute

func (s *Server[T]) AddWebsocketRoute(path string, handle websocket.Handler)

func (*Server[T]) AddWebsocketRouteWithDescription

func (s *Server[T]) AddWebsocketRouteWithDescription(path string, handle websocket.Handler, description Description[T])

func (*Server[T]) GetRoute

func (s *Server[T]) GetRoute() *route.Route

func (*Server[T]) GetRouteDescriptions

func (s *Server[T]) GetRouteDescriptions() []Description[T]

func (*Server[T]) Group

func (s *Server[T]) Group(groupName string) *Group[T]

func (*Server[T]) Run

func (s *Server[T]) Run(ctx context.Context) error

Run 启动服务

func (*Server[T]) TLS

func (s *Server[T]) TLS() *tls.Config

Jump to

Keyboard shortcuts

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