118 lines
3.9 KiB
Go
118 lines
3.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/golib/env"
|
|
"git.haelnorr.com/h/golib/hlog"
|
|
"git.haelnorr.com/h/golib/tmdb"
|
|
"github.com/joho/godotenv"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string // Host to listen on
|
|
Port string // Port to listen on
|
|
TrustedHost string // Domain/Hostname to accept as trusted
|
|
SSL bool // Flag for SSL Mode
|
|
GZIP bool // Flag for GZIP compression on requests
|
|
ReadHeaderTimeout time.Duration // Timeout for reading request headers in seconds
|
|
WriteTimeout time.Duration // Timeout for writing requests in seconds
|
|
IdleTimeout time.Duration // Timeout for idle connections in seconds
|
|
DBName string // Filename of the db - hardcoded and doubles as DB version
|
|
DBLockTimeout time.Duration // Timeout for acquiring database lock
|
|
SecretKey string // Secret key for signing tokens
|
|
AccessTokenExpiry int64 // Access token expiry in minutes
|
|
RefreshTokenExpiry int64 // Refresh token expiry in minutes
|
|
TokenFreshTime int64 // Time for tokens to stay fresh in minutes
|
|
LogLevel hlog.Level // Log level for global logging. Defaults to info
|
|
LogOutput string // "file", "console", or "both". Defaults to console
|
|
LogDir string // Path to create log files
|
|
TMDBToken string // Read access token for TMDB API
|
|
TMDBConfig *tmdb.Config // Config data for interfacing with TMDB
|
|
}
|
|
|
|
// Load the application configuration and get a pointer to the Config object
|
|
func GetConfig(args map[string]string) (*Config, error) {
|
|
godotenv.Load(".env")
|
|
var (
|
|
host string
|
|
port string
|
|
logLevel hlog.Level
|
|
logOutput string
|
|
valid bool
|
|
)
|
|
|
|
if args["host"] != "" {
|
|
host = args["host"]
|
|
} else {
|
|
host = env.String("HOST", "127.0.0.1")
|
|
}
|
|
if args["port"] != "" {
|
|
port = args["port"]
|
|
} else {
|
|
port = env.String("PORT", "3010")
|
|
}
|
|
if args["loglevel"] != "" {
|
|
logLevel = hlog.LogLevel(args["loglevel"])
|
|
} else {
|
|
logLevel = hlog.LogLevel(env.String("LOG_LEVEL", "info"))
|
|
}
|
|
if args["logoutput"] != "" {
|
|
opts := map[string]string{
|
|
"both": "both",
|
|
"file": "file",
|
|
"console": "console",
|
|
}
|
|
logOutput, valid = opts[args["logoutput"]]
|
|
if !valid {
|
|
logOutput = "console"
|
|
fmt.Println(
|
|
"Log output type was not parsed correctly. Defaulting to console only",
|
|
)
|
|
}
|
|
} else {
|
|
logOutput = env.String("LOG_OUTPUT", "console")
|
|
}
|
|
if logOutput != "both" && logOutput != "console" && logOutput != "file" {
|
|
logOutput = "console"
|
|
}
|
|
tmdbcfg, err := tmdb.GetConfig(os.Getenv("TMDB_API_TOKEN"))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "tmdb.GetConfig")
|
|
}
|
|
|
|
config := &Config{
|
|
Host: host,
|
|
Port: port,
|
|
TrustedHost: env.String("TRUSTED_HOST", "127.0.0.1"),
|
|
SSL: env.Bool("SSL_MODE", false),
|
|
GZIP: env.Bool("GZIP", false),
|
|
ReadHeaderTimeout: env.Duration("READ_HEADER_TIMEOUT", 2),
|
|
WriteTimeout: env.Duration("WRITE_TIMEOUT", 10),
|
|
IdleTimeout: env.Duration("IDLE_TIMEOUT", 120),
|
|
DBName: "00001",
|
|
DBLockTimeout: env.Duration("DB_LOCK_TIMEOUT", 60),
|
|
SecretKey: env.String("SECRET_KEY", ""),
|
|
AccessTokenExpiry: env.Int64("ACCESS_TOKEN_EXPIRY", 5),
|
|
RefreshTokenExpiry: env.Int64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day
|
|
TokenFreshTime: env.Int64("TOKEN_FRESH_TIME", 5),
|
|
LogLevel: logLevel,
|
|
LogOutput: logOutput,
|
|
LogDir: env.String("LOG_DIR", ""),
|
|
TMDBToken: env.String("TMDB_API_TOKEN", ""),
|
|
TMDBConfig: tmdbcfg,
|
|
}
|
|
|
|
if config.SecretKey == "" && args["dbver"] != "true" {
|
|
return nil, errors.New("Envar not set: SECRET_KEY")
|
|
}
|
|
if config.TMDBToken == "" && args["dbver"] != "true" {
|
|
return nil, errors.New("Envar not set: TMDB_API_TOKEN")
|
|
}
|
|
|
|
return config, nil
|
|
}
|