-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.go
218 lines (189 loc) · 5.38 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
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
package app
import (
"os"
"os/exec"
"path"
"runtime/debug"
"strconv"
"strings"
"time"
_ "time/tzdata"
"github.com/ca17/teamsacs/assets"
"github.com/ca17/teamsacs/common"
"github.com/ca17/teamsacs/common/zaplog"
"github.com/ca17/teamsacs/common/zaplog/log"
"github.com/ca17/teamsacs/config"
"github.com/ca17/teamsacs/models"
"github.com/robfig/cron/v3"
"github.com/spf13/cast"
bolt "go.etcd.io/bbolt"
"gorm.io/gorm"
)
const (
AutoRegisterPopNodeId int64 = 999999999
)
var app *Application
type Application struct {
appConfig *config.AppConfig
gormDB *gorm.DB
sched *cron.Cron
// tsdb tstorage.Storage
cwmpTable *CwmpEventTable
transDB *bolt.DB
}
func GApp() *Application {
return app
}
func GDB() *gorm.DB {
return app.gormDB
}
func GConfig() *config.AppConfig {
return app.appConfig
}
// func GTsdb() tstorage.Storage {
// return app.tsdb
// }
func InitGlobalApplication(cfg *config.AppConfig) {
app = NewApplication(cfg)
app.Init(cfg)
}
func NewApplication(appConfig *config.AppConfig) *Application {
return &Application{appConfig: appConfig}
}
func (a *Application) Config() *config.AppConfig {
return a.appConfig
}
func (a *Application) DB() *gorm.DB {
return a.gormDB
}
func (a *Application) CwmpTable() *CwmpEventTable {
return a.cwmpTable
}
func (a *Application) Init(cfg *config.AppConfig) {
loc, err := time.LoadLocation(cfg.System.Location)
if err != nil {
log.Error("timezone config error")
} else {
time.Local = loc
}
zaplog.InitGlobalLogger(zaplog.LogConfig{
Mode: zaplog.Dev,
ConsoleEnable: true,
LokiEnable: false,
FileEnable: true,
Filename: path.Join(cfg.System.Workdir, "logs/teamsacs.log"),
MetricsHistory: 24 * 7,
MetricsStorage: path.Join(cfg.System.Workdir, "data/metrics"),
})
switch cfg.Database.Type {
case "postgres":
a.gormDB = getPgDatabase(cfg.Database)
default:
panic("not support database type")
}
common.Must(err)
go a.checkSuper()
go a.checkSettings()
// init default node
a.checkDefaultPNode()
a.cwmpTable = NewCwmpEventTable()
a.initJob()
a.RenderTranslateFiles()
}
func (a *Application) MigrateDB(track bool) (err error) {
defer func() {
if err1 := recover(); err1 != nil {
if os.Getenv("GO_DEGUB_TRACE") != "" {
debug.PrintStack()
}
err2, ok := err1.(error)
if ok {
err = err2
log.Error(err2.Error())
}
}
}()
if track {
_ = a.gormDB.Debug().Migrator().AutoMigrate(models.Tables...)
} else {
_ = a.gormDB.Migrator().AutoMigrate(models.Tables...)
}
return nil
}
func (a *Application) DropAll() {
_ = a.gormDB.Migrator().DropTable(models.Tables...)
}
// checkDefaultPNode check default node
func (a *Application) checkDefaultPNode() {
var pnode models.NetNode
err := a.gormDB.Where("id=?", AutoRegisterPopNodeId).First(&pnode).Error
if err != nil {
a.gormDB.Create(&models.NetNode{
ID: AutoRegisterPopNodeId,
Name: "default",
Remark: "Device auto-registration node",
})
}
}
// GetSettingsStringValue Get settings string value
func (a *Application) GetSettingsStringValue(stype string, name string) string {
var value string
a.gormDB.Raw("SELECT value FROM sys_config WHERE type = ? and name = ? limit 1", stype, name).Scan(&value)
return value
}
func (a *Application) GetSettingsInt64Value(stype string, name string) int64 {
var value = a.GetSettingsStringValue(stype, name)
return cast.ToInt64(value)
}
func (a *Application) GetSystemTheme() string {
var value string
a.gormDB.Raw("SELECT value FROM sys_config WHERE type = 'system' and name = 'SystemTheme' limit 1").Scan(&value)
if value == "" {
a.SetSystemTheme("light")
return "light"
}
return value
}
func (a *Application) SetSystemTheme(value string) {
a.gormDB.Exec("UPDATE sys_config set value = ? WHERE type = 'system' and name = 'SystemTheme'", value)
}
// GetTr069SettingsStringValue Get tr069 settings string value
func (a *Application) GetTr069SettingsStringValue(name string) string {
return a.GetSettingsStringValue("tr069", name)
}
func (a *Application) GetRadiusSettingsStringValue(name string) string {
return a.GetSettingsStringValue("radius", name)
}
func (a *Application) GetSystemSettingsStringValue(name string) string {
return a.GetSettingsStringValue("system", name)
}
// BackupDatabase Backup database
func (a *Application) BackupDatabase() error {
scriptsh := assets.PgdumpShell
scriptsh = strings.ReplaceAll(scriptsh, "{dbhost}", a.appConfig.Database.Host)
scriptsh = strings.ReplaceAll(scriptsh, "{dbport}", strconv.FormatInt(int64(a.appConfig.Database.Port), 10))
scriptsh = strings.ReplaceAll(scriptsh, "{dbuser}", a.appConfig.Database.User)
scriptsh = strings.ReplaceAll(scriptsh, "{dbpwd}", a.appConfig.Database.Passwd)
scriptsh = strings.ReplaceAll(scriptsh, "{dbname}", a.appConfig.Database.Name)
_ = os.WriteFile("/tmp/databackup.sh", []byte(scriptsh), 0777)
defer os.Remove("/tmp/databackup.sh")
rbs, err := exec.Command("/bin/sh", "/tmp/databackup.sh").CombinedOutput()
log.Info(string(rbs))
if err != nil {
return err
}
return nil
}
// checkAppVersion Check version
func (a *Application) checkAppVersion() {
cver := a.GetSettingsStringValue("system", "TeamsacsVersion")
buildVersion := assets.BuildVersion()
if buildVersion != cver {
_ = a.gormDB.Exec("UPDATE sys_config SET value = ? WHERE type = ? and name = ?", buildVersion, "system", "TeamsacsVersion")
}
}
func Release() {
app.sched.Stop()
_ = app.transDB.Close()
zaplog.Release()
}