github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/request/context.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package request 5 6 import ( 7 "context" 8 9 "github.com/masterhung0112/hk_server/v5/model" 10 "github.com/masterhung0112/hk_server/v5/shared/i18n" 11 ) 12 13 type Context struct { 14 t i18n.TranslateFunc 15 session model.Session 16 requestId string 17 ipAddress string 18 path string 19 userAgent string 20 acceptLanguage string 21 22 context context.Context 23 } 24 25 func NewContext(ctx context.Context, requestId, ipAddress, path, userAgent, acceptLanguage string, session model.Session, t i18n.TranslateFunc) *Context { 26 return &Context{ 27 t: t, 28 session: session, 29 requestId: requestId, 30 ipAddress: ipAddress, 31 path: path, 32 userAgent: userAgent, 33 acceptLanguage: acceptLanguage, 34 context: ctx, 35 } 36 } 37 38 func EmptyContext() *Context { 39 return &Context{ 40 t: i18n.T, 41 context: context.Background(), 42 } 43 } 44 45 func (c *Context) T(translationID string, args ...interface{}) string { 46 return c.t(translationID, args...) 47 } 48 func (c *Context) Session() *model.Session { 49 return &c.session 50 } 51 func (c *Context) RequestId() string { 52 return c.requestId 53 } 54 func (c *Context) IpAddress() string { 55 return c.ipAddress 56 } 57 func (c *Context) Path() string { 58 return c.path 59 } 60 func (c *Context) UserAgent() string { 61 return c.userAgent 62 } 63 func (c *Context) AcceptLanguage() string { 64 return c.acceptLanguage 65 } 66 67 func (c *Context) Context() context.Context { 68 return c.context 69 } 70 71 func (c *Context) SetSession(s *model.Session) { 72 c.session = *s 73 } 74 75 func (c *Context) SetT(t i18n.TranslateFunc) { 76 c.t = t 77 } 78 func (c *Context) SetRequestId(s string) { 79 c.requestId = s 80 } 81 func (c *Context) SetIpAddress(s string) { 82 c.ipAddress = s 83 } 84 func (c *Context) SetUserAgent(s string) { 85 c.userAgent = s 86 } 87 func (c *Context) SetAcceptLanguage(s string) { 88 c.acceptLanguage = s 89 } 90 func (c *Context) SetPath(s string) { 91 c.path = s 92 } 93 func (c *Context) SetContext(ctx context.Context) { 94 c.context = ctx 95 } 96 97 func (c *Context) GetT() i18n.TranslateFunc { 98 return c.t 99 }