-
Notifications
You must be signed in to change notification settings - Fork 234
/
init.go
113 lines (91 loc) · 2.46 KB
/
init.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
package gopher
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"strings"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/jimmykuu/gopher/conf"
"github.com/jimmykuu/gopher/models"
utils1 "github.com/jimmykuu/gopher/utils"
)
var logger = log.New(os.Stdout, "[gopher]:", log.LstdFlags)
func init() {
conf.Version = Version
// 加载敏感词
file, err := os.Open("etc/sensitive_words.txt")
if err != nil {
panic(err)
}
conf.DirtyManager = utils1.NewDirtyManager(file)
err = conf.InitConfig("etc/config.json")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
conf.AnalyticsCode = getDefaultCode(conf.Config.AnalyticsFile)
if conf.Config.DB == "" {
fmt.Println("数据库地址还没有配置,请到config.json内配置db字段.")
os.Exit(1)
}
session, err := mgo.Dial(conf.Config.DB)
if err != nil {
fmt.Println("MongoDB连接失败:", err.Error())
panic(err)
}
session.SetMode(mgo.Monotonic, true)
db := session.DB("gopher")
models.DB = conf.Config.DB
models.PublicSalt = conf.Config.PublicSalt
utils = &Utils{}
// 如果没有status,创建
var status models.Status
c := db.C(models.STATUS)
err = c.Find(nil).One(&status)
if err != nil {
c.Insert(&models.Status{
Id_: bson.NewObjectId(),
UserCount: 0,
TopicCount: 0,
ReplyCount: 0,
UserIndex: 0,
})
}
// 检查是否有超级账户设置
var superusers []string
for _, username := range strings.Split(conf.Config.Superusers, ",") {
username = strings.TrimSpace(username)
if username != "" {
superusers = append(superusers, username)
}
}
if len(superusers) == 0 {
fmt.Println("你没有设置超级账户,请在config.json中的superusers中设置,如有多个账户,用逗号分开")
}
c = db.C(models.USERS)
var users []models.User
c.Find(bson.M{"issuperuser": true}).All(&users)
// 如果mongodb中的超级用户不在配置文件中,取消超级用户
for _, user := range users {
if !stringInArray(superusers, user.Username) {
c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"issuperuser": false}})
}
}
// 设置超级用户
for _, username := range superusers {
c.Update(bson.M{"username": username, "issuperuser": false}, bson.M{"$set": bson.M{"issuperuser": true}})
}
}
func getDefaultCode(path string) (code template.HTML) {
if path != "" {
content, err := ioutil.ReadFile(path)
if err != nil {
logger.Fatal("文件 " + path + " 没有找到")
}
code = template.HTML(string(content))
}
return
}