-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathinit.go
More file actions
216 lines (179 loc) · 4.9 KB
/
Copy pathinit.go
File metadata and controls
216 lines (179 loc) · 4.9 KB
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
package configs
import (
"crypto/md5"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/glebarez/sqlite"
"github.com/xxcheng123/cloudpan189-share/internal/models"
logger2 "github.com/xxcheng123/cloudpan189-share/internal/pkgs/logger"
"github.com/xxcheng123/cloudpan189-share/internal/pkgs/utils"
"github.com/xxcheng123/cloudpan189-share/internal/shared"
"github.com/zeromicro/go-zero/core/conf"
"go.uber.org/zap"
"gorm.io/gorm"
)
var (
BuildDate string
Commit string
GitBranch string
GitSummary string
)
var c = new(Config)
var configPath string
func init() {
flag.StringVar(&configPath, "config", "etc/config.yaml", "config path")
}
func init() {
flag.Parse()
conf.MustLoad(configPath, c)
}
func Init() {
var err error
// 先判断数据库目录是否存在
dbDir := filepath.Dir(c.DBFile)
if dbDir != "" && dbDir != "." {
// 检查目录是否存在
if _, err = os.Stat(dbDir); os.IsNotExist(err) {
// 目录不存在,创建目录
if err = os.MkdirAll(dbDir, 0755); err != nil {
panic(fmt.Sprintf("创建数据库目录失败: %v", err))
}
} else if err != nil {
// 其他错误(如权限问题)
panic(fmt.Sprintf("检查数据库目录失败: %v", err))
}
}
// 创建日志目录
logDir := filepath.Dir(c.LogFile)
if logDir != "" && logDir != "." {
if _, err = os.Stat(logDir); os.IsNotExist(err) {
if err = os.MkdirAll(logDir, 0755); err != nil {
panic(fmt.Sprintf("创建日志目录失败: %v", err))
}
} else if err != nil {
panic(fmt.Sprintf("检查日志目录失败: %v", err))
}
}
// 创建文件存储目录
if c.FileDir != "" {
if _, err = os.Stat(c.FileDir); os.IsNotExist(err) {
if err = os.MkdirAll(c.FileDir, 0755); err != nil {
panic(fmt.Sprintf("创建文件存储目录失败: %v", err))
}
} else if err != nil {
panic(fmt.Sprintf("检查文件存储目录失败: %v", err))
}
}
db, err = gorm.Open(sqlite.Open(c.DBFile), &gorm.Config{
NowFunc: func() time.Time {
// 使用中国时区,处理加载失败的情况
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
// 如果加载失败,使用固定偏移量 UTC+8
loc = time.FixedZone("CST", 8*3600)
}
return time.Now().In(loc)
},
})
if err != nil {
panic(err)
}
// 数据迁移
if err = db.AutoMigrate(
new(models.User),
new(models.Setting),
new(models.CloudToken),
new(models.VirtualFile),
new(models.UserGroup),
new(models.Group2File),
new(models.SettingDict),
); err != nil {
panic(err)
}
{
var options = []logger2.Option{
logger2.WithTimeLayout(time.DateTime),
logger2.WithFileRotationP(c.LogFile),
logger2.WithInfoLevel(),
//logger2.WithDebugLevel(),
logger2.WithOutputInConsole(),
logger2.WithField("build_info", fmt.Sprintf("[buildDate:%s]&&[commit:%s]&&[gitSummary:%s]&&[gitBranch:%s]", BuildDate, Commit, GitSummary, GitBranch)),
}
logger, err = logger2.NewJSONLogger(options...)
if err != nil {
panic(err)
}
}
//initUser()
initSetting()
var setting = new(models.Setting)
if err = db.First(setting).Error; err != nil {
panic(err)
}
var dicts = make([]*models.SettingDict, 0)
if err = db.Model(new(models.SettingDict)).Find(&dicts).Error; err != nil {
panic(err)
}
for _, dict := range dicts {
switch dict.Key {
case models.SettingDictKeyMultipleStreamThreadCount:
shared.MultipleStreamThreadCount = dict.Value.Int()
case models.SettingDictKeyMultipleStreamChunkSize:
shared.MultipleStreamChunkSize = dict.Value.Int64()
case models.SettingDictKeyStrmFileEnable:
shared.StrmFileEnable = dict.Value.Bool()
case models.SettingDictKeyStrmSupportFileExtList:
shared.StrmSupportFileExtList = dict.Value.StringSlice()
case models.SettingDictKeyFileWritable:
shared.FileWritable = dict.Value.Bool()
}
}
shared.Setting = setting
logger.Info("binary build info",
zap.String("build date", BuildDate),
zap.String("go version", runtime.Version()),
zap.String("git commit", Commit),
zap.String("git branch", GitBranch),
zap.String("git summar", GitSummary),
)
}
func initUser() {
var count int64
db.Model(new(models.User)).Count(&count)
if count > 0 {
return
}
// 随机生成密码
pass := utils.GenerateRandomPassword(12)
// md5计算
user := &models.User{
Username: "admin",
Password: hash(pass),
Permissions: models.PermissionAdmin | models.PermissionDavRead | models.PermissionBase,
}
logger.Info("init create admin user", zap.String("username", user.Username), zap.String("password", pass))
db.Create(user)
}
// 生成 md5
func hash(input string) string {
data := []byte(input)
has := md5.Sum(data)
return fmt.Sprintf("%x", has)
}
func initSetting() {
var count int64
db.Model(new(models.Setting)).Count(&count)
if count > 0 {
return
}
user := &models.Setting{
Title: "天翼订阅小站",
EnableAuth: true,
SaltKey: utils.GenerateRandomPassword(16),
}
db.Create(user)
}