Add: Template Config options & comments;

This commit is contained in:
$(pass /github/name)
2024-07-18 12:25:51 +02:00
parent f4a9166bee
commit 602ac0fef0
5 changed files with 46 additions and 42 deletions

View File

@@ -1,3 +1,10 @@
// 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 (
@@ -12,7 +19,7 @@ import (
)
type DatabaseConfig struct {
Path string `json:"Path" envconfig:"DB_PATH"`
Path string `json:"Path" default:"data/db.sqlite3" envconfig:"DB_PATH"`
}
type AuthenticationConfig struct {
@@ -25,23 +32,27 @@ 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" envconfig:"MAIL_TYPE"`
Mailtype string `json:"Mailtype" default:"html" envconfig:"MAIL_TYPE"`
AdminEmail string `json:"AdminEmail" envconfig:"ADMIN_MAIL"`
Port int `json:"Port" envconfig:"SMTP_PORT"`
Port int `json:"Port" default:"465" envconfig:"SMTP_PORT"`
}
type TemplateConfig struct {
MailDir string `json:"MailDir" envconfig:"TEMPLATE_DIR"`
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 {
Auth AuthenticationConfig `json:"auth"`
DB DatabaseConfig `json:"db"`
Templates TemplateConfig `json:"templates"`
SMTP SMTPConfig `json:"smtp"`
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
@@ -49,7 +60,11 @@ var (
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)
@@ -70,13 +85,19 @@ func LoadConfig() {
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) {
path, err := os.Getwd()
if err != nil {
logger.Error.Fatalf("could not get working directory: %v", err)
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(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)
@@ -90,6 +111,8 @@ func readFile(cfg *Config) {
}
}
// 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 {