-
Notifications
You must be signed in to change notification settings - Fork 91
/
storage.go
220 lines (199 loc) · 5.02 KB
/
storage.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
219
220
package model
import (
"encoding/json"
"github.com/fuxiaohei/GoBlog/app/utils"
"io/ioutil"
"os"
"path"
"strconv"
)
var (
appVersion int
Storage *jsonStorage
)
type jsonStorage struct {
dir string
}
func (jss *jsonStorage) Init(dir string) {
jss.dir = dir
if !jss.Has("version") {
os.Mkdir(jss.dir, os.ModePerm)
os.Mkdir(path.Join(jss.dir, "content"), os.ModePerm)
os.Mkdir(path.Join(jss.dir, "plugin"), os.ModePerm)
writeDefaultData()
}
}
func (jss *jsonStorage) Has(key string) bool {
file := path.Join(jss.dir, key+".json")
_, e := os.Stat(file)
return e == nil
}
func (jss *jsonStorage) Get(key string, v interface{}) {
file := path.Join(jss.dir, key+".json")
bytes, e := ioutil.ReadFile(file)
if e != nil {
println("read storage '" + key + "' error")
return
}
e = json.Unmarshal(bytes, v)
if e != nil {
println("json decode '" + key + "' error")
}
}
func (jss *jsonStorage) Set(key string, v interface{}) {
locker.Lock()
defer locker.Unlock()
bytes, e := json.Marshal(v)
if e != nil {
println("json encode '" + key + "' error")
return
}
file := path.Join(jss.dir, key+".json")
e = ioutil.WriteFile(file, bytes, 0777)
if e != nil {
println("write storage '" + key + "' error")
}
}
func (jss *jsonStorage) Dir(name string) {
os.MkdirAll(path.Join(jss.dir, name), os.ModePerm)
}
func writeDefaultData() {
// write user
u := new(User)
u.Id = Storage.TimeInc(10)
u.Name = "admin"
u.Password = utils.Sha1("adminxxxxx")
u.Nick = "管理员"
u.Email = "admin@example.com"
u.Url = "http://example.com/"
u.CreateTime = utils.Now()
u.Bio = "这是站点的管理员,你可以添加一些个人介绍,支持换行不支持markdown"
u.LastLoginTime = u.CreateTime
u.Role = "ADMIN"
Storage.Set("users", []*User{u})
// write token
Storage.Set("tokens", map[string]*Token{})
// write contents
a := new(Content)
a.Id = Storage.TimeInc(9)
a.Title = "欢迎使用 Fxh.Go"
a.Slug = "welcome-fxh-go"
a.Text = "如果您看到这篇文章,表示您的 blog 已经安装成功."
a.Tags = []string{"Fxh.Go"}
a.CreateTime = utils.Now()
a.EditTime = a.CreateTime
a.UpdateTime = a.CreateTime
a.IsComment = true
a.IsLinked = false
a.AuthorId = u.Id
a.Type = "article"
a.Status = "publish"
a.Format = "markdown"
a.Template = "blog.html"
a.Hits = 1
// write comments
co := new(Comment)
co.Author = u.Nick
co.Email = u.Email
co.Url = u.Url
co.Content = "欢迎加入使用 Fxh.Go"
co.Avatar = utils.Gravatar(co.Email, "50")
co.Pid = 0
co.Ip = "127.0.0.1"
co.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17"
co.IsAdmin = true
co.Id = Storage.TimeInc(7)
co.CreateTime = utils.Now()
co.Status = "approved"
co.Cid = a.Id
a.Comments = []*Comment{co}
Storage.Set("content/article-"+strconv.Itoa(a.Id), a)
// write pages
p := new(Content)
p.Id = a.Id + Storage.TimeInc(6)
p.Title = "关于"
p.Slug = "about-me"
p.Text = "本页面由 Fxh.Go 创建, 这只是个测试页面."
p.Tags = []string{}
p.CreateTime = utils.Now()
p.EditTime = p.CreateTime
p.UpdateTime = p.UpdateTime
p.IsComment = true
p.IsLinked = true
p.AuthorId = u.Id
p.Type = "page"
p.Status = "publish"
p.Format = "markdown"
p.Comments = make([]*Comment, 0)
p.Template = "page.html"
p.Hits = 1
Storage.Set("content/page-"+strconv.Itoa(p.Id), p)
p2 := new(Content)
p2.Id = p.Id + Storage.TimeInc(6)
p2.Title = "好友"
p2.Slug = "friends"
p2.Text = "本页面由 Fxh.Go 创建, 这只是个测试页面."
p2.Tags = []string{}
p2.CreateTime = utils.Now()
p2.EditTime = p2.CreateTime
p2.UpdateTime = p2.UpdateTime
p2.IsComment = true
p2.IsLinked = true
p2.AuthorId = u.Id
p2.Type = "page"
p2.Status = "publish"
p2.Format = "markdown"
p2.Comments = make([]*Comment, 0)
p2.Template = "page.html"
p2.Hits = 1
Storage.Set("content/page-"+strconv.Itoa(p2.Id), p2)
// write new reader
Storage.Set("readers", map[string]*Reader{})
// write version
v := new(version)
v.Name = "Fxh.Go"
v.BuildTime = utils.Now()
v.Version = appVersion
Storage.Set("version", v)
// write settings
s := map[string]string{
"site_title": "Fxh.Go",
"site_sub_title": "Go开发的简单博客",
"site_keywords": "Fxh.Go,Golang,Blog",
"site_description": "Go语言开发的简单博客程序",
"site_url": "http://localhost/",
"article_size": "4",
"site_theme": "default",
"enable_go_markdown": "false",
"c_footer_weibo": "#",
"c_footer_github": "#",
"c_footer_email": "#",
"c_home_avatar": "/static/img/site.png",
"c_footer_ga": "<!-- google analytics or other -->",
}
Storage.Set("settings", s)
// write files
Storage.Set("files", []*File{})
}
func loadAllData() {
loadVersion()
LoadSettings()
LoadUsers()
LoadTokens()
LoadContents()
LoadReaders()
LoadComments()
LoadFiles()
}
func (jss *jsonStorage) TimeInc(d int) int {
return int(utils.Now())%d + 1
}
func Init(v int) {
appVersion = v
Storage = new(jsonStorage)
Storage.Init("data")
}
func All() {
loadAllData()
StartContentsTimer()
}