-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.go
258 lines (218 loc) · 7.35 KB
/
helper.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2020 The GMC Author. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// More information at https://github.com/snail007/gmc
package gmc
import (
"fmt"
"github.com/snail007/gmc/core"
"github.com/snail007/gmc/module/cache"
gdb "github.com/snail007/gmc/module/db"
gi18n "github.com/snail007/gmc/module/i18n"
gcaptcha "github.com/snail007/gmc/util/captcha"
gmap "github.com/snail007/gmc/util/map"
)
var (
// New shortcut of New gmc stuff
New = &NewAssistant{}
// DB shortcut of gmc database stuff
DB = &DBAssistant{}
// Cache shortcut of gmc cache stuff
Cache = &CacheAssistant{}
// I18n shortcut of gmc i18n
I18n = &I18nAssistant{}
// Err shortcut of gerror
Err = &ErrorAssistant{}
)
// NewAssistant helper to new different gmc objects.
type NewAssistant struct {
}
// Config creates a new object of gconfig.Config
func (s *NewAssistant) Config() gcore.Config {
return gcore.ProviderConfig()()
}
// ConfigFile creates a new object of gconfig.Config from a toml file.
func (s *NewAssistant) ConfigFile(file string) (cfg gcore.Config, err error) {
v := s.Config()
v.SetConfigFile(file)
err = v.ReadInConfig()
if err != nil {
return
}
cfg = v
return
}
// Ctx creates an new object of gcore.Ctx
func (s *NewAssistant) Ctx() gcore.Ctx {
return gcore.ProviderCtx()()
}
// Logger creates an new object of gcore.Logger
func (s *NewAssistant) Logger(ctx gcore.Ctx, prefix string) gcore.Logger {
return gcore.ProviderLogger()(ctx, prefix)
}
// App creates an new object of gcore.App
func (s *NewAssistant) App() gcore.App {
return gcore.ProviderApp()(false)
}
// Captcha creates an captcha object
func (s *NewAssistant) Captcha() *gcaptcha.Captcha {
return gcaptcha.New()
}
// CaptchaDefault creates an captcha object, and sets default configuration
func (s *NewAssistant) CaptchaDefault() *gcaptcha.Captcha {
return gcaptcha.NewDefault()
}
// Tr creates an new object of gi18n.I18nTool
// This only worked after gmc.I18n.Init() called.
// lang is the language translation to.
func (s *NewAssistant) Tr(lang string, ctx gcore.Ctx) (tr gcore.I18n) {
tr, _ = gcore.ProviderI18n()(ctx)
tr.Lang(lang)
return
}
// AppDefault creates a new object of APP and search config file locations:
// ./app.toml or ./conf/app.toml or ./config/app.toml
func (s *NewAssistant) AppDefault() gcore.App {
return gcore.ProviderApp()(true)
}
// Router creates a new object of gcore.HTTPRouter
func (s *NewAssistant) Router(ctx gcore.Ctx) gcore.HTTPRouter {
return gcore.ProviderHTTPRouter()(ctx)
}
// HTTPServer creates a new object of gmc.HTTPServer
func (s *NewAssistant) HTTPServer(ctx gcore.Ctx) gcore.HTTPServer {
return gcore.ProviderHTTPServer()(ctx)
}
// APIServer creates a new object of gmc.APIServer
func (s *NewAssistant) APIServer(ctx gcore.Ctx, address string) (gcore.APIServer, error) {
return gcore.ProviderAPIServer()(ctx, address)
}
// APIServerDefault creates a new object of gmc.APIServer and initialized from app.toml [apiserver] section.
// cfg is a gconfig.Config object contains section [apiserver] in `app.toml`.
func (s *NewAssistant) APIServerDefault(ctx gcore.Ctx) (gcore.APIServer, error) {
return gcore.ProviderAPIServer()(ctx, "")
}
// Map creates a gmap.Map object, gmap.Map's keys are sequenced.
func (s *NewAssistant) Map() *gmap.Map {
return gmap.New()
}
// DBAssistant helper to access database, DB.Init Must be called firstly with config object of app.toml.
type DBAssistant struct {
}
// Init initialize the db group objects from a config object
// contains app.toml section [database].
func (s *DBAssistant) Init(cfg gcore.Config) error {
return gdb.Init(cfg)
}
// InitFromFile initialize the db group objects from a foo.toml config file.
// foo.toml must be contains section [database].
func (s *DBAssistant) InitFromFile(cfgFile string) error {
return gdb.InitFromFile(cfgFile)
}
// DB acquires the default db group object, you must be call Init firstly.
// And you must assert it to the correct type to use.
func (s *DBAssistant) DB(id ...string) gcore.Database {
return gdb.DB(id...)
}
// MySQL acquires the mysql db group object, you must be call Init firstly.
func (s *DBAssistant) MySQL(id ...string) *gdb.MySQLDB {
return gdb.DBMySQL(id...)
}
// SQLite3 acquires the sqlite3 db group object, you must be call Init firstly.
func (s *DBAssistant) SQLite3(id ...string) *gdb.SQLite3DB {
return gdb.DBSQLite3(id...)
}
// Table acquires the table model object, you must be call Init firstly.
func (s *DBAssistant) Table(tableName string) *gdb.Model {
return gdb.Table(tableName)
}
// CacheAssistant helper to access cache, Init Must be called firstly with config object of app.toml.
type CacheAssistant struct {
}
// Init initialize the cache group objects from a config object
// contains app.toml section [cache].
func (s *CacheAssistant) Init(cfg gcore.Config) error {
return gcache.Init(cfg)
}
// Cache acquires the default cache object, you must be call Init firstly.
func (s *CacheAssistant) Cache(id ...string) gcore.Cache {
return gcache.Cache(id...)
}
// Redis acquires the default redis cache object, you must be call Init firstly.
func (s *CacheAssistant) Redis(id ...string) *gcache.RedisCache {
return gcache.Redis(id...)
}
// File acquires the default file cache object, you must be call Init firstly.
func (s *CacheAssistant) File(id ...string) *gcache.FileCache {
return gcache.File(id...)
}
// Memory acquires the default memory cache object, you must be call Init firstly.
func (s *CacheAssistant) Memory(id ...string) *gcache.MemCache {
return gcache.Memory(id...)
}
// I18nAssistant helper to using i18n, Init Must be called firstly with config object of app.toml.
type I18nAssistant struct {
}
// Init initialize the i18n object from a config object
// contains app.toml section [i18n].
func (s *I18nAssistant) Init(cfg gcore.Config) (i18n gcore.I18n, err error) {
err = gi18n.Init(cfg)
if err != nil {
return
}
i18n = gi18n.I18N
return
}
// ErrorAssistant helper to using error or add stack info to error.
type ErrorAssistant struct {
p gcore.ErrorProvider
}
// NewErrorAssistant returns new a ErrorAssistant object.
func NewErrorAssistant() *ErrorAssistant {
return &ErrorAssistant{p: gcore.ProviderError()}
}
// Stack acquires an error full stack info string
func (e *ErrorAssistant) Stack(err interface{}) string {
return e.p().StackError(err)
}
// Recover catch fatal error in defer,
// f can be func(err interface{}) or string or object as string
func (e *ErrorAssistant) Recover(f ...interface{}) {
if err := recover(); err != nil {
eObj := e.p().New(nil)
var f0 interface{}
var printStack bool
if len(f) == 0 {
return
}
if len(f) == 2 {
printStack = f[1].(bool)
}
f0 = f[0]
switch v := f0.(type) {
case func(interface{}):
v(err)
case string:
s := ""
if printStack {
s = fmt.Sprintf(",stack: %s", eObj.StackError(err))
}
fmt.Printf("\nrecover error, %v%s\n", f, s)
default:
fmt.Printf("\nrecover error %s\n", eObj.Wrap(err).ErrorStack())
}
}
}
// New creates a gcore.Error from an error or string, the gcore.Error
// keeps the full stack information.
func (e *ErrorAssistant) New(err interface{}) error {
return e.p().New(err)
}
// Wrap wraps an error to gcore.Error, which keeps
// the full stack information.
func (e *ErrorAssistant) Wrap(err interface{}) error {
return e.p().WrapN(err, 2)
}
func initHelper() {
Err = NewErrorAssistant()
}