-
Notifications
You must be signed in to change notification settings - Fork 13
/
core.go
157 lines (138 loc) · 3.72 KB
/
core.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
package system
import (
"log"
"runtime"
"net/url"
"strings"
"github.com/ego008/youdb"
"github.com/gorilla/securecookie"
"github.com/terminus2049/2049bbs/util"
"github.com/weint/config"
)
type MainConf struct {
HttpPort int
HttpsOn bool
Domain string // 若启用https 则该domain 为注册的域名,eg: domain.com、www.domain.com
HttpsPort int
PubDir string
ViewDir string
Youdb string
CookieSecure bool
CookieHttpOnly bool
OldSiteDomain string
TLSCrtFile string
TLSKeyFile string
}
type SiteConf struct {
GoVersion string
MD5Sums string
Name string
Desc string
AdminEmail string
MainDomain string // 上传图片后添加网址前缀, eg: http://domian.com 、http://234.21.35.89:8082
MainNodeIds string
MustLoginNodeIds string
NotHomeNodeIds string
ProverbId string
AvatarMinId int
AvatarMaxId int
HomeNode string
AdminBotId int
AnonymousBotId int
TimeZone int
HomeShowNum int
PageShowNum int
TagShowNum int
CategoryShowNum int
TitleMaxLen int
ContentMaxLen int
PostInterval int
CommentListNum int
CommentInterval int
Authorized bool
RegReview bool
CloseReg bool
AutoDataBackup bool
ResetCookieKey bool
AutoGetTag bool
GetTagApi string
UploadSuffix string
UploadImgOnly bool
UploadImgResize bool
UploadMaxSize int
UploadMaxSizeByte int64
}
type AppConf struct {
Main *MainConf
Site *SiteConf
}
type Application struct {
Cf *AppConf
Db *youdb.DB
Sc *securecookie.SecureCookie
}
func LoadConfig(filename string) *config.Engine {
c := &config.Engine{}
c.Load(filename)
return c
}
func (app *Application) Init(c *config.Engine, currentFilePath string) {
mcf := &MainConf{}
c.GetStruct("Main", mcf)
// check domain
if strings.HasPrefix(mcf.Domain, "http") {
dm, err := url.Parse(mcf.Domain)
if err != nil {
log.Fatal("domain fmt err", err)
}
mcf.Domain = dm.Host
} else {
mcf.Domain = strings.Trim(mcf.Domain, "/")
}
scf := &SiteConf{}
c.GetStruct("Site", scf)
scf.GoVersion = runtime.Version()
fMd5, _ := util.HashFileMD5(currentFilePath)
scf.MD5Sums = fMd5
scf.MainDomain = strings.Trim(scf.MainDomain, "/")
log.Println("MainDomain:", scf.MainDomain)
if scf.TimeZone < -12 || scf.TimeZone > 12 {
scf.TimeZone = 0
}
if scf.UploadMaxSize < 1 {
scf.UploadMaxSize = 1
}
scf.UploadMaxSizeByte = int64(scf.UploadMaxSize) << 20
app.Cf = &AppConf{mcf, scf}
db, err := youdb.Open(mcf.Youdb)
if err != nil {
log.Fatalf("Connect Error: %v", err)
}
app.Db = db
// set main node
db.Hset("keyValue", []byte("main_category"), []byte(scf.MainNodeIds))
// app.Sc = securecookie.New(securecookie.GenerateRandomKey(64),
// securecookie.GenerateRandomKey(32))
//app.Sc.SetSerializer(securecookie.JSONEncoder{})
var hashKey []byte
var blockKey []byte
if scf.ResetCookieKey {
hashKey = securecookie.GenerateRandomKey(64)
blockKey = securecookie.GenerateRandomKey(32)
_ = db.Hmset("keyValue", []byte("hashKey"), hashKey, []byte("blockKey"), blockKey)
} else {
hashKey = append(hashKey, db.Hget("keyValue", []byte("hashKey")).Bytes()...)
blockKey = append(blockKey, db.Hget("keyValue", []byte("blockKey")).Bytes()...)
if len(hashKey) == 0 {
hashKey = securecookie.GenerateRandomKey(64)
blockKey = securecookie.GenerateRandomKey(32)
_ = db.Hmset("keyValue", []byte("hashKey"), hashKey, []byte("blockKey"), blockKey)
}
}
app.Sc = securecookie.New(hashKey, blockKey)
log.Println("youdb Connect to", mcf.Youdb)
}
func (app *Application) Close() {
app.Db.Close()
log.Println("db cloded")
}