Test config package

This commit is contained in:
2024-11-23 16:36:29 +01:00
parent ebdd7bd741
commit 1150c4ba39
2 changed files with 219 additions and 14 deletions

View File

@@ -1,9 +1,9 @@
// Package config provides the configuration for the application
package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@@ -11,6 +11,7 @@ import (
"novamd/internal/crypto"
)
// Config holds the configuration for the application
type Config struct {
DBPath string
WorkDir string
@@ -27,6 +28,7 @@ type Config struct {
IsDevelopment bool
}
// DefaultConfig returns a new Config instance with default values
func DefaultConfig() *Config {
return &Config{
DBPath: "./novamd.db",
@@ -39,6 +41,7 @@ func DefaultConfig() *Config {
}
}
// Validate checks if the configuration is valid
func (c *Config) Validate() error {
if c.AdminEmail == "" || c.AdminPassword == "" {
return fmt.Errorf("NOVAMD_ADMIN_EMAIL and NOVAMD_ADMIN_PASSWORD must be set")
@@ -63,16 +66,10 @@ func Load() (*Config, error) {
if dbPath := os.Getenv("NOVAMD_DB_PATH"); dbPath != "" {
config.DBPath = dbPath
}
if err := ensureDir(filepath.Dir(config.DBPath)); err != nil {
return nil, fmt.Errorf("failed to create database directory: %w", err)
}
if workDir := os.Getenv("NOVAMD_WORKDIR"); workDir != "" {
config.WorkDir = workDir
}
if err := ensureDir(config.WorkDir); err != nil {
return nil, fmt.Errorf("failed to create work directory: %w", err)
}
if staticPath := os.Getenv("NOVAMD_STATIC_PATH"); staticPath != "" {
config.StaticPath = staticPath
@@ -115,10 +112,3 @@ func Load() (*Config, error) {
return config, nil
}
func ensureDir(dir string) error {
if dir == "" {
return nil
}
return os.MkdirAll(dir, 0755)
}