-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathsystem.go
188 lines (171 loc) · 4.14 KB
/
system.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
package system
import (
"fmt"
"github.com/pelletier/go-toml/v2"
"io/ioutil"
"os"
)
type (
Backup struct {
Enabled bool `toml:"enabled"`
BackupKey string `toml:"backup_key"`
}
Database struct {
Dialect string `toml:"dialect"`
DSN string `toml:"dsn"`
}
Author struct {
Name string `toml:"name"`
Email string `toml:"email"`
}
Seo struct {
Description string `toml:"description"`
Author Author `toml:"author"`
}
Qiniu struct {
Enabled bool `toml:"enabled"`
AccessKey string `toml:"accesskey"`
SecretKey string `toml:"secretkey"`
FileServer string `toml:"fileserver"`
Bucket string `toml:"bucket"`
}
Smms struct {
Enabled bool `toml:"enabled"`
ApiUrl string `toml:"apiurl"`
ApiKey string `toml:"apikey"`
}
Github struct {
Enabled bool `toml:"enabled"`
ClientId string `toml:"clientid"`
ClientSecret string `toml:"clientsecret"`
RedirectURL string `toml:"redirecturl"`
AuthUrl string `toml:"authurl"`
TokenUrl string `toml:"tokenurl"`
Scope string `toml:"scope"`
}
Smtp struct {
Enabled bool `toml:"enabled"`
Username string `toml:"username"`
Password string `toml:"password"`
Host string `toml:"host"`
}
Navigator struct {
Title string `toml:"title"`
Url string `toml:"url"`
Target string `toml:"target"`
}
Configuration struct {
Addr string `toml:"addr"`
SignupEnabled bool `toml:"signup_enabled"`
Title string `toml:"title"`
SessionSecret string `toml:"session_secret"`
Domain string `toml:"domain"`
FileServer string `toml:"file_server"`
NotifyEmails string `toml:"notify_emails"`
PageSize int `toml:"page_size"`
PublicDir string `toml:"public"`
ViewDir string `toml:"view"`
Database Database `toml:"database"`
Seo Seo `toml:"seo"`
Qiniu Qiniu `toml:"qiniu"`
Smms Smms `toml:"smms"`
Github Github `toml:"github"`
Smtp Smtp `toml:"smtp"`
Navigators []Navigator `toml:"navigators"`
Backup Backup `toml:"backup"`
}
)
func (a Author) String() string {
return fmt.Sprintf("%s,%s", a.Name, a.Email)
}
var configuration *Configuration
func defaultConfig() Configuration {
return Configuration{
Addr: ":8090",
Domain: "https://example.com",
Title: "Wblog",
SessionSecret: "wblog",
FileServer: "smms",
PageSize: 10,
PublicDir: "static",
ViewDir: "views/**/*",
Database: Database{
Dialect: "sqlite",
DSN: "wblog.db?_loc=Asia/Shanghai",
},
Seo: Seo{
Description: "Wblog,talk about golang,java and so on.",
Author: Author{
Name: "wangsy",
Email: "[email protected]",
},
},
Qiniu: Qiniu{
AccessKey: "",
SecretKey: "",
FileServer: "",
Bucket: "wblog",
},
Smms: Smms{
ApiUrl: "https://sm.ms/api/v2/upload",
},
Github: Github{
ClientId: "",
ClientSecret: "",
RedirectURL: "https://example.com/oauth2callback",
AuthUrl: "https://github.com/login/oauth/authorize?client_id=%s&scope=user:email&state=%s",
TokenUrl: "https://github.com/login/oauth/access_token",
Scope: "",
},
Smtp: Smtp{
Username: "",
Password: "",
Host: "smtp.163.com:25",
},
Navigators: []Navigator{
{
Title: "Posts",
Url: "/index",
},
{
Title: "AboutMe",
Url: "/page/6",
},
{
Title: "RSS",
Url: "/rss",
Target: "_blank",
},
{
Title: "Subscribe",
Url: "/subscribe",
},
},
}
}
func LoadConfiguration(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var config = defaultConfig()
err = toml.Unmarshal(data, &config)
if err != nil {
return err
}
configuration = &config
return nil
}
func Generate() error {
config := defaultConfig()
placeholder := "[!!]"
config.Domain = placeholder
data, err := toml.Marshal(config)
if err != nil {
return err
}
return os.WriteFile("conf/conf.sample.toml", data, os.ModePerm)
}
func GetConfiguration() *Configuration {
return configuration
}