web

package
v0.0.0-...-2aa3bab Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package web provides a simple HTTP multiplexer with middleware support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address[T utils.UintT | utils.IntT](ip net.IP, port T) string

Address 组合IP地址和端口为网络地址字符串

  • ip: IP地址
  • port: 端口号

返回组合的网络地址字符串

func Blob

func Blob[T ~[]byte | ~string](body T, contentType string, status int) http.HandlerFunc

Blob 创建返回字节切片或字符串内容的处理器

  • body: 响应体内容
  • contentType: 内容类型
  • status: HTTP状态码

返回HTTP处理器函数

func FBlob

func FBlob[T ~[]byte | ~string](body func() (T, error), contentType string) http.HandlerFunc

func FJSON

func FJSON[T any](body func() (T, error)) http.HandlerFunc

func JSON

func JSON(body any, status int) http.HandlerFunc

func Recoverer

func Recoverer(next http.Handler) http.Handler

Recoverer 中间件,从panic中恢复并记录日志

func Redirect

func Redirect(to string, permanent ...bool) http.HandlerFunc

Redirect 创建重定向处理器

  • to: 重定向目标URL
  • permanent: 是否永久重定向,默认临时重定向

返回HTTP处理器函数

Types

type Middleware

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

Middleware 定义中间件处理器函数类型,接收下一个处理器并返回包装后的处理器

func BasicAuth

func BasicAuth(username, password string) Middleware

BasicAuth 创建基础认证中间件

  • username: 用户名
  • password: 密码

返回认证处理器

type Mux

type Mux struct {
	chi.Router // 当前多路复用器
	// contains filtered or unexported fields
}

Mux 表示一个HTTP请求多路复用器,支持中间件处理链

func NewMux

func NewMux() *Mux

NewMux 创建一个新的Mux实例,默认包含Recoverer中间件

func (*Mux) BasicAuth

func (mux *Mux) BasicAuth(user, pwd string) *Mux

func (*Mux) OnShutDown

func (mux *Mux) OnShutDown(fn func())

OnShutDown 设置服务器关闭时的回调函数

  • fn: 服务器关闭时执行的函数

func (*Mux) OnStarted

func (mux *Mux) OnStarted(fn func(addr string))

func (*Mux) Recoverer

func (mux *Mux) Recoverer() *Mux

func (*Mux) Run

func (mux *Mux) Run(ctx context.Context, addr string) (err error)

Run 启动HTTP服务器并监听指定地址

  • ctx: 上下文对象
  • addr: 监听地址

返回启动错误(如果有的话)

type Router

type Router interface {
	Handle(pattern string, handler http.Handler)
}

Router 接口定义路由的基本操作

Jump to

Keyboard shortcuts

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