2022-11-13 19:17:07 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-11-21 22:52:44 +01:00
|
|
|
"fmt"
|
2022-11-13 19:17:07 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"os"
|
2022-11-18 21:25:40 +01:00
|
|
|
"time"
|
2022-11-13 19:17:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace string
|
|
|
|
GinDebug bool
|
|
|
|
ServerIP string
|
|
|
|
ServerPort string
|
|
|
|
DBFile string
|
|
|
|
RequestTimeout time.Duration
|
|
|
|
ReturnRawErrors bool
|
|
|
|
FirebaseTokenURI string
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID string
|
2022-11-20 17:19:11 +01:00
|
|
|
FirebasePrivKeyID string
|
|
|
|
FirebaseClientMail string
|
|
|
|
FirebasePrivateKey string
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var Conf Config
|
|
|
|
|
2022-11-20 03:41:38 +01:00
|
|
|
var configLocHost = Config{
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace: "local-host",
|
|
|
|
GinDebug: true,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "8080",
|
|
|
|
DBFile: ".run-data/db.sqlite3",
|
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID: "",
|
|
|
|
FirebasePrivKeyID: "",
|
|
|
|
FirebaseClientMail: "",
|
|
|
|
FirebasePrivateKey: "",
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 03:41:38 +01:00
|
|
|
var configLocDocker = Config{
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace: "local-docker",
|
|
|
|
GinDebug: true,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn_docker.sqlite3",
|
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID: "",
|
|
|
|
FirebasePrivKeyID: "",
|
|
|
|
FirebaseClientMail: "",
|
|
|
|
FirebasePrivateKey: "",
|
2022-11-20 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
var configDev = Config{
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace: "develop",
|
|
|
|
GinDebug: true,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var configStag = Config{
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace: "staging",
|
|
|
|
GinDebug: true,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var configProd = Config{
|
2022-11-20 17:19:11 +01:00
|
|
|
Namespace: "production",
|
|
|
|
GinDebug: false,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: false,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
2022-11-21 22:52:44 +01:00
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var allConfig = []Config{
|
2022-11-20 03:41:38 +01:00
|
|
|
configLocHost,
|
|
|
|
configLocDocker,
|
2022-11-13 19:17:07 +01:00
|
|
|
configDev,
|
|
|
|
configStag,
|
|
|
|
configProd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfig(ns string) (Config, bool) {
|
|
|
|
if ns == "" {
|
2022-11-20 03:41:38 +01:00
|
|
|
return configLocHost, true
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
for _, c := range allConfig {
|
|
|
|
if c.Namespace == ns {
|
|
|
|
return c, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Config{}, false
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:52:44 +01:00
|
|
|
func confEnv(key string) string {
|
|
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
|
|
return v
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Missing required environment variable '%s'", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
func init() {
|
|
|
|
ns := os.Getenv("CONF_NS")
|
|
|
|
|
|
|
|
cfg, ok := getConfig(ns)
|
|
|
|
if !ok {
|
|
|
|
log.Fatal().Str("ns", ns).Msg("Unknown config-namespace")
|
|
|
|
}
|
|
|
|
|
|
|
|
Conf = cfg
|
|
|
|
}
|