43 lines
988 B
Go
43 lines
988 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type User struct {
|
|
Name string `mapstructure:"name"`
|
|
Email string `mapstructure:"email"`
|
|
Password string `mapstructure:"password"`
|
|
}
|
|
|
|
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"`
|
|
SuperUser User `mapstructure:"superuser"`
|
|
}
|
|
|
|
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
|
|
}
|