Documentation
¶
Index ¶
- Variables
- func CtxError(ctx context.Context) error
- func CtxRequestErr(r *http.Request) error
- func Dir(root string, listDirectory bool) fs.FS
- func FileHandler(prefix string, fs fs.FS) http.Handler
- func GetCookie(r *http.Request, name string) (string, error)
- func GetCookieDefault(r *http.Request, name, defaultValue string) string
- func GetForm(r *http.Request, key string) string
- func GetFormDefault(r *http.Request, key, defaultValue string) string
- func GetHeader(r *http.Request, key string) string
- func GetHeaderDefault(r *http.Request, key, defaultValue string) string
- func GetQuery(r *http.Request, key string) string
- func GetQueryDefault(r *http.Request, key, defaultValue string) string
- func GetRealIP(r *http.Request) string
- func HandlerChain(h http.Handler, middlewares ...HTTPMiddleware) http.Handler
- func LogDebug(v ...any)
- func LogDebugf(format string, v ...any)
- func LogError(v ...any)
- func LogErrorf(format string, v ...any)
- func LogInfo(v ...any)
- func LogInfof(format string, v ...any)
- func OnlyFilesFS(fs fs.FS, listDirectory bool, root string) fs.FS
- func SetDebug(debug bool)
- func SetLogger(logger Logger)
- func ShouldBind(r *http.Request, obj any) error
- func ShouldBindJSON(r *http.Request, obj any) error
- func WithErrRequest(r *http.Request, err error) *http.Request
- func WithError(ctx context.Context, err error) context.Context
- func WrapHandler(h http.Handler) http.HandlerFunc
- func Write(w http.ResponseWriter, code int, contentType string, message any) error
- func WriteJSON(w http.ResponseWriter, code int, data any) error
- func WriteNoContent(w http.ResponseWriter, code int) error
- func WriteString(w http.ResponseWriter, code int, message any) error
- type HTTPMiddleware
- type Logger
- type Map
- type MiddlewareFunc
- type Mux
- func (mux *Mux) Any(pattern string, hf http.HandlerFunc) *Mux
- func (mux *Mux) DELETE(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) GET(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) Group(prefix string) *Mux
- func (mux *Mux) HEAD(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) Handle(pattern string, handler http.Handler) *Mux
- func (mux *Mux) HandleFunc(pattern string, hf http.HandlerFunc) *Mux
- func (mux *Mux) OPTIONS(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) PATCH(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) POST(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) PUT(relativePath string, hf http.HandlerFunc) *Mux
- func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *Mux) Static(pattern string, root string) *Mux
- func (mux *Mux) StaticFS(pattern string, fs fs.FS) *Mux
- func (mux *Mux) Use(middlewares ...HTTPMiddleware) *Mux
- type Xin
- func (x *Xin) Any(pattern string, hf http.HandlerFunc) *Xin
- func (x *Xin) DELETE(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) GET(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) Group(prefix string) *Mux
- func (x *Xin) HEAD(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) Handle(pattern string, handler http.Handler) *Xin
- func (x *Xin) HandleFunc(pattern string, hf http.HandlerFunc) *Xin
- func (x *Xin) HostPort() (host string, port int)
- func (x *Xin) Mux() *Mux
- func (x *Xin) OPTIONS(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) PATCH(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) POST(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) PUT(relativePath string, hf http.HandlerFunc) *Xin
- func (x *Xin) RecoverHandle(fn errs.RecoverHandle) *Xin
- func (x *Xin) Run(address string, sync bool) error
- func (x *Xin) Serve(ln net.Listener, sync bool) error
- func (x *Xin) Shutdown(timeout time.Duration) error
- func (x *Xin) Static(pattern string, root string) *Xin
- func (x *Xin) StaticFS(pattern string, fs fs.FS) *Xin
- func (x *Xin) Use(middlewares ...HTTPMiddleware) *Xin
Constants ¶
This section is empty.
Variables ¶
var Debug = false
Functions ¶
func CtxRequestErr ¶
CtxRequestErr 从 http.Request 的 context 中获取错误 r 是包含错误的 http.Request 返回存储在 request context 中的错误,如果没有错误则返回 nil
func Dir ¶
Dir 返回 http.FileSystem 实现 listDirectory = true,返回 http.Dir() listDirectory = false,返回 onlyFilesFS,不会显示目录
func FileHandler ¶
FileHandler 处理静态文件请求 参考 http.StripPrefix
func GetCookieDefault ¶
GetCookieDefault 获取Cookie值,如果不存在返回默认值
func GetFormDefault ¶
GetFormDefault 获取表单参数,如果参数不存在返回默认值
func GetHeaderDefault ¶
GetHeaderDefault 获取请求头,如果不存在返回默认值
func GetQueryDefault ¶
GetQueryDefault 获取URL查询参数,如果参数不存在返回默认值
func HandlerChain ¶
func HandlerChain(h http.Handler, middlewares ...HTTPMiddleware) http.Handler
HandlerChain 使用中间件包装 handler
func OnlyFilesFS ¶
OnlyFilesFS 将 fs.FS 包装为 onlyFilesFS root 可以设置根路径,文件将会从根路径开始查找
func ShouldBindJSON ¶
ShouldBindJSON 从body解析json
func WithErrRequest ¶
WithErrRequest 将错误添加到 http.Request 的 context 中 r 是原始的 http.Request err 是要存储的错误 返回包含错误 context 的新 http.Request
func WrapHandler ¶
func WrapHandler(h http.Handler) http.HandlerFunc
WrapHandler wraps `http.Handler` into `http.HandlerFunc`.
func WriteNoContent ¶
func WriteNoContent(w http.ResponseWriter, code int) error
WriteNoContent 只返回响应码,不返回内容
func WriteString ¶
func WriteString(w http.ResponseWriter, code int, message any) error
WriteString 写入响应
Types ¶
type HTTPMiddleware ¶
HTTPMiddleware http.Handler 请求中间件
type Logger ¶
type Logger interface { Debug(v ...any) Debugf(format string, v ...any) Info(v ...any) Infof(format string, v ...any) Error(v ...any) Errorf(format string, v ...any) }
Logger 定义了日志接口
func NewCustomLogger ¶
NewCustomLogger 创建一个自定义输出的日志实现
type MiddlewareFunc ¶
type MiddlewareFunc func(next http.HandlerFunc) http.HandlerFunc
MiddlewareFunc http.HandlerFunc 请求中间件
func WrapMiddleware ¶
func WrapMiddleware(m HTTPMiddleware) MiddlewareFunc
WrapMiddleware wraps `func(http.Handler) http.Handler` into `xin.MiddlewareFunc`
type Mux ¶
Mux http 路由
func (*Mux) Any ¶
func (mux *Mux) Any(pattern string, hf http.HandlerFunc) *Mux
Any alias for HandleFunc
func (*Mux) DELETE ¶
func (mux *Mux) DELETE(relativePath string, hf http.HandlerFunc) *Mux
DELETE 绑定 DELETE 请求
func (*Mux) HEAD ¶
func (mux *Mux) HEAD(relativePath string, hf http.HandlerFunc) *Mux
HEAD is a shortcut for router.Handle("HEAD", path, handlers).
func (*Mux) HandleFunc ¶
func (mux *Mux) HandleFunc(pattern string, hf http.HandlerFunc) *Mux
HandleFunc 注册HTTP处理函数 参考 http.ServeMux.HandleFunc [METHOD][HOST]/[PATH]
func (*Mux) OPTIONS ¶
func (mux *Mux) OPTIONS(relativePath string, hf http.HandlerFunc) *Mux
OPTIONS 绑定 OPTIONS 请求
func (*Mux) PATCH ¶
func (mux *Mux) PATCH(relativePath string, hf http.HandlerFunc) *Mux
PATCH 绑定 PATCH 请求
func (*Mux) POST ¶
func (mux *Mux) POST(relativePath string, hf http.HandlerFunc) *Mux
POST 绑定 POST 请求
type Xin ¶
type Xin struct {
// contains filtered or unexported fields
}
Xin 是核心Web服务器结构体,用于管理HTTP路由和服务器操作
func (*Xin) Any ¶
func (x *Xin) Any(pattern string, hf http.HandlerFunc) *Xin
Any alias for HandleFunc
func (*Xin) DELETE ¶
func (x *Xin) DELETE(relativePath string, hf http.HandlerFunc) *Xin
DELETE 绑定 DELETE 请求
func (*Xin) GET ¶
func (x *Xin) GET(relativePath string, hf http.HandlerFunc) *Xin
GET 注册一个处理GET请求的路由 relativePath 为相对路径 hf 为处理HTTP请求的函数
func (*Xin) HEAD ¶
func (x *Xin) HEAD(relativePath string, hf http.HandlerFunc) *Xin
HEAD is a shortcut for router.Handle("HEAD", path, handlers).
func (*Xin) Handle ¶
Handle 注册一个处理特定模式的HTTP处理器 pattern 格式为 "[METHOD ][HOST]/[PATH]" handler 为实现了http.Handler接口的处理器
func (*Xin) HandleFunc ¶
func (x *Xin) HandleFunc(pattern string, hf http.HandlerFunc) *Xin
HandleFunc 注册一个处理特定模式的处理函数 pattern 格式为 "[METHOD ][HOST]/[PATH]" hf 为处理HTTP请求的函数
func (*Xin) OPTIONS ¶
func (x *Xin) OPTIONS(relativePath string, hf http.HandlerFunc) *Xin
OPTIONS 绑定 OPTIONS 请求
func (*Xin) PATCH ¶
func (x *Xin) PATCH(relativePath string, hf http.HandlerFunc) *Xin
PATCH 绑定 PATCH 请求
func (*Xin) POST ¶
func (x *Xin) POST(relativePath string, hf http.HandlerFunc) *Xin
POST 注册一个处理POST请求的路由 relativePath 为相对路径 hf 为处理HTTP请求的函数
func (*Xin) RecoverHandle ¶
func (x *Xin) RecoverHandle(fn errs.RecoverHandle) *Xin
Recover 设置 panic 处理函数
func (*Xin) Run ¶
Run 启动HTTP服务器 sync 是否同步启动 address 参数格式为 "host:port",例如 ":8080" 或 "192.168.1.100:8080"
func (*Xin) Use ¶
func (x *Xin) Use(middlewares ...HTTPMiddleware) *Xin
Use 添加全局中间件 middlewares 可以添加多个中间件,它们将按照添加顺序依次执行