54 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"log"
 | |
| 	"os"
 | |
| 	"path"
 | |
| 	"path/filepath"
 | |
| 	"strings"
 | |
| 
 | |
| 	"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)
 | |
| 	// 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()
 | |
| 	err = viper.ReadInConfig()
 | |
| 
 | |
| 	// 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"))
 | |
| 		}
 | |
| 
 | |
| 	}
 | |
| 
 | |
| }
 |