Documentation
¶
Index ¶
- func RestfulMW_CORS(cfg *RestfulCORSConfig) func(http.Handler) http.Handler
- func RestfulMW_Logging(logger *slog.Logger) func(next http.Handler) http.Handler
- func RestfulMW_SecurityHeader(tlsDisabled bool) func(http.Handler) http.Handler
- func RestfulMW_SkipOptions(next http.Handler) http.Handler
- type RestfulCORSConfig
- type RestfulConfig
- type RestfulRoute
- type RestfulServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RestfulMW_CORS ¶
func RestfulMW_CORS(cfg *RestfulCORSConfig) func(http.Handler) http.Handler
RestfulMW_CORS CORS 中间件
说明:根据配置参数创建 CORS 中间件,支持跨域请求。
func RestfulMW_SecurityHeader ¶
RestfulMW_SecurityHeader 安全头中间件
说明:添加安全头,防止MIME类型猜测、点击劫持、XSS攻击等。 参数:
- next: 下一个处理函数
- tlsDisabled: 是否禁用 TLS 连接,默认 true
Types ¶
type RestfulCORSConfig ¶
type RestfulCORSConfig struct {
AllowOrigins []string `toml:"allow_origins" yaml:"allow_origins" json:"allow_origins"`
AllowMethods []string `toml:"allow_methods" yaml:"allow_methods" json:"allow_methods"`
AllowHeaders []string `toml:"allow_headers" yaml:"allow_headers" json:"allow_headers"`
ExposeHeaders []string `toml:"expose_headers" yaml:"expose_headers" json:"expose_headers"`
MaxAge int `toml:"max_age" yaml:"max_age" json:"max_age"`
AllowCredentials bool `toml:"allow_credentials" yaml:"allow_credentials" json:"allow_credentials"`
}
RestfulCORSConfig CORS 配置
说明:支持 TOML、YAML、 JSON 三种格式配置
func DefaultRestfulCORS ¶
func DefaultRestfulCORS() *RestfulCORSConfig
DefaultRestfulCORS 默认 CORS 配置
说明:返回默认的 CORS 配置,允许所有来源、方法、头,不支持跨域请求。
type RestfulConfig ¶
type RestfulConfig struct {
ListenAddr string `toml:"listen_addr" yaml:"listen_addr" json:"listen_addr"`
URLPrefix string `toml:"url_prefix" yaml:"url_prefix" json:"url_prefix"`
ReadTimeout int `toml:"read_timeout" yaml:"read_timeout" json:"read_timeout"`
WriteTimeout int `toml:"write_timeout" yaml:"write_timeout" json:"write_timeout"`
IdleTimeout int `toml:"idle_timeout" yaml:"idle_timeout" json:"idle_timeout"`
TLSDisabled bool `toml:"tls_disabled" yaml:"tls_disabled" json:"tls_disabled"`
CertFile string `toml:"cert_file" yaml:"cert_file" json:"cert_file"`
KeyFile string `toml:"key_file" yaml:"key_file" json:"key_file"`
CORSDisabled bool `toml:"cors_disabled" yaml:"cors_disabled" json:"cors_disabled"`
CORSConfig *RestfulCORSConfig `toml:"cors_config" yaml:"cors_config" json:"cors_config"`
}
HTTPConfig HTTP 服务配置
说明:支持 TOML、YAML、 JSON 三种格式配置 特别说明:URLPrefix 必须以 / 字符开头,不能以 / 字符结尾,例如 /app01
type RestfulRoute ¶
type RestfulRoute struct {
Pattern string // 路由路径
Method string // HTTP 方法,例如 GET、POST、PUT、DELETE、OPTIONS
HandlerFunc http.HandlerFunc // 处理函数
}
Route 路由定义结构
特别说明:Pattern 是 RestfulConfig.URLPrefix 后面之后的部分,例如 /api/users
type RestfulServer ¶
type RestfulServer struct {
// contains filtered or unexported fields
}
RestfulServer RESTful 服务结构体
func NewRestfulServer ¶
func NewRestfulServer(cfg *RestfulConfig, routes []*RestfulRoute, logger *slog.Logger) *RestfulServer
func (*RestfulServer) Start ¶
func (s *RestfulServer) Start()
Click to show internal directories.
Click to hide internal directories.