# rht **Repository Path**: ruige_fun/rht ## Basic Information - **Project Name**: rht - **Description**: No description available - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-08 - **Last Updated**: 2025-08-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 介绍 建议版本go版本 >= 1.23.0 # 安装教程 ``` go get gitee.com/ruige_fun/rht ``` # 集成到http服务框架 详情查看 https://gitee.com/ruige_fun/jk # 用法 ## 创建 HttpServerContextFunc ````go func index(w http.ResponseWriter, r *http.Request) { ctx := rht.NewHttpServerContext(w, r) } ```` ## 创建 HttpServerRequestFunc ````go func index(w http.ResponseWriter, r *http.Request) { req := rht.NewHttpServerRequest(r) } ```` ## 创建 HttpServerResponseFunc ````go func index(w http.ResponseWriter, r *http.Request) { writer := rht.NewHttpServerResponse(w) } ```` # 读请求体方法 ````go type HttpServerRequestFunc interface { Request() *http.Request // GetIP 获取客户端IP地址 GetIP() string // GetRemoteAddrIP 获取客户端/反向代理服务器IP,如果有反向代理,则获取的是服务器的IP。 GetRemoteAddrIP() string // GetTrueClientIP 获取真实客户端IP,前提是反向代理里面设置了头 "True-Client-IP" GetTrueClientIP() string // GetXRealIP 获取真实客户端IP,前提是反向代理里面设置了头 "X-Real-Ip" GetXRealIP() string // GetXForwardedForIP 获取相对真实的客户端IP,前提是反向代理里面设置了头 "X-Forwarded-For" GetXForwardedForIP() string // ReadBody 获取全部请求体,只能调用一次,第二次会读取不到内容。 ReadBody() ([]byte, error) // CloneBody 克隆全部请求体,不宜克隆body大的请求体。 CloneBody() ([]byte, error) // ReadQuery 读取请求查询参数,并且值绑定到obj ReadQuery(obj any, skipVerify ...bool) error // ReadQueryByTag 读取请求查询参数,并且值绑定到obj,tag为绑定的tag ReadQueryByTag(obj any, tag string, skipVerify ...bool) error // ReadForm 读取请求表单参数,并且值绑定到obj ReadForm(obj any, skipVerify ...bool) error // ReadFormByTag 读取请求表单参数,并且值绑定到obj,tag为绑定的tag ReadFormByTag(obj any, tag string, skipVerify ...bool) error // ReadPostForm 读取请求表单参数,并且值绑定到obj ReadPostForm(obj any, skipVerify ...bool) error // ReadPostFormByTag 读取请求表单参数,并且值绑定到obj,tag为绑定的tag ReadPostFormByTag(obj any, tag string, skipVerify ...bool) error // ReadJSON 读取请求体,并且JSON解码,只能调用一次,第二次会读取不到内容。 ReadJSON(obj any, skipVerify ...bool) error // ReadXML 读取请求体,并且XML解码,只能调用一次,第二次会读取不到内容。 ReadXML(obj any, skipVerify ...bool) error // GetFormFile 获取表单文件 GetFormFile(name string) (multipart.File, *multipart.FileHeader, error) // GetFormFileSave 获取表单文件,并且保存到本地 GetFormFileSave(name string, saveName string) (int64, error) // SaveFormFile 保存表单文件 SaveFormFile(fh *multipart.FileHeader, saveName 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 // GetAuthorizationBearer 获取请求头 "Authorization",并且去掉 "Bearer " 前缀。 GetAuthorizationBearer() 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 // ContextValue 获取上下文参数 ContextValue(key any) any ContextValueString(key any, def ...string) string ContextValueInt(key any, def ...int) int ContextValueInt64(key any, def ...int64) int64 ContextValueUint(key any, def ...uint) uint ContextValueUint64(key any, def ...uint64) uint64 ContextValueFloat64(key any, def ...float64) float64 ContextValueBool(key any, def ...bool) bool } ```` # 写响应体方法 ````go type HttpServerResponseFunc interface { http.ResponseWriter ResponseWriter() http.ResponseWriter StatusCode() int // HeaderSet 设置响应头 HeaderSet(key, value string) // HeaderAdd 添加响应头 HeaderAdd(key, value string) // HeaderDel 删除响应头 HeaderDel(key string) // HeaderConnectionClose 设置响应头 Connection: close HeaderConnectionClose() // HeaderConnectionKeepAlive 设置响应头 Connection: keep-alive HeaderConnectionKeepAlive() // HeaderSetContentType 设置响应头 Content-Type HeaderSetContentType(value string) // WriteStatusCode 写入状态码 WriteStatusCode(statusCode int) // 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, req *http.Request) error // WriteFileStream 写入响应体 WriteFileStream(reader io.Reader, ext string) error // WriteDownload 写入响应体,让前端下载。 WriteDownload(name string, filePath string, req *http.Request) error // WriteDownloadStream 写入响应体,让前端下载。 WriteDownloadStream(name string, reader io.Reader) error // WriteStdJSON 写入响应体,以JSON编码的格式,按 StdResp 结构体来编码。 WriteStdJSON(code int, msg string, data any, other any) error // WriteStdJsonData 写入响应体,以JSON编码的格式,按 StdResp 结构体来编码。 WriteStdJsonData(data any) error // WriteStdJsonDataOther 写入响应体,以JSON编码的格式,按 StdResp 结构体来编码。 WriteStdJsonDataOther(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 } ```` # 上下文传值方法 仅在作为 Context 的参数时才有效。 比如:https://gitee.com/ruige_fun/jk/blob/master/examples/middleware/main.go ````go type Store interface { Set(key string, value any) SetString(key string, value string) SetInt(key string, value int) SetInt8(key string, value int8) SetInt16(key string, value int16) SetInt32(key string, value int32) SetInt64(key string, value int64) SetUint(key string, value uint) SetUint8(key string, value uint8) SetUint16(key string, value uint16) SetUint32(key string, value uint32) SetUint64(key string, value uint64) SetBool(key string, value bool) SetFloat32(key string, value float32) SetFloat64(key string, value float64) SetTime(key string, value time.Time) SetTimeUnix(key string, value time.Time) SetTimeUnixMilli(key string, value time.Time) Get(key string) any GetValue(key string) (any, bool) GetString(key string, def ...string) string GetInt(key string, def ...int) int GetInt8(key string, def ...int8) int8 GetInt16(key string, def ...int16) int16 GetInt32(key string, def ...int32) int32 GetInt64(key string, def ...int64) int64 GetUint(key string, def ...uint) uint GetUint8(key string, def ...uint8) uint8 GetUint16(key string, def ...uint16) uint16 GetUint32(key string, def ...uint32) uint32 GetUint64(key string, def ...uint64) uint64 GetBool(key string, def ...bool) bool GetFloat32(key string, def ...float32) float32 GetFloat64(key string, def ...float64) float64 GetTime(key string, def ...time.Time) time.Time GetTimeUnix(key string, def ...time.Time) int64 GetTimeUnixMilli(key string, def ...time.Time) int64 Exist(key string) bool Delete(key string) Clear() } ````