-
Notifications
You must be signed in to change notification settings - Fork 62
/
new.go
112 lines (95 loc) · 3 KB
/
new.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package config
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
"github.com/bangumi/server/internal/pkg/logger"
)
const defaultMaxMysqlConnection = 4
func NewAppConfig() AppConfig {
logger.Info("reading app config", zap.String("version", Version))
host := getEnv("MYSQL_HOST", "127.0.0.1")
port := getEnv("MYSQL_PORT", "3306")
user := getEnv("MYSQL_USER", "user")
pass := getEnv("MYSQL_PASS", "password")
db := getEnv("MYSQL_DB", "bangumi")
maxConnection := getEnvInt("MYSQL_MAX_CONNECTION", defaultMaxMysqlConnection)
httpPort, err := strconv.Atoi(getEnv("HTTP_PORT", "3000"))
if err != nil {
logger.Fatal("can't parse http port", zap.Error(err))
}
var debug = make(map[string]bool)
if debugV := getEnv("DEBUG", ""); debugV != "" {
logger.Info("enable debug: " + debugV)
for _, v := range strings.Split(debugV, ",") {
v = strings.TrimSpace(v)
debug[v] = true
}
}
redisURL := getEnv("REDIS_URI", "redis://127.0.0.1:6379/0")
redisOptions, err := redis.ParseURL(redisURL)
if err != nil {
logger.Fatal("failed to parse redis url", zap.String("url", redisURL))
}
return AppConfig{
RedisOptions: redisOptions,
MySQLHost: host,
MySQLPort: port,
MySQLUserName: user,
MySQLPassword: pass,
MySQLDatabase: db,
MySQLMaxConn: maxConnection,
Debug: debug,
HTTPPort: httpPort,
HCaptchaSecretKey: getEnv("HCAPTCHA_SECRET_KEY", "0x0000000000000000000000000000000000000000"),
FrontendDomain: getEnv("WEB_DOMAIN", ""),
HTTPHost: getEnv("HTTP_HOST", "127.0.0.1"),
}
}
type AppConfig struct {
Debug map[string]bool
RedisOptions *redis.Options
MySQLHost string
MySQLPort string
MySQLUserName string
MySQLPassword string
MySQLDatabase string
HCaptchaSecretKey string
FrontendDomain string // new frontend web page domain, like next.bgm.tv
HTTPHost string
HTTPPort int
MySQLMaxConn int
}
func getEnv(n, v string) string {
if e, ok := os.LookupEnv(n); ok {
return e
}
return v
}
func getEnvInt(name string, defaultValue int) int {
if raw, ok := os.LookupEnv(name); ok {
v, err := strconv.Atoi(raw)
if err != nil {
logger.Fatal(fmt.Sprintf("failed to read config from env, can't convert '%v' to int", raw),
zap.Error(err), zap.String("env_name", name))
}
return v
}
return defaultValue
}