portal/config/config.go

47 lines
1.2 KiB
Go
Raw Normal View History

2023-04-29 17:38:21 +00:00
package config
import (
2023-04-30 06:14:14 +00:00
"errors"
2023-04-29 17:38:21 +00:00
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
)
func Init() {
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath("/etc/lumeweb/portal/")
viper.AddConfigPath("$HOME/.lumeweb/portal/")
viper.AddConfigPath(".")
viper.SetEnvPrefix("LUME_WEB_PORTAL")
2023-04-30 06:14:14 +00:00
pflag.String("database.type", "sqlite", "Database type")
2023-04-29 17:38:21 +00:00
pflag.String("database.host", "localhost", "Database host")
pflag.Int("database.port", 3306, "Database port")
pflag.String("database.user", "root", "Database user")
pflag.String("database.password", "", "Database password")
2023-04-30 06:14:14 +00:00
pflag.String("database.name", "lumeweb_portal", "Database name")
pflag.String("database.path", "./db.sqlite", "Database path for SQLite")
2023-04-29 17:38:21 +00:00
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
log.Fatalf("Fatal error arguments: %s \n", err)
return
}
err = viper.ReadInConfig()
if err != nil {
if errors.As(err, &viper.ConfigFileNotFoundError{}) {
// Config file not found, this is not an error.
fmt.Println("Config file not found, using default settings.")
} else {
// Other error, panic.
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
2023-04-29 17:38:21 +00:00
}
}