gitee.com/h79/goutils@v1.22.10/api/context.go (about) 1 package api 2 3 import ( 4 "gitee.com/h79/goutils/auth/token" 5 "gitee.com/h79/goutils/common/app" 6 "gitee.com/h79/goutils/common/http" 7 "gitee.com/h79/goutils/common/stringutil" 8 "strings" 9 ) 10 11 type HeaderContext interface { 12 Set(key, value string) 13 Get(key string) string 14 } 15 16 type CacheContext interface { 17 MustGet(key string) interface{} 18 Get(key string) (interface{}, bool) 19 Set(key string, value interface{}) 20 } 21 22 type HttpContext interface { 23 Send(httpCode int, data interface{}) 24 Abort() 25 Next() 26 Param(key string) string 27 Query(key string) string 28 ClientIP() string 29 } 30 31 type Context interface { 32 HttpContext() HttpContext 33 HeaderContext() HeaderContext 34 CacheContext() CacheContext 35 AuthContext() token.Engine 36 } 37 38 func Get[T any](ctx Context, key string) T { 39 var a T 40 if v, exist := ctx.CacheContext().Get(key); exist { 41 a = v.(T) 42 } 43 return a 44 } 45 46 func MustGet[T any](ctx Context, key string) T { 47 var v = ctx.CacheContext().MustGet(key) 48 return v.(T) 49 } 50 51 func SetUid(ctx Context, uid string) { 52 ctx.CacheContext().Set(Uid, uid) 53 } 54 55 func GetUid(ctx Context) string { 56 return Get[string](ctx, Uid) 57 } 58 59 func SetInt64Uid(ctx Context, uid int64) { 60 SetUid(ctx, stringutil.Int64ToString(uid)) 61 } 62 63 func GetInt64Uid(ctx Context) int64 { 64 return stringutil.StringToInt64(GetUid(ctx)) 65 } 66 67 func GetHead(ctx Context) *Header { 68 return Get[*Header](ctx, Head) 69 } 70 71 func GetBase(ctx Context) Base { 72 header := GetHead(ctx) 73 return header.Base 74 } 75 76 func GetReqHead(ctx Context) ReqHead { 77 header := GetHead(ctx) 78 return header.ReqHead 79 } 80 81 func GetCustom(ctx Context) interface{} { 82 header := GetHead(ctx) 83 return header.Custom 84 } 85 86 func GetSystem(ctx Context) app.System { 87 return GetBase(ctx).System 88 } 89 90 func GetApp(ctx Context) app.Info { 91 return GetBase(ctx).App 92 } 93 94 func GetClientIP(ctx Context) string { 95 return GetBase(ctx).ClientIP 96 } 97 98 func QueryInt64(ctx Context, key string) int64 { 99 return stringutil.StringToInt64(ctx.HttpContext().Query(key)) 100 } 101 102 func HeadInt64(ctx Context, key string) int64 { 103 return stringutil.StringToInt64(ctx.HeaderContext().Get(key)) 104 } 105 106 func ParamInt64(ctx Context, key string) int64 { 107 return stringutil.StringToInt64(ctx.HttpContext().Param(key)) 108 } 109 110 const ( 111 UNK = "unk" 112 XML = "xml" 113 JSON = "json" 114 PLAIN = "plain" 115 ContentType = "Content-Type" 116 ) 117 118 func GetContentType(ctx Context) string { 119 return ContentTypeTo(ctx.HeaderContext().Get(ContentType)) 120 } 121 122 func ContentTypeTo(ct string) string { 123 ct = strings.ToLower(ct) 124 if strings.Contains(ct, http.MimeJSON) { 125 return JSON 126 } 127 if strings.Contains(ct, http.MimeXML2) || 128 strings.Contains(ct, http.MimeXML) { 129 return XML 130 } 131 if strings.Contains(ct, http.MimePlain) { 132 return PLAIN 133 } 134 return UNK 135 }