audon/config.go

210 wiersze
4.9 KiB
Go

package main
import (
2023-01-23 12:10:21 +00:00
"image"
"image/png"
"net/url"
"os"
2023-01-23 12:10:21 +00:00
"path/filepath"
2023-01-26 22:31:53 +00:00
"strconv"
"time"
"github.com/joho/godotenv"
)
type (
AppConfig struct {
AppConfigBase
Livekit *LivekitConfig
MongoURL *url.URL
2022-12-04 17:52:44 +00:00
Database *DBConfig
2022-12-05 07:45:51 +00:00
Redis *RedisConfig
2023-01-14 23:02:15 +00:00
Bot *BotConfig
}
AppConfigBase struct {
LocalDomain string `validate:"required,fqdn"`
2023-01-25 20:58:15 +00:00
Environment string `validate:"printascii"`
StorageDir string
LogoImageBlueBack image.Image
LogoImageWhiteBack image.Image
LogoImageFront image.Image
}
LivekitConfig struct {
2023-01-26 22:31:53 +00:00
APIKey string `validate:"required,ascii"`
APISecret string `validate:"required,ascii"`
Host string `validate:"required,hostname|hostname_port"`
LocalDomain string `validate:"required,hostname|hostname_port"`
URL *url.URL
EmptyRoomTimeout time.Duration `validate:"required"`
}
DBConfig struct {
User string `validate:"required,alphanum"`
Password string `validate:"required,ascii"`
2022-12-04 05:19:41 +00:00
Host string `validare:"required,hostname_port"`
Name string `validate:"required,alphanum"`
}
2022-12-05 07:45:51 +00:00
RedisConfig struct {
Host string `validate:"required,hostname_port"`
User string `validate:"printascii"`
Password string `validate:"printascii"`
}
2023-01-14 23:02:15 +00:00
BotConfig struct {
Enable bool
Server *url.URL
ClientID string
ClientSecret string
AccessToken string
}
)
const (
2022-12-05 07:45:51 +00:00
SESSION_NAME = "session-id"
SESSION_DATASTORE_NAME = "data"
)
func loadConfig(envname string) (*AppConfig, error) {
if envname == "" {
envname = "development"
}
2022-12-10 16:31:32 +00:00
// Loads environment variables in .env files if they exist
localEnv := ".env." + envname + ".local"
if _, err := os.Stat(localEnv); err == nil {
if err := godotenv.Load(localEnv); err != nil {
return nil, err
}
}
2022-12-04 05:19:41 +00:00
if _, err := os.Stat(".env"); err == nil {
if err := godotenv.Load(".env"); err != nil {
return nil, err
}
}
var appConf AppConfig
// Setup base config
2023-01-23 12:10:21 +00:00
storageDir, err := filepath.Abs("public/storage")
if err != nil {
return nil, err
}
if err := os.MkdirAll(storageDir, 0775); err != nil {
return nil, err
}
publicDir, _ := filepath.Abs("public")
2023-01-25 20:58:15 +00:00
logoBlueBack, err := os.Open(filepath.Join(publicDir, "logo_back_blue.png"))
2023-01-23 12:10:21 +00:00
if err != nil {
return nil, err
}
2023-01-25 20:58:15 +00:00
defer logoBlueBack.Close()
logoBlueBackPng, err := png.Decode(logoBlueBack)
if err != nil {
return nil, err
}
logoWhiteBack, err := os.Open(filepath.Join(publicDir, "logo_back_white.png"))
if err != nil {
return nil, err
}
defer logoWhiteBack.Close()
logoWhiteBackPng, err := png.Decode(logoWhiteBack)
2023-01-23 12:10:21 +00:00
if err != nil {
return nil, err
}
logoFront, err := os.Open(filepath.Join(publicDir, "logo_front.png"))
if err != nil {
return nil, err
}
defer logoFront.Close()
logoFrontPng, err := png.Decode(logoFront)
if err != nil {
return nil, err
}
basicConf := AppConfigBase{
2023-01-25 20:58:15 +00:00
LocalDomain: os.Getenv("LOCAL_DOMAIN"),
Environment: envname,
StorageDir: storageDir,
LogoImageBlueBack: logoBlueBackPng,
LogoImageWhiteBack: logoWhiteBackPng,
LogoImageFront: logoFrontPng,
}
if err := mainValidator.Struct(&basicConf); err != nil {
return nil, err
}
appConf.AppConfigBase = basicConf
// Setup MongoDB config
dbconf := &DBConfig{
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASS"),
Host: os.Getenv("DB_HOST"),
2022-12-04 05:19:41 +00:00
Name: os.Getenv("DB_NAME"),
}
if err := mainValidator.Struct(dbconf); err != nil {
return nil, err
}
2022-12-04 05:19:41 +00:00
appConf.Database = dbconf
mongoURL := &url.URL{
Scheme: "mongodb",
User: url.UserPassword(dbconf.User, dbconf.Password),
2022-12-04 05:19:41 +00:00
Host: dbconf.Host,
}
appConf.MongoURL = mongoURL
2022-12-05 07:45:51 +00:00
// Setup Redis config
redisConf := &RedisConfig{
Host: os.Getenv("REDIS_HOST"),
User: os.Getenv("REDIS_USER"),
Password: os.Getenv("REDIS_PASS"),
}
if err := mainValidator.Struct(redisConf); err != nil {
return nil, err
}
appConf.Redis = redisConf
// Setup LiveKit config
2023-01-26 22:31:53 +00:00
timeout, err := strconv.Atoi(os.Getenv("LIVEKIT_EMPTY_ROOM_TIMEOUT"))
if err != nil {
return nil, err
}
lkConf := &LivekitConfig{
2023-01-26 22:31:53 +00:00
APIKey: os.Getenv("LIVEKIT_API_KEY"),
APISecret: os.Getenv("LIVEKIT_API_SECRET"),
Host: os.Getenv("LIVEKIT_HOST"),
LocalDomain: os.Getenv("LIVEKIT_LOCAL_DOMAIN"),
EmptyRoomTimeout: time.Duration(timeout) * time.Second,
}
if err := mainValidator.Struct(lkConf); err != nil {
return nil, err
}
2022-12-05 07:45:51 +00:00
lkURL := &url.URL{
2022-12-10 16:31:32 +00:00
Scheme: "wss",
2022-12-05 07:45:51 +00:00
Host: lkConf.LocalDomain,
2022-12-05 02:38:34 +00:00
}
2022-12-05 07:45:51 +00:00
lkConf.URL = lkURL
appConf.Livekit = lkConf
2023-01-14 23:02:15 +00:00
// Setup Notification Bot config
botHost := os.Getenv("BOT_SERVER")
botConf := &BotConfig{
Enable: botHost != "",
ClientID: os.Getenv("BOT_CLIENT_ID"),
ClientSecret: os.Getenv("BOT_CLIENT_SECRET"),
AccessToken: os.Getenv("BOT_ACCESS_TOKEN"),
}
if botConf.Enable {
botConf.Server = &url.URL{
Host: botHost,
Scheme: "https",
Path: "/",
}
}
appConf.Bot = botConf
return &appConf, nil
}