Files
cms/pkg/config/config.go
T
2026-05-31 17:35:41 -05:00

36 lines
795 B
Go

package config
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
DBDriver string `mapstructure:"db_driver"`
DBConnString string `mapstructure:"db_conn_string"`
JWTSecret string `mapstructure:"jwt_secret"`
SessionSecret string `mapstructure:"session_secret"`
ServerPort int `mapstructure:"server_port"`
}
func Load(path string) (*Config, error) {
v := viper.New()
v.SetConfigFile(path)
v.SetDefault("db_driver", "sqlite")
v.SetDefault("db_conn_string", "file::memory:?cache=shared")
v.SetDefault("server_port", 8080)
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("read config: %w", err)
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("unmarshal config: %w", err)
}
return &cfg, nil
}