Documentation ¶
Index ¶
- Variables
- func BasicAuthMiddleware(valid func(c *HTTPContext, username, password string) bool) func(*HTTPContext)
- func ClientIP(r *http.Request) string
- func ClientPublicIP(r *http.Request) string
- func HasLocalIPAddr(ip net.IP) bool
- func HeaderMiddleware(h map[string]string) func(*HTTPContext)
- func LimitMiddleware(tl *utils.TokenBucketLimiter) func(*HTTPContext)
- func LoggerMiddleware() func(*HTTPContext)
- func RemoteIP(r *http.Request) string
- func ValidatorMiddleware(a ...string) func(*HTTPContext)
- type FileBuffer
- type H
- type HTTPContext
- func (c *HTTPContext) BindJSON(v any) error
- func (c *HTTPContext) Debug(msg string, args ...any)
- func (c *HTTPContext) Error(msg string, args ...any)
- func (c *HTTPContext) File(filepath string)
- func (c *HTTPContext) Get(k string) (v any, b bool)
- func (c *HTTPContext) Info(msg string, args ...any)
- func (c *HTTPContext) JSON(status int, v any)
- func (c *HTTPContext) Next()
- func (c *HTTPContext) Render(status int, name string, v any)
- func (c *HTTPContext) Set(k string, v any)
- func (c *HTTPContext) String(status int, msg string)
- func (c *HTTPContext) Warn(msg string, args ...any)
- type IPAdmission
- type JWT
- type MemoryFile
- func (mf *MemoryFile) DeleteFileBuffer(key string)
- func (mf *MemoryFile) FreeFileBuffer(key string)
- func (mf *MemoryFile) GetETag(key string) string
- func (mf *MemoryFile) ReloadFileBuffer(key string) error
- func (mf *MemoryFile) StoreFileBuffer(key string, load func() ([]byte, error)) error
- func (mf *MemoryFile) WriteTo(key string, w io.Writer) (int, error)
- type Renderer
- type WRoute
- func (r *WRoute) CacheFS(dir string, mf *MemoryFile, group ...func(*HTTPContext)) error
- func (r *WRoute) CacheFSGZIP(level int, dir string, mf *MemoryFile, group ...func(*HTTPContext)) error
- func (r *WRoute) CacheFile(file string, mf *MemoryFile, group ...func(*HTTPContext)) error
- func (r *WRoute) CacheFileGZIP(level int, file string, mf *MemoryFile, group ...func(*HTTPContext)) error
- func (r *WRoute) DELETE(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) GET(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) POST(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) PUT(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) SetDebugMode(b bool)
- func (r *WRoute) SetRenderer(s Renderer)
- func (r *WRoute) Static(relativePath, root string, group ...func(*HTTPContext))
- func (r *WRoute) StaticFS(dir string, group ...func(*HTTPContext))
- func (r *WRoute) Use(g ...func(*HTTPContext))
Constants ¶
This section is empty.
Variables ¶
DefaultMarshal 缺省JSON编码器
DefaultUnmarshal 缺省JSON解码器
var GZIPExcludedExtentions []string = []string{".png", ".gif", ".jpeg", ".jpg"}
var HTTPContextPool = sync.Pool{ New: func() interface{} { return &HTTPContext{} }, }
var IPAdmissioneEmpty struct{}
Functions ¶
func BasicAuthMiddleware ¶
func BasicAuthMiddleware(valid func(c *HTTPContext, username, password string) bool) func(*HTTPContext)
func ClientIP ¶
ClientIP 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。
func ClientPublicIP ¶
ClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。
func HasLocalIPAddr ¶
HasLocalIPAddr 检测 IP 地址是否是内网地址 通过直接对比ip段范围效率更高,详见:https://github.com/thinkeridea/go-extend/issues/2
func HeaderMiddleware ¶
func HeaderMiddleware(h map[string]string) func(*HTTPContext)
func LimitMiddleware ¶
func LimitMiddleware(tl *utils.TokenBucketLimiter) func(*HTTPContext)
LimitMiddleware 令牌桶限流器
func ValidatorMiddleware ¶
func ValidatorMiddleware(a ...string) func(*HTTPContext)
ValidatorMiddleware 输入参数验证
Types ¶
type FileBuffer ¶
type FileBuffer struct { LastModify time.Time ETag string Load func() ([]byte, error) Buf *bytes.Buffer }
func NewFileBuffer ¶
func NewFileBuffer(load func() ([]byte, error)) (FileBuffer, error)
func (FileBuffer) Len ¶
func (f FileBuffer) Len() int
type HTTPContext ¶
type HTTPContext struct { Writer http.ResponseWriter Request *http.Request // contains filtered or unexported fields }
HTTPContext 上下文
func (*HTTPContext) Debug ¶
func (c *HTTPContext) Debug(msg string, args ...any)
func (*HTTPContext) Error ¶
func (c *HTTPContext) Error(msg string, args ...any)
func (*HTTPContext) Info ¶
func (c *HTTPContext) Info(msg string, args ...any)
func (*HTTPContext) Render ¶
func (c *HTTPContext) Render(status int, name string, v any)
Render 渲染模板
func (*HTTPContext) String ¶
func (c *HTTPContext) String(status int, msg string)
String 带有状态码的纯文本响应
func (*HTTPContext) Warn ¶
func (c *HTTPContext) Warn(msg string, args ...any)
type IPAdmission ¶
IPAdmission IP准入
func NewIPAdmission ¶
func NewIPAdmission() *IPAdmission
func (*IPAdmission) BlacklistMiddleware ¶
func (f *IPAdmission) BlacklistMiddleware() func(*HTTPContext)
BlacklistMiddleware 黑名单。
func (*IPAdmission) RemoveNode ¶
func (f *IPAdmission) RemoveNode(line string) error
RemoveNode 移除规则
func (*IPAdmission) WhitelistMiddleware ¶
func (f *IPAdmission) WhitelistMiddleware() func(*HTTPContext)
WhitelistMiddleware 白名单。
type MemoryFile ¶
func (*MemoryFile) DeleteFileBuffer ¶
func (mf *MemoryFile) DeleteFileBuffer(key string)
DeleteFileBuffer 删除缓存
func (*MemoryFile) FreeFileBuffer ¶
func (mf *MemoryFile) FreeFileBuffer(key string)
FreeFileBuffer 释放缓存
func (*MemoryFile) GetETag ¶
func (mf *MemoryFile) GetETag(key string) string
func (*MemoryFile) ReloadFileBuffer ¶
func (mf *MemoryFile) ReloadFileBuffer(key string) error
ReloadBuffer 重新载入缓存
func (*MemoryFile) StoreFileBuffer ¶
func (mf *MemoryFile) StoreFileBuffer(key string, load func() ([]byte, error)) error
StoreFileBuffer 存储缓存
type WRoute ¶
type WRoute struct { Mux *http.ServeMux //HTTPContext String、JSON、Render IO Write时错误的处理函数 HookIOWriteError func(*HTTPContext, int, error) // contains filtered or unexported fields }
WRoute 路由
func (*WRoute) CacheFS ¶
func (r *WRoute) CacheFS(dir string, mf *MemoryFile, group ...func(*HTTPContext)) error
CacheFS 当客户端以GET方法请求dir目录时,将返回缓存中的文件 目录内文件修改,缓存并不会一起更改 后缀".tmpl"的文件不缓存
func (*WRoute) CacheFSGZIP ¶
func (r *WRoute) CacheFSGZIP(level int, dir string, mf *MemoryFile, group ...func(*HTTPContext)) error
CacheFSGZIP 当客户端以GET方法请求dir目录时,将返回缓存中的文件,缓存中文件GZIP压缩 目录内文件修改,缓存并不会一起更改 后缀".tmpl"的文件不缓存 数组中GZIPExcludedExtentions包含的后缀文件不压缩,默认设置".png", ".gif", ".jpeg", ".jpg"
func (*WRoute) CacheFile ¶
func (r *WRoute) CacheFile(file string, mf *MemoryFile, group ...func(*HTTPContext)) error
CacheFile 缓存单个静态文件
func (*WRoute) CacheFileGZIP ¶
func (r *WRoute) CacheFileGZIP(level int, file string, mf *MemoryFile, group ...func(*HTTPContext)) error
CacheFileGZIP 用GZIP压缩缓存单个静态文件
func (*WRoute) SetDebugMode ¶
func (*WRoute) SetRenderer ¶
func (*WRoute) Static ¶
func (r *WRoute) Static(relativePath, root string, group ...func(*HTTPContext))
Static 将指定目录下的静态文件映射到URL路径中
func (*WRoute) StaticFS ¶
func (r *WRoute) StaticFS(dir string, group ...func(*HTTPContext))
StaticFS 静态文件目录服务