-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
109 lines (96 loc) · 2.14 KB
/
app.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
/**
* Copyright 2014 @ S1N1 Team.
* name :
* author : jarryliu
* date : 2014-02-05 21:53
* description :
* history :
*/
package core
import (
"github.com/atnet/gof"
"github.com/atnet/gof/db"
"github.com/atnet/gof/log"
"github.com/atnet/gof/storage"
"github.com/garyburd/redigo/redis"
)
var _ gof.App = new(MainApp)
// application context
// implement of web.Application
type MainApp struct {
Loaded bool
_confFilePath string
_config *gof.Config
_redis *redis.Pool
_dbConnector db.Connector
_debugMode bool
_template *gof.Template
_logger log.ILogger
_storage gof.Storage
}
func NewMainApp(confPath string) *MainApp {
return &MainApp{
_confFilePath: confPath,
}
}
func (this *MainApp) Db() db.Connector {
if this._dbConnector == nil {
this._dbConnector = getDb(this.Config(), this.Log())
}
return this._dbConnector
}
func (this *MainApp) Storage() gof.Storage {
if this._storage == nil {
this._storage = storage.NewRedisStorage(this.Redis())
}
return this._storage
}
func (this *MainApp) Template() *gof.Template {
if this._template == nil {
this._template = initTemplate(this.Config())
}
return this._template
}
func (this *MainApp) Config() *gof.Config {
if this._config == nil {
if cfg, err := gof.LoadConfig(this._confFilePath); err == nil {
this._config = cfg
cfg.Set("exp_fee_bit", float64(1.5))
} else {
log.Fatalln(err)
}
}
return this._config
}
func (this *MainApp) Source() interface{} {
return this
}
func (this *MainApp) Debug() bool {
return this._debugMode
}
func (this *MainApp) Log() log.ILogger {
if this._logger == nil {
var flag int = 0
if this._debugMode {
flag = log.LOpen | log.LESource | log.LStdFlags
}
this._logger = log.NewLogger(nil, " O2O", flag)
}
return this._logger
}
func (this *MainApp) Redis() *redis.Pool {
if this._redis == nil {
this._redis = createRedisPool(this.Config())
}
return this._redis
}
func (this *MainApp) Init(debug, trace bool) bool {
this._debugMode = debug
// 初始化支付数据
paymentCfg(this.Config())
if trace {
this.Db().GetOrm().SetTrace(this._debugMode)
}
this.Loaded = true
return true
}