2022-11-13 19:17:07 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-11-21 22:52:44 +01:00
|
|
|
"fmt"
|
2022-12-07 22:11:44 +01:00
|
|
|
"github.com/rs/zerolog"
|
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-25 22:42:21 +01:00
|
|
|
Namespace string
|
|
|
|
BaseURL string
|
|
|
|
GinDebug bool
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel zerolog.Level
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP string
|
|
|
|
ServerPort string
|
|
|
|
DBFile string
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal string
|
|
|
|
DBTimeout time.Duration
|
|
|
|
DBMaxOpenConns int
|
|
|
|
DBMaxIdleConns int
|
|
|
|
DBConnMaxLifetime time.Duration
|
|
|
|
DBConnMaxIdleTime time.Duration
|
|
|
|
DBCheckForeignKeys bool
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout time.Duration
|
|
|
|
ReturnRawErrors bool
|
|
|
|
DummyFirebase bool
|
|
|
|
FirebaseTokenURI string
|
|
|
|
FirebaseProjectID string
|
|
|
|
FirebasePrivKeyID string
|
|
|
|
FirebaseClientMail string
|
|
|
|
FirebasePrivateKey string
|
|
|
|
DummyGoogleAPI bool
|
|
|
|
GoogleAPITokenURI string
|
|
|
|
GoogleAPIPrivKeyID string
|
|
|
|
GoogleAPIClientMail string
|
|
|
|
GoogleAPIPrivateKey string
|
|
|
|
GooglePackageName string
|
|
|
|
GoogleProProductID string
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var Conf Config
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var configLocHost = func() Config {
|
|
|
|
return Config{
|
2022-11-25 22:42:21 +01:00
|
|
|
Namespace: "local-host",
|
|
|
|
BaseURL: "http://localhost:8080",
|
|
|
|
GinDebug: true,
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel: zerolog.DebugLevel,
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "8080",
|
|
|
|
DBFile: ".run-data/db.sqlite3",
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 5 * time.Second,
|
|
|
|
DBCheckForeignKeys: true,
|
|
|
|
DBMaxOpenConns: 5,
|
|
|
|
DBMaxIdleConns: 5,
|
|
|
|
DBConnMaxLifetime: 60 * time.Minute,
|
|
|
|
DBConnMaxIdleTime: 60 * time.Minute,
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
DummyFirebase: true,
|
|
|
|
FirebaseTokenURI: "",
|
|
|
|
FirebaseProjectID: "",
|
|
|
|
FirebasePrivKeyID: "",
|
|
|
|
FirebaseClientMail: "",
|
|
|
|
FirebasePrivateKey: "",
|
|
|
|
DummyGoogleAPI: true,
|
|
|
|
GoogleAPITokenURI: "",
|
|
|
|
GoogleAPIPrivKeyID: "",
|
|
|
|
GoogleAPIClientMail: "",
|
|
|
|
GoogleAPIPrivateKey: "",
|
|
|
|
GooglePackageName: "",
|
|
|
|
GoogleProProductID: "",
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var configLocDocker = func() Config {
|
|
|
|
return Config{
|
2022-11-25 22:42:21 +01:00
|
|
|
Namespace: "local-docker",
|
|
|
|
BaseURL: "http://localhost:8080",
|
|
|
|
GinDebug: true,
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel: zerolog.DebugLevel,
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn_docker.sqlite3",
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 5 * time.Second,
|
|
|
|
DBCheckForeignKeys: true,
|
|
|
|
DBMaxOpenConns: 5,
|
|
|
|
DBMaxIdleConns: 5,
|
|
|
|
DBConnMaxLifetime: 60 * time.Minute,
|
|
|
|
DBConnMaxIdleTime: 60 * time.Minute,
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
DummyFirebase: true,
|
|
|
|
FirebaseTokenURI: "",
|
|
|
|
FirebaseProjectID: "",
|
|
|
|
FirebasePrivKeyID: "",
|
|
|
|
FirebaseClientMail: "",
|
|
|
|
FirebasePrivateKey: "",
|
|
|
|
DummyGoogleAPI: true,
|
|
|
|
GoogleAPITokenURI: "",
|
|
|
|
GoogleAPIPrivKeyID: "",
|
|
|
|
GoogleAPIClientMail: "",
|
|
|
|
GoogleAPIPrivateKey: "",
|
|
|
|
GooglePackageName: "",
|
|
|
|
GoogleProProductID: "",
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-20 03:41:38 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var configDev = func() Config {
|
|
|
|
return Config{
|
2022-11-25 22:42:21 +01:00
|
|
|
Namespace: "develop",
|
|
|
|
BaseURL: confEnv("BASE_URL"),
|
|
|
|
GinDebug: true,
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel: zerolog.DebugLevel,
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 5 * time.Second,
|
|
|
|
DBCheckForeignKeys: true,
|
|
|
|
DBMaxOpenConns: 5,
|
|
|
|
DBMaxIdleConns: 5,
|
|
|
|
DBConnMaxLifetime: 60 * time.Minute,
|
|
|
|
DBConnMaxIdleTime: 60 * time.Minute,
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
DummyFirebase: false,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
|
|
|
DummyGoogleAPI: false,
|
|
|
|
GoogleAPITokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
GoogleAPIPrivKeyID: confEnv("GOOG_PRIVATEKEYID"),
|
|
|
|
GoogleAPIClientMail: confEnv("GOOG_CLIENTEMAIL"),
|
|
|
|
GoogleAPIPrivateKey: confEnv("GOOG_PRIVATEKEY"),
|
|
|
|
GooglePackageName: confEnv("GOOG_PACKAGENAME"),
|
|
|
|
GoogleProProductID: confEnv("GOOG_PROPRODUCTID"),
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var configStag = func() Config {
|
|
|
|
return Config{
|
2022-11-25 22:42:21 +01:00
|
|
|
Namespace: "staging",
|
|
|
|
BaseURL: confEnv("BASE_URL"),
|
|
|
|
GinDebug: true,
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel: zerolog.DebugLevel,
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 5 * time.Second,
|
|
|
|
DBCheckForeignKeys: true,
|
|
|
|
DBMaxOpenConns: 5,
|
|
|
|
DBMaxIdleConns: 5,
|
|
|
|
DBConnMaxLifetime: 60 * time.Minute,
|
|
|
|
DBConnMaxIdleTime: 60 * time.Minute,
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: true,
|
|
|
|
DummyFirebase: false,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
|
|
|
DummyGoogleAPI: false,
|
|
|
|
GoogleAPITokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
GoogleAPIPrivKeyID: confEnv("GOOG_PRIVATEKEYID"),
|
|
|
|
GoogleAPIClientMail: confEnv("GOOG_CLIENTEMAIL"),
|
|
|
|
GoogleAPIPrivateKey: confEnv("GOOG_PRIVATEKEY"),
|
|
|
|
GooglePackageName: confEnv("GOOG_PACKAGENAME"),
|
|
|
|
GoogleProProductID: confEnv("GOOG_PROPRODUCTID"),
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var configProd = func() Config {
|
|
|
|
return Config{
|
2022-11-25 22:42:21 +01:00
|
|
|
Namespace: "production",
|
|
|
|
BaseURL: confEnv("BASE_URL"),
|
|
|
|
GinDebug: false,
|
2022-12-07 22:11:44 +01:00
|
|
|
LogLevel: zerolog.InfoLevel,
|
2022-11-25 22:42:21 +01:00
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "80",
|
|
|
|
DBFile: "/data/scn.sqlite3",
|
2022-12-07 22:11:44 +01:00
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 5 * time.Second,
|
|
|
|
DBCheckForeignKeys: true,
|
|
|
|
DBMaxOpenConns: 5,
|
|
|
|
DBMaxIdleConns: 5,
|
|
|
|
DBConnMaxLifetime: 60 * time.Minute,
|
|
|
|
DBConnMaxIdleTime: 60 * time.Minute,
|
2022-11-25 22:42:21 +01:00
|
|
|
RequestTimeout: 16 * time.Second,
|
|
|
|
ReturnRawErrors: false,
|
|
|
|
DummyFirebase: false,
|
|
|
|
FirebaseTokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
FirebaseProjectID: confEnv("FB_PROJECTID"),
|
|
|
|
FirebasePrivKeyID: confEnv("FB_PRIVATEKEYID"),
|
|
|
|
FirebaseClientMail: confEnv("FB_CLIENTEMAIL"),
|
|
|
|
FirebasePrivateKey: confEnv("FB_PRIVATEKEY"),
|
|
|
|
DummyGoogleAPI: false,
|
|
|
|
GoogleAPITokenURI: "https://oauth2.googleapis.com/token",
|
|
|
|
GoogleAPIPrivKeyID: confEnv("GOOG_PRIVATEKEYID"),
|
|
|
|
GoogleAPIClientMail: confEnv("GOOG_CLIENTEMAIL"),
|
|
|
|
GoogleAPIPrivateKey: confEnv("GOOG_PRIVATEKEY"),
|
|
|
|
GooglePackageName: confEnv("GOOG_PACKAGENAME"),
|
|
|
|
GoogleProProductID: confEnv("GOOG_PROPRODUCTID"),
|
2022-11-23 19:32:23 +01:00
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:32:23 +01:00
|
|
|
var allConfig = map[string]func() Config{
|
|
|
|
"local-host": configLocHost,
|
|
|
|
"local-docker": configLocDocker,
|
|
|
|
"develop": configDev,
|
|
|
|
"staging": configStag,
|
|
|
|
"production": configProd,
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getConfig(ns string) (Config, bool) {
|
|
|
|
if ns == "" {
|
2022-11-23 19:32:23 +01:00
|
|
|
return configLocHost(), true
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
2022-11-23 19:32:23 +01:00
|
|
|
if c, ok := allConfig[ns]; ok {
|
|
|
|
return c(), true
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
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 {
|
2022-11-26 17:03:26 +01:00
|
|
|
log.Fatal().Msg(fmt.Sprintf("Missing required environment variable '%s'", key))
|
|
|
|
return ""
|
2022-11-21 22:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|