portal/config/config.go

60 lines
1.4 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"
)
var (
ConfigFilePaths = []string{
"/etc/lumeweb/portal/",
"$HOME/.lumeweb/portal/",
".",
}
)
2023-04-29 17:38:21 +00:00
func Init() {
viper.SetConfigName("config")
viper.SetConfigType("json")
for _, path := range ConfigFilePaths {
viper.AddConfigPath(path)
}
2023-04-29 17:38:21 +00:00
viper.SetEnvPrefix("LUME_WEB_PORTAL")
2023-06-06 20:35:58 +00:00
viper.AutomaticEnv()
2023-04-29 17:38:21 +00:00
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")
pflag.String("renterd-api-password", ".", "admin password for renterd")
2023-06-06 20:37:22 +00:00
pflag.Bool("debug", false, "enable debug mode")
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
}
}