first commit

This commit is contained in:
Matthias Hinrichs
2025-08-26 03:17:49 +02:00
commit 189e7a2329
34 changed files with 8835 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
package config
import (
"github.com/spf13/viper"
)
type Config struct {
Server ServerConfig `mapstructure:"server"`
Database DatabaseConfig `mapstructure:"database"`
}
type ServerConfig struct {
Port string `mapstructure:"port"`
Host string `mapstructure:"host"`
}
type DatabaseConfig struct {
Type string `mapstructure:"type"`
Path string `mapstructure:"path"`
}
func Load() (*Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("./config")
// Defaults
viper.SetDefault("server.port", "8080")
viper.SetDefault("server.host", "localhost")
viper.SetDefault("database.type", "sqlite")
viper.SetDefault("database.path", "./data.db")
// Environment variables
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
// Config file not found, use defaults
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return nil, err
}
}
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
return &config, nil
}