Files
GoMembership/internal/config/config.go
2024-07-18 12:25:51 +02:00

122 lines
4.0 KiB
Go

// Package config provides functionality for loading application configuration from a JSON file and environment variables.
// It defines structs for different configuration sections (database, authentication, SMTP, templates) and functions
// to read and populate these configurations. It also generates secrets for JWT and CSRF tokens.
//
// This package uses the `envconfig` library to map environment variables to struct fields, falls back to variables of a config
// file and provides functions for error handling and logging during the configuration loading process.
package config
import (
"encoding/json"
"os"
"path/filepath"
"github.com/kelseyhightower/envconfig"
"GoMembership/internal/utils"
"GoMembership/pkg/logger"
)
type DatabaseConfig struct {
Path string `json:"Path" default:"data/db.sqlite3" envconfig:"DB_PATH"`
}
type AuthenticationConfig struct {
JWTSecret string
CSRFSecret string
APIKEY string `json:"APIKey" envconfig:"API_KEY"`
}
type SMTPConfig struct {
Host string `json:"Host" envconfig:"SMTP_HOST"`
User string `json:"User" envconfig:"SMTP_USER"`
Password string `json:"Password" envconfig:"SMTP_PASS"`
Mailtype string `json:"Mailtype" default:"html" envconfig:"MAIL_TYPE"`
AdminEmail string `json:"AdminEmail" envconfig:"ADMIN_MAIL"`
Port int `json:"Port" default:"465" envconfig:"SMTP_PORT"`
}
type TemplateConfig struct {
MailPath string `json:"MailPath" default:"templates/email" envconfig:"TEMPLATE_MAIL_PATH"`
HTMLPath string `json:"HTMLPath" default:"templates/html" envconfig:"TEMPLATE_HTML_PATH"`
StaticPath string `json:"StaticPath" default:"templates/css" envconfig:"TEMPLATE_STATIC_PATH"`
}
type Config struct {
ConfigFilePath string `json:"config_file_path" envconfig:"CONFIG_FILE_PATH"`
Auth AuthenticationConfig `json:"auth"`
DB DatabaseConfig `json:"db"`
Templates TemplateConfig `json:"templates"`
SMTP SMTPConfig `json:"smtp"`
}
var (
CFGPath string
CFG Config
Auth AuthenticationConfig
DB DatabaseConfig
Templates TemplateConfig
SMTP SMTPConfig
)
// LoadConfig initializes the configuration by reading from a file and environment variables.
// It also generates JWT and CSRF secrets. Returns a Config pointer or an error if any step fails.
func LoadConfig() {
CFGPath = os.Getenv("CONFIG_FILE_PATH")
logger.Info.Printf("Config file environment: %v", CFGPath)
readFile(&CFG)
readEnv(&CFG)
csrfSecret, err := utils.GenerateRandomString(32)
if err != nil {
logger.Error.Fatalf("could not generate CSRF secret: %v", err)
}
jwtSecret, err := utils.GenerateRandomString(32)
if err != nil {
logger.Error.Fatalf("could not generate JWT secret: %v", err)
}
CFG.Auth.JWTSecret = jwtSecret
CFG.Auth.CSRFSecret = csrfSecret
Auth = CFG.Auth
DB = CFG.DB
Templates = CFG.Templates
SMTP = CFG.SMTP
}
// readFile reads the configuration from the specified file path into the provided Config struct.
// If the file path is empty, it defaults to "configs/config.json" in the current working directory.
// Returns an error if the file cannot be opened or the JSON cannot be decoded.
func readFile(cfg *Config) {
if CFGPath == "" {
path, err := os.Getwd()
if err != nil {
logger.Error.Fatalf("could not get working directory: %v", err)
}
CFGPath = filepath.Join(path, "configs", "config.json")
}
configFile, err := os.Open(CFGPath)
// configFile, err := os.Open("config.json")
if err != nil {
logger.Error.Fatalf("could not open config file: %v", err)
}
defer configFile.Close()
decoder := json.NewDecoder(configFile)
err = decoder.Decode(cfg)
if err != nil {
logger.Error.Fatalf("could not decode config file: %v", err)
}
}
// readEnv populates the Config struct with values from environment variables using the envconfig package.
// Returns an error if environment variable decoding fails.
func readEnv(cfg *Config) {
err := envconfig.Process("", cfg)
if err != nil {
logger.Error.Fatalf("could not decode env variables: %#v", err)
}
}