forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
86 lines (79 loc) · 2.98 KB
/
config.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
package gopher
import (
"encoding/json"
"html/template"
"io/ioutil"
"os"
"runtime"
"github.com/bradrydzewski/go.auth"
)
type ConfigStruct struct {
Host string `json:"host"`
Port int `json:"port"`
DB string `json:"db"`
CookieSecret string `json:"cookie_secret"`
SendMailPath string `json:"sendmail_path"`
SmtpUsername string `json:"smtp_username"`
SmtpPassword string `json:"smtp_password"`
SmtpHost string `json:"smtp_host"`
SmtpAddr string `json:"smtp_addr"`
FromEmail string `json:"from_email"`
Superusers string `json:"superusers"`
TimeZoneOffset int64 `json:"time_zone_offset"`
AnalyticsFile string `json:"analytics_file"`
StaticFileVersion int `json:"static_file_version"`
QiniuAccessKey string `json:"qiniu_access_key"`
QiniuSecretKey string `json:"qiniu_secret_key"`
GoGetPath string `json:"go_get_path"`
PackagesDownloadPath string `json:"packages_download_path"`
PublicSalt string `json:"public_salt"`
CookieSecure bool `json:"cookie_secure"`
GithubClientId string `json:"github_auth_client_id"`
GithubClientSecret string `json:"github_auth_client_secret"`
GithubLoginRedirect string `json:"github_login_redirect"`
GithubLoginSuccessRedirect string `json:"github_login_success_redirect"`
DeferPanicApiKey string `json:"deferpanic_api_key"`
GtCaptchaId string `json:"gt_captcha_id"`
GtPrivateKey string `json:"gt_private_key"`
}
var (
Config ConfigStruct
analyticsCode template.HTML // 网站统计分析代码
shareCode template.HTML // 分享代码
goVersion = runtime.Version()
)
func parseJsonFile(path string, v interface{}) {
file, err := os.Open(path)
if err != nil {
logger.Fatal("配置文件读取失败:", err)
}
defer file.Close()
dec := json.NewDecoder(file)
err = dec.Decode(v)
if err != nil {
logger.Fatal("配置文件解析失败:", err)
}
}
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
}
func configGithubAuth() {
if Config.GithubClientId == "" || Config.GithubClientSecret == "" {
logger.Fatal("没有配置github应用的参数")
}
auth.Config.CookieSecret = []byte(Config.CookieSecret)
auth.Config.LoginRedirect = Config.GithubLoginRedirect
auth.Config.LoginSuccessRedirect = Config.GithubLoginSuccessRedirect
auth.Config.CookieSecure = Config.CookieSecure
if !auth.Config.CookieSecure {
logger.Println("注意,cookie_secure设置为false,只能在本地环境下测试")
}
githubHandler = auth.Github(Config.GithubClientId, Config.GithubClientSecret, "user")
}