Documentation ¶
Index ¶
- Constants
- Variables
- func CORS(c Context) bool
- func IgnoreOne[T1 any, T2 any](_t1 T1, _t2 T2) T2
- func IgnoreTwo[T1 any, T2 any](_t1 T1, _t2 T2) T1
- func PointerFalse() *bool
- func PointerFloat32(i float32) *float32
- func PointerFloat64(i float64) *float64
- func PointerInt(i int) *int
- func PointerInt32(i int32) *int32
- func PointerInt64(i int64) *int64
- func PointerString(str string) *string
- func PointerTrue() *bool
- func PointerUint(i uint) *uint
- func StrToBool(str string) bool
- func StrToFloat32(str string) float32
- func StrToFloat64(str string) float64
- func StrToInt(str string) int
- func StrToInt32(str string) int32
- func StrToInt64(str string) int64
- func StrToUint(str string) uint
- func StrToUint32(str string) uint32
- func StrToUint64(str string) uint64
- type Context
- type MiddlewareFunc
- type Serve
- type StdResp
- type StdRespData
- type Store
- type Validator
Constants ¶
View Source
const ( CorsAccessControlAllowHeaders = "Content-Type,Authorization,X-Request-Id,X-CSRF-Token,Sign" CorsAccessControlAllowMethods = "GET,HEAD,POST,PUT,PATCH,DELETE,CONNECT,OPTIONS,TRACE" )
View Source
const ( ContentTypeJSON = "application/json; charset=utf-8" ContentTypeXML = "application/xml; charset=utf-8" ContentTypeStream = "application/octet-stream" ContentTypeDownload = `application/force-download` //文件下载 ContentTypeTextPlain = `text/plain; charset=utf-8` //文本 ContentTypeHTML = `text/html; charset=utf-8` //HTML ContentTypeXJS = `application/x-javascript;charset=utf-8` //JavaScript ContentTypeJS = `text/javascript; charset=utf-8` //JavaScript ContentTypeCSS = `text/css; charset=utf-8` //CSS ContentTypeImageJPEG = `image/jpeg` //jpeg图片 ContentTypeImageJPG = `image/jpeg` //jpg图片 ContentTypeImagePNG = `image/png` //png图片 ContentTypeImageWEBP = `image/webp` //webp图片 ContentTypeImageGIF = `image/gif` //gif图片 ContentTypeImageICON = `image/x-icon` //icon图标 HeaderXRequestID = "X-Request-Id" HeaderAuthorization = "Authorization" HeaderContentType = "Content-Type" HeaderContentDisposition = "Content-Disposition" HeaderCookie = "Cookie" HeaderSetCookie = "Set-Cookie" HeaderXCSRFToken = "X-CSRF-Token" HeaderUpgrade = "Upgrade" HeaderSign = "Sign" HeaderXRealIP = "X-Real-Ip" HeaderXForwardedFor = "X-Forwarded-For" )
Variables ¶
View Source
var Rand = rand.New(rand.NewPCG(uint64(time.Now().UnixNano()+rand.Int64N(1024)), rand.Uint64()))
Functions ¶
func PointerFalse ¶
func PointerFalse() *bool
func PointerFloat32 ¶
func PointerFloat64 ¶
func PointerInt ¶
func PointerInt32 ¶
func PointerInt64 ¶
func PointerString ¶
func PointerTrue ¶
func PointerTrue() *bool
func PointerUint ¶
func StrToFloat32 ¶
func StrToFloat64 ¶
func StrToInt32 ¶
func StrToInt64 ¶
func StrToUint32 ¶
func StrToUint64 ¶
Types ¶
type Context ¶
type Context interface { context.Context // Request 返回原生请求信息 *http.Request Request() *http.Request // ResponseWriter 返回原生的响应写入,http.ResponseWriter ResponseWriter() http.ResponseWriter // Logger 返回原生日志包 *slog.Logger Logger() *slog.Logger // Header 响应头 Header() http.Header // Write 写入响应体 Write(bytes []byte) (int, error) // WriteHeader 写入状态码 WriteHeader(statusCode int) // WriteStatusCode 写入状态码 WriteStatusCode(statusCode int) // GetIP 获取客户端IP地址 GetIP() string // GetRemoteAddrIP 获取客户端/反向代理服务器IP,如果有反向代理,则获取的是服务器的IP。 GetRemoteAddrIP() string // GetXRealIP 获取真实客户端IP,前提是反向代理里面设置了头 "X-Real-Ip" GetXRealIP() string // GetXForwardedForIP 获取相对真实的客户端IP,前提是反向代理里面设置了头 "X-Forwarded-For" GetXForwardedForIP() string // ReadBody 获取全部请求体,只能调用一次,第二次会读取不到内容。 ReadBody() ([]byte, error) // ReadJson 读取请求体,并且JSON解码,只能调用一次,第二次会读取不到内容。 ReadJson(obj any) error // ReadXml 读取请求体,并且XML解码,只能调用一次,第二次会读取不到内容。 ReadXml(obj any) error // ReadJsonV 读取请求体,并且JSON解码和Validator校验,只能调用一次,第二次会读取不到内容。 ReadJsonV(obj any) error // ReadXmlV 读取请求体,并且XML解码和Validator校验,只能调用一次,第二次会读取不到内容。 ReadXmlV(obj any) error // Validator 执行一次Validator校验,需要提前调用 mux.SetValidator设置校验函数。 Validator(obj any) error // WriteHTML 写入响应体 WriteHTML(statusCode int, str string) error // WriteString 写入响应体。 WriteString(statusCode int, str string) error // WriteBytes 写入响应体。 WriteBytes(statusCode int, contentType string, body []byte) error // WriteStream 写入响应体 WriteStream(statusCode int, contentType string, reader io.Reader) error // WriteFile 写入响应体 WriteFile(filePath string) error // WriteDownload 写入响应体,让前端下载。 WriteDownload(name string, filePath string) error // WriteStdJSON 写入响应体,以JSON编码的格式,按 StdResp 结构体来编码。 WriteStdJSON(code int, msg string, data any, other any) error // WriteJSON 写入响应体,以JSON编码的格式。 WriteJSON(v any) error // WriteJSONBytes 写入响应体,以JSON编码的格式。 WriteJSONBytes(body []byte) error // WriteStdXML 写入响应体,以XML编码的格式,按 StdResp 结构体来编码。 WriteStdXML(code int, msg string, data any, other any) error // WriteXML 写入响应体,以XML编码的格式。 WriteXML(v any) error // WriteXMLBytes 写入响应体,以XML编码的格式。 WriteXMLBytes(body []byte) error // GetFormFile 获取表单文件 GetFormFile(name string) (multipart.File, *multipart.FileHeader, error) // GetFormFileSave 获取表单文件,并且保存到本地 GetFormFileSave(name string, saveName string) (int64, error) // SaveFormFile 保存表单文件 SaveFormFile(fh *multipart.FileHeader, dest string) (int64, error) // GetQuery 获取URL查询参数 GetQuery(key string) string // GetFormValue 获取URL查询参数 GetFormValue(key string) string // GetPostFormValue 获取请求体表单参数 GetPostFormValue(key string) string // GetPathValue 获取URL路径参数 GetPathValue(key string) string // GetBasicAuth 获取Basic认证信息 GetBasicAuth() (username, password string, ok bool) // GetPath 获取请求路径 GetPath() string // GetURI 获取请求路径和查询参数 GetURI() string // GetHost 获取Host GetHost() string // GetMethod 获取请求方法 GetMethod() string // GetProto 获取请求协议 GetProto() string // GetHeader 获取请求头 GetHeader() http.Header // GetUserAgent 获取UserAgent GetUserAgent() string // GetXRequestID 获取请求头 "X-Request-Id" GetXRequestID() string // GetAuthorization 获取请求头 "Authorization" GetAuthorization() string // GetXCSRFToken 获取请求头 "X-CSRF-Token" GetXCSRFToken() string // GetQueryString 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryString(key string, def ...string) string // GetFormValueString 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueString(key string, def ...string) string // GetPostFormValueString 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueString(key string, def ...string) string // GetPathValueString 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueString(key string, def ...string) string // GetQueryInt 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryInt(key string, def ...int) int // GetFormValueInt 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueInt(key string, def ...int) int // GetPostFormValueInt 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueInt(key string, def ...int) int // GetPathValueInt 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueInt(key string, def ...int) int // GetQueryUint 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryUint(key string, def ...uint) uint // GetFormValueUint 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueUint(key string, def ...uint) uint // GetPostFormValueUint 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueUint(key string, def ...uint) uint // GetPathValueUint 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueUint(key string, def ...uint) uint // GetQueryInt64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryInt64(key string, def ...int64) int64 // GetFormValueInt64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueInt64(key string, def ...int64) int64 // GetPostFormValueInt64 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueInt64(key string, def ...int64) int64 // GetPathValueInt64 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueInt64(key string, def ...int64) int64 // GetQueryUint64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryUint64(key string, def ...uint64) uint64 // GetFormValueUint64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueUint64(key string, def ...uint64) uint64 // GetPostFormValueUint64 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueUint64(key string, def ...uint64) uint64 // GetPathValueUint64 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueUint64(key string, def ...uint64) uint64 // GetQueryFloat64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryFloat64(key string, def ...float64) float64 // GetFormValueFloat64 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueFloat64(key string, def ...float64) float64 // GetPostFormValueFloat64 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueFloat64(key string, def ...float64) float64 // GetPathValueFloat64 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueFloat64(key string, def ...float64) float64 // GetQueryBool 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetQueryBool(key string, def ...bool) bool // GetFormValueBool 获取URL查询参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetFormValueBool(key string, def ...bool) bool // GetPostFormValueBool 获取请求体表单参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPostFormValueBool(key string, def ...bool) bool // GetPathValueBool 获取URL路径参数,如果不存在或错误,则随机返回def的任意一个,或者默认值。 GetPathValueBool(key string, def ...bool) bool // Store 上下文的数据的kv数据存储管理 Store() Store // Rand 随机函数 Rand() *rand.Rand }
type MiddlewareFunc ¶
type Serve ¶
type Serve interface { // Use 中间件 Use(fx MiddlewareFunc) Serve // Group 分组,自动往请求路径前面增加prevPath Group(prevPath string) Serve // HandleFunc 处理函数 HandleFunc(pattern string, handler func(Context)) // ServeHTTP http服务 ServeHTTP(w http.ResponseWriter, r *http.Request) // AllRouters 返回所有已注册的路由地址 AllRouters() []string // SetValidator 设置表单校验的函数 SetValidator(v Validator) }
func NewServeMux ¶
type StdResp ¶
type StdResp struct { Code int `json:"code" xml:"code"` Msg string `json:"msg" xml:"msg"` Data StdRespData `json:"data" xml:"data"` }
type StdRespData ¶
type Store ¶
type Store interface { Set(key string, val any) Get(key string, def ...any) (any, bool) GetString(key string, def ...string) (string, bool) GetInt(key string, def ...int) (int, bool) GetInt8(key string, def ...int8) (int8, bool) GetInt16(key string, def ...int16) (int16, bool) GetInt32(key string, def ...int32) (int32, bool) GetInt64(key string, def ...int64) (int64, bool) GetUint(key string, def ...uint) (uint, bool) GetUint8(key string, def ...uint8) (uint8, bool) GetUint16(key string, def ...uint16) (uint16, bool) GetUint32(key string, def ...uint32) (uint32, bool) GetUint64(key string, def ...uint64) (uint64, bool) GetFloat32(key string, def ...float32) (float32, bool) GetFloat64(key string, def ...float64) (float64, bool) GetBool(key string, def ...bool) (bool, bool) GetTime(key string, def ...time.Time) (time.Time, bool) }
Store 上下文存储信息,如果key不存在或错误,则随机返回def的任意一个,或者默认值。
Click to show internal directories.
Click to hide internal directories.