Documentation
¶
Index ¶
- Constants
- 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 LoggerMiddleware() func(*HTTPContext)
- func RemoteIP(r *http.Request) string
- type H
- type HTTPContext
- func (c *HTTPContext) BindJSON(v any) error
- func (c *HTTPContext) Blob(status int, contentType string, data []byte)
- func (c *HTTPContext) Debug(msg string, args ...any)
- func (c *HTTPContext) Del(k string)
- 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 Renderer
- type WRoute
- func (r *WRoute) DELETE(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) GET(pattern string, fn ...func(*HTTPContext))
- func (r *WRoute) HEAD(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, file string, group ...func(*HTTPContext))
- func (r *WRoute) StaticFS(dir string, group ...func(*HTTPContext))
- func (r *WRoute) Use(g ...func(*HTTPContext))
Constants ¶
View Source
const ( // MIME text MIMETextPlain = "text/plain" MIMETextPlainUTF8 = "text/plain; charset=utf-8" MIMETextPlainISO88591 = "text/plain; charset=iso-8859-1" MIMETextPlainFormatFlowed = "text/plain; format=flowed" MIMETextPlainDelSpaceYes = "text/plain; delsp=yes" MIMETextPlainDelSpaceNo = "text/plain; delsp=no" MIMETextHtml = "text/html" MIMETextCss = "text/css" MIMETextJavascript = "text/javascript" MIMETextCSV = "text/csv" // MIME application MIMEApplicationOctetStream = "application/octet-stream" MIMEApplicationFlash = "application/x-shockwave-flash" MIMEApplicationHTMLForm = "application/x-www-form-urlencoded" MIMEApplicationHTMLFormUTF8 = "application/x-www-form-urlencoded; charset=UTF-8" MIMEApplicationTar = "application/x-tar" MIMEApplicationGZip = "application/gzip" MIMEApplicationXGZip = "application/x-gzip" MIMEApplicationBZip2 = "application/bzip2" MIMEApplicationXBZip2 = "application/x-bzip2" MIMEApplicationShell = "application/x-sh" MIMEApplicationDownload = "application/x-msdownload" MIMEApplicationJSON = "application/json" MIMEApplicationJSONUTF8 = "application/json; charset=utf-8" MIMEApplicationXML = "application/xml" MIMEApplicationXMLUTF8 = "application/xml; charset=utf-8" MIMEApplicationZip = "application/zip" MIMEApplicationPdf = "application/pdf" MIMEApplicationWord = "application/msword" MIMEApplicationExcel = "application/vnd.ms-excel" MIMEApplicationPPT = "application/vnd.ms-powerpoint" MIMEApplicationOpenXMLWord = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" MIMEApplicationOpenXMLExcel = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" MIMEApplicationOpenXMLPPT = "application/vnd.openxmlformats-officedocument.presentationml.presentation" MIMEApplicationWASM = "application/wasm" // MIME image MIMEImageJPEG = "image/jpeg" MIMEImagePNG = "image/png" MIMEImageGIF = "image/gif" MIMEImageBitmap = "image/bmp" MIMEImageWebP = "image/webp" MIMEImageIco = "image/x-icon" MIMEImageMicrosoftICO = "image/vnd.microsoft.icon" MIMEImageTIFF = "image/tiff" MIMEImageSVG = "image/svg+xml" MIMEImagePhotoshop = "image/vnd.adobe.photoshop" MIMEImageAPNG = "image/apng" // MIME audio MIMEAudioBasic = "audio/basic" MIMEAudioL24 = "audio/L24" MIMEAudioMP3 = "audio/mpeg" MIMEAudioMP4 = "audio/mp4" MIMEAudioMPEG = "audio/mpeg" MIMEAudioOggVorbis = "audio/ogg" MIMEAudioWAVE = "audio/wav" MIMEAudioWebM = "audio/webm" MIMEAudioAAC = "audio/aac" MIMEAudioAIFF = "audio/aiff" MIMEAudioMIDI = "audio/midi" MIMEAudioM3U = "application/x-mpegurl" MIMEAudioRealAudio = "audio/x-pn-realaudio" // MIME video MIMEVideoMPEG = "video/mpeg" MIMEVideoOgg = "video/ogg" MIMEVideoMP4 = "video/mp4" MIMEVideoQuickTime = "video/quicktime" MIMEVideoWinMediaVideo = "video/x-ms-wmv" MIMEVideoWebM = "video/webm" MIMEVideoFLV = "video/x-flv" MIMEVideo3GPP = "video/3gpp" MIMEVideoAVI = "video/x-msvideo" MIMEVideoMatroska = "video/x-matroska" MIMEVideoHLS = "application/vnd.apple.mpegurl" )
HTTP 常用 MIME 类型
View Source
const ( // 通用头 HeaderCacheControl = "Cache-Control" HeaderConnection = "Connection" HeaderDate = "Date" HeaderPragma = "Pragma" HeaderTrailer = "Trailer" HeaderTransferEncoding = "Transfer-Encoding" HeaderUpgrade = "Upgrade" HeaderVia = "Via" HeaderTE = "TE" // 请求头 HeaderAccept = "Accept" HeaderAcceptCharset = "Accept-Charset" HeaderAcceptEncoding = "Accept-Encoding" HeaderAcceptLanguage = "Accept-Language" HeaderAuthorization = "Authorization" HeaderCookie = "Cookie" HeaderExpect = "Expect" HeaderFrom = "From" HeaderHost = "Host" HeaderIfMatch = "If-Match" HeaderIfModifiedSince = "If-Modified-Since" HeaderIfNoneMatch = "If-None-Match" HeaderIfRange = "If-Range" HeaderIfUnmodifiedSince = "If-Unmodified-Since" HeaderMaxForwards = "Max-Forwards" HeaderProxyAuthorization = "Proxy-Authorization" HeaderRange = "Range" HeaderReferer = "Referer" HeaderReferrerPolicy = "Referrer-Policy" HeaderUserAgent = "User-Agent" // 响应头 HeaderAcceptRanges = "Accept-Ranges" HeaderAge = "Age" HeaderAllow = "Allow" HeaderContentEncoding = "Content-Encoding" HeaderContentLanguage = "Content-Language" HeaderContentLength = "Content-Length" HeaderContentLocation = "Content-Location" HeaderContentRange = "Content-Range" HeaderContentType = "Content-Type" HeaderETag = "ETag" HeaderExpires = "Expires" HeaderLastModified = "Last-Modified" HeaderLocation = "Location" HeaderProxyAuthenticate = "Proxy-Authenticate" HeaderRetryAfter = "Retry-After" HeaderServer = "Server" HeaderSetCookie = "Set-Cookie" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderVary = "Vary" // 安全头 (新增现代Web安全头) HeaderContentSecurityPolicy = "Content-Security-Policy" HeaderXFrameOptions = "X-Frame-Options" HeaderXXSSProtection = "X-XSS-Protection" HeaderXContentTypeOptions = "X-Content-Type-Options" HeaderStrictTransportSecurity = "Strict-Transport-Security" )
HTTP 头信息
View Source
const ( // Protocol HTTP11 = "HTTP/1.1" HTTP10 = "HTTP/1.0" HTTP20 = "HTTP/2.0" )
HTTP 协议版本
Variables ¶
DefaultMarshal 缺省JSON编码器
DefaultUnmarshal 缺省JSON解码器
View Source
var HTTPContextPool = sync.Pool{ New: func() any { return &HTTPContext{} }, }
View Source
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)
Types ¶
type HTTPContext ¶
type HTTPContext struct {
Writer http.ResponseWriter
Request *http.Request
Flush func() (int, error)
// contains filtered or unexported fields
}
HTTPContext 上下文
func (*HTTPContext) Blob ¶
func (c *HTTPContext) Blob(status int, contentType string, data []byte)
Blob
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 JWT ¶
JWT
func (JWT) CreateToken ¶
CreateToken 生成
func (JWT) JWTMiddleware ¶
func (j JWT) JWTMiddleware(requiredClaims ...string) func(*HTTPContext)
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) DELETE ¶
func (r *WRoute) DELETE(pattern string, fn ...func(*HTTPContext))
DELETE 注册DELETE方法
func (*WRoute) SetDebugMode ¶
func (*WRoute) SetRenderer ¶
func (*WRoute) Static ¶
func (r *WRoute) Static(relativePath, file string, group ...func(*HTTPContext))
Static 将指定目录下的静态文件映射到URL路径中
func (*WRoute) StaticFS ¶
func (r *WRoute) StaticFS(dir string, group ...func(*HTTPContext))
StaticFS 静态文件目录服务
Click to show internal directories.
Click to hide internal directories.