use envvars

This commit is contained in:
rxdn 2020-07-26 17:06:28 +01:00
parent 189ec33874
commit b78aec6eef
4 changed files with 214 additions and 130 deletions

View File

@ -15,8 +15,8 @@ func VerifyWhitelabel(isApi bool) func(ctx *gin.Context) {
if rpc.PremiumClient.GetTierByUser(userId, false) < premium.Whitelabel { if rpc.PremiumClient.GetTierByUser(userId, false) < premium.Whitelabel {
var isForced bool var isForced bool
for _, forced := range config.Conf.ForceWhitelabel { for _, id := range config.Conf.Admins {
if forced == userId { if id == userId {
isForced = true isForced = true
break break
} }

View File

@ -1,8 +1,10 @@
package config package config
import ( import (
"github.com/BurntSushi/toml" "github.com/TicketsBot/common/sentry"
"io/ioutil" "os"
"strconv"
"strings"
) )
type ( type (
@ -37,7 +39,7 @@ type (
} }
Oauth struct { Oauth struct {
Id int64 Id uint64
Secret string Secret string
RedirectUri string RedirectUri string
} }
@ -71,13 +73,71 @@ var (
) )
func LoadConfig() { func LoadConfig() {
raw, err := ioutil.ReadFile("config.toml") var admins []uint64
if err != nil { for _, id := range strings.Split(os.Getenv("ADMINS"), ",") {
panic(err) if parsed, err := strconv.ParseUint(id, 10, 64); err == nil {
admins = append(admins, parsed)
} else {
sentry.Error(err)
}
} }
_, err = toml.Decode(string(raw), &Conf) var forcedWhitelabel []uint64
if err != nil { for _, id := range strings.Split(os.Getenv("FORCED_WHITELABEL"), ",") {
panic(err) if parsed, err := strconv.ParseUint(id, 10, 64); err == nil {
forcedWhitelabel = append(forcedWhitelabel, parsed)
} else {
sentry.Error(err)
}
}
rateLimitWindow, _ := strconv.Atoi(os.Getenv("RATELIMIT_WINDOW"))
rateLimitMax, _ := strconv.Atoi(os.Getenv("RATELIMIT_MAX"))
sessionThreads, _ := strconv.Atoi(os.Getenv("SESSION_DB_THREADS"))
oauthId, _ := strconv.ParseUint(os.Getenv("OAUTH_ID"), 10, 64)
redisPort, _ := strconv.Atoi(os.Getenv("REDIS_PORT"))
redisThreads, _ := strconv.Atoi(os.Getenv("REDIS_THREADS"))
Conf = Config{
Admins: admins,
ForceWhitelabel: forcedWhitelabel,
Server: Server{
Host: os.Getenv("SERVER_ADDR"),
BaseUrl: os.Getenv("BASE_URL"),
MainSite: os.Getenv("MAIN_SITE"),
Ratelimit: Ratelimit{
Window: rateLimitWindow,
Max: rateLimitMax,
},
Session: Session{
Threads: sessionThreads,
Secret: os.Getenv("SESSION_SECRET"),
},
Secret: os.Getenv("JWT_SECRET"),
},
Oauth: Oauth{
Id: oauthId,
Secret: os.Getenv("OAUTH_SECRET"),
RedirectUri: os.Getenv("OAUTH_REDIRECT_URI"),
},
Database: Database{
Uri: os.Getenv("DATABASE_URI"),
},
Bot: Bot{
Token: os.Getenv("BOT_TOKEN"),
PremiumLookupProxyUrl: os.Getenv("PREMIUM_PROXY_URL"),
PremiumLookupProxyKey: os.Getenv("PREMIUM_PROXY_KEY"),
ObjectStore: os.Getenv("LOG_ARCHIVER_URL"),
AesKey: os.Getenv("LOG_AES_KEY"),
},
Redis: Redis{
Host: os.Getenv("REDIS_HOST"),
Port: redisPort,
Password: os.Getenv("REDIS_PORT"),
Threads: redisThreads,
},
Cache: Cache{
Uri: os.Getenv("CACHE_URI"),
},
} }
} }

24
envvars.md Normal file
View File

@ -0,0 +1,24 @@
- ADMINS
- FORCED_WHITELABEL
- SERVER_ADDR
- BASE_URL
- MAIN_SITE
- RATELIMIT_WINDOW
- RATELIMIT_MAX
- SESSION_DB_THREADS
- SESSION_SECRET
- JWT_SECRET
- OAUTH_ID
- OAUTH_SECRET
- OAUTH_REDIRECT_URI
- DATABASE_URI
- BOT_TOKEN
- PREMIUM_PROXY_URL
- PREMIUM_PROXY_KEY
- LOG_ARCHIVER_URL
- LOG_AES_KEY
- REDIS_HOST
- REDIS_PORT
- REDIS_PASSWORD
- REDIS_THREADS
- CACHE_URI

View File

@ -43,7 +43,7 @@ const TokenEndpoint = "https://discordapp.com/api/oauth2/token"
func AccessToken(code string) (TokenResponse, error) { func AccessToken(code string) (TokenResponse, error) {
data := TokenData{ data := TokenData{
ClientId: strconv.Itoa(int(config.Conf.Oauth.Id)), ClientId: strconv.FormatUint(config.Conf.Oauth.Id, 10),
ClientSecret: config.Conf.Oauth.Secret, ClientSecret: config.Conf.Oauth.Secret,
GrantType: "authorization_code", GrantType: "authorization_code",
Code: code, Code: code,
@ -66,7 +66,7 @@ func AccessToken(code string) (TokenResponse, error) {
func RefreshToken(refreshToken string) (TokenResponse, error) { func RefreshToken(refreshToken string) (TokenResponse, error) {
data := RefreshData{ data := RefreshData{
ClientId: strconv.Itoa(int(config.Conf.Oauth.Id)), ClientId: strconv.FormatUint(config.Conf.Oauth.Id, 10),
ClientSecret: config.Conf.Oauth.Secret, ClientSecret: config.Conf.Oauth.Secret,
GrantType: "refresh_token", GrantType: "refresh_token",
RefreshToken: refreshToken, RefreshToken: refreshToken,