1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
context.go 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-11-06 18:35 . http 改进
package api
import (
"gitee.com/h79/goutils/auth/token"
"gitee.com/h79/goutils/common/app"
"gitee.com/h79/goutils/common/http"
"gitee.com/h79/goutils/common/stringutil"
"strings"
)
type HeaderContext interface {
Set(key, value string)
Get(key string) string
}
type CacheContext interface {
Get(key string) (interface{}, bool)
Set(key string, value interface{})
}
type HttpContext interface {
Send(httpCode int, data Response)
Abort()
Next()
Param(key string) string
Query(key string) string
ClientIP() string
}
type Context interface {
HttpContext() HttpContext
HeaderContext() HeaderContext
CacheContext() CacheContext
AuthContext() token.Engine
}
func Get[T any](ctx Context, key string) T {
var a T
if v, exist := ctx.CacheContext().Get(key); exist {
a = v.(T)
}
return a
}
func SetUid(ctx Context, uid string) {
ctx.CacheContext().Set(KUid, uid)
}
func GetUid(ctx Context) string {
return Get[string](ctx, KUid)
}
func SetUidI64(ctx Context, uid int64) {
ctx.CacheContext().Set(KUid, stringutil.Int64ToString(uid))
}
func GetUidI64(ctx Context) int64 {
return stringutil.StringToInt64(Get[string](ctx, KUid))
}
func GetHead(ctx Context) *Header {
return Get[*Header](ctx, KHead)
}
func GetBase(ctx Context) Base {
header := GetHead(ctx)
return header.Base
}
func GetReqHead(ctx Context) ReqHead {
header := GetHead(ctx)
return header.ReqHead
}
func GetCustom(ctx Context) interface{} {
header := GetHead(ctx)
return header.Custom
}
func GetSystem(ctx Context) app.System {
return GetBase(ctx).System
}
func GetApp(ctx Context) app.Info {
return GetBase(ctx).App
}
func GetClientIP(ctx Context) string {
return GetBase(ctx).ClientIP
}
func QueryInt64(ctx Context, key string) int64 {
return stringutil.StringToInt64(ctx.HttpContext().Query(key))
}
func HeadInt64(ctx Context, key string) int64 {
return stringutil.StringToInt64(ctx.HeaderContext().Get(key))
}
func ParamInt64(ctx Context, key string) int64 {
return stringutil.StringToInt64(ctx.HttpContext().Param(key))
}
const (
UNK = "unk"
XML = "xml"
JSON = "json"
PLAIN = "plain"
)
func GetContentType(ctx Context) string {
return ContentTypeTo(ctx.HeaderContext().Get("Content-Type"))
}
func ContentTypeTo(ct string) string {
ct = strings.ToLower(ct)
if strings.Contains(ct, http.MimeJSON) {
return JSON
}
if strings.Contains(ct, http.MimeXML2) ||
strings.Contains(ct, http.MimeXML) {
return XML
}
if strings.Contains(ct, http.MimePlain) {
return PLAIN
}
return UNK
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.3.50

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385