-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.go
197 lines (173 loc) · 4.16 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package application
import (
"net/http"
"github.com/PolarPanda611/trinitygo/httputil"
"github.com/PolarPanda611/trinitygo/util"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
// Context record all thing inside one request
type Context interface {
Application() Application
setRuntime(map[string]string)
Runtime() map[string]string
DB() *gorm.DB
DBTx() *gorm.DB
SafeCommit()
SafeRollback()
DBTxIsOpen() bool
setGinCTX(c *gin.Context)
GinCtx() *gin.Context
setDB(*gorm.DB)
cleanRuntime()
HTTPResponseUnauthorizedErr(error)
HTTPResponseInternalErr(error)
HTTPResponseErr(int, error)
HTTPResponseOk(interface{}, error)
HTTPResponseCreated(interface{}, error)
HTTPResponse(int, interface{}, error)
}
// ContextImpl Context impl
type ContextImpl struct {
app Application
runtime map[string]string
db *gorm.DB
dbTxOpen bool
// http
c *gin.Context
}
// Application get app
func (c *ContextImpl) Application() Application {
return c.app
}
// Runtime get runtime info
func (c *ContextImpl) Runtime() map[string]string {
newRuntime := make(map[string]string, len(c.runtime))
for k, v := range c.runtime {
newRuntime[k] = v
}
return newRuntime
}
// setRuntime set runtime info
func (c *ContextImpl) setRuntime(runtime map[string]string) {
c.runtime = runtime
}
// setRuntime set runtime info
func (c *ContextImpl) setGinCTX(ginCtx *gin.Context) {
c.c = ginCtx
}
// GinCtx get gin ctx
func (c *ContextImpl) GinCtx() *gin.Context {
return c.c
}
// DB get db instance
func (c *ContextImpl) DB() *gorm.DB {
return c.db
}
// DBTx get db instance
func (c *ContextImpl) DBTx() *gorm.DB {
if c.dbTxOpen {
return c.db
}
c.db = c.db.Begin()
c.dbTxOpen = true
return c.db
}
// SafeCommit safe commit
func (c *ContextImpl) SafeCommit() {
if c.dbTxOpen {
c.db.Commit()
c.dbTxOpen = true
}
}
// SafeRollback safe rollback
func (c *ContextImpl) SafeRollback() {
if c.dbTxOpen {
c.db.Rollback()
c.dbTxOpen = false
}
}
// DBTxIsOpen return is transaction is open
func (c *ContextImpl) DBTxIsOpen() bool {
return c.dbTxOpen
}
// setDB set db
func (c *ContextImpl) setDB(db *gorm.DB) {
c.db = db
}
// CleanRuntime clean runtime info
func (c *ContextImpl) cleanRuntime() {
c.c = nil
c.runtime = nil
c.db = nil
}
// HTTPResponseUnauthorizedErr handle http 400 response
func (c *ContextImpl) HTTPResponseUnauthorizedErr(err error) {
c.HTTPResponseErr(401, err)
}
// HTTPResponseInternalErr handle http 400 response
func (c *ContextImpl) HTTPResponseInternalErr(err error) {
c.HTTPResponseErr(400, err)
}
// HTTPResponseErr handle http 400 response
func (c *ContextImpl) HTTPResponseErr(status int, err error) {
if c.c == nil {
panic("gin context not set , please if the func is used in http request handle")
}
err = util.HTTPErrEncoder(err)
if err != nil {
if c.app.Conf().GetAtomicRequest() {
c.SafeRollback()
}
_, resErr := util.HTTPErrDecoder(err)
c.c.AbortWithStatusJSON(status, httputil.ResponseData{
Status: status,
Error: resErr,
Runtime: c.runtime,
})
return
}
}
// HTTPResponseOk handle http http.StatusOK 200 response
func (c *ContextImpl) HTTPResponseOk(res interface{}, err error) {
c.HTTPResponse(http.StatusOK, res, err)
}
// HTTPResponseCreated handle http http.StatusCreated 201 response
func (c *ContextImpl) HTTPResponseCreated(res interface{}, err error) {
c.HTTPResponse(http.StatusCreated, res, err)
}
// HTTPResponse handle response
func (c *ContextImpl) HTTPResponse(status int, res interface{}, err error) {
if c.c == nil {
panic("gin context not set , please if the func is used in http request handle")
}
if err != nil {
c.HTTPResponseInternalErr(err)
return
}
if c.app.Conf().GetAtomicRequest() {
c.SafeCommit()
}
c.c.JSON(status, httputil.ResponseData{
Status: status,
Result: res,
Runtime: c.runtime,
})
return
}
// NewContext new contedt
func NewContext(app Application) Context {
return &ContextImpl{
app: app,
dbTxOpen: false,
}
}
// MockContext used for unittest , don't use in production
func MockContext(app Application, db *gorm.DB, c *gin.Context, runtime map[string]string) Context {
return &ContextImpl{
app: app,
db: db,
c: c,
runtime: runtime,
}
}