goipam/config.go

55 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-02-22 10:18:59 +01:00
package main
import (
2024-02-22 20:52:37 +01:00
"fmt"
2024-02-22 10:18:59 +01:00
"log"
"os"
2024-02-22 20:52:37 +01:00
"path"
2024-02-22 10:18:59 +01:00
"path/filepath"
2024-02-22 20:52:37 +01:00
"strings"
2024-02-22 10:18:59 +01:00
"github.com/spf13/viper"
)
func readConfig() {
// configFile
executable, err := os.Executable()
if err != nil {
log.Fatal("err")
}
executableDir := filepath.Dir(executable)
viper.SetConfigType("yaml")
viper.SetConfigName("goipam")
viper.AddConfigPath(executableDir)
viper.AddConfigPath("/etc")
viper.SetDefault("web.host", "0.0.0.0")
viper.SetDefault("web.port", 8080)
viper.SetDefault("web.prod", true)
2024-02-22 20:52:37 +01:00
// Database Config
viper.SetDefault("db.type", "sqlite")
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.user", "dbuser")
viper.SetDefault("db.path", "./db.sqlite")
viper.SetDefault("db.password", "dbpw")
viper.SetDefault("db.port", 5432)
viper.SetDefault("db.sslmode", "disable")
viper.AutomaticEnv()
2024-02-22 10:18:59 +01:00
err = viper.ReadInConfig()
2024-02-22 20:52:37 +01:00
// If a config file is found, read it in.
if err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
} else {
log.Println(err)
errString := err.Error()
if strings.Contains(errString, "Not Found") {
log.Println("could not find config file, creating...")
viper.WriteConfigAs(path.Join(executableDir, "goipam.yaml"))
}
2024-02-22 10:18:59 +01:00
}
}