From a4114307d2d2527be6b33f7ef06615764f80a118 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Sat, 19 Dec 2015 20:10:42 +0100 Subject: Config hinzugefuegt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3546183 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +grilist.conf + diff --git a/config.go b/config.go new file mode 100644 index 0000000..65e15ba --- /dev/null +++ b/config.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/BurntSushi/toml" +) + +type Config struct { + Database struct { + Hostname string `toml:"hostname"` + //Port int `toml:"port"` + Database string `toml:"database"` + User string `toml:"user"` + Password string `toml:"password"` + SSLEnabled bool `toml:ssl_enabled"` + } `toml:"database"` +} + +func (c *Config) DBConnectionString() string { + var sslmode string + if c.Database.SSLEnabled { + sslmode = "enabled" + } else { + sslmode = "disabled" + } + return fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=%s", + c.Database.Hostname, + c.Database.User, + c.Database.Database, + c.Database.Password, + sslmode) +} + +func LoadConfig() Config { + var config Config + var path string + log.Println("Loading config...") + for _, v := range []string{ + "grilist.conf", + "~/grilist.conf", + "/etc/grilist.conf", + } { + log.Printf("Try \"%s\"...", v) + if _, err := os.Stat(v); err != nil { + log.Printf("Loading config at \"%s\" failed!", v) + } else { + log.Printf("Config file found!") + path = v + break + } + } + if path == "" { + log.Fatal("No config file found at the possible locations!") + } + if _, err := toml.DecodeFile(path, &config); err != nil { + log.Printf("%v", err) + log.Fatal("Fatal error while reading the config!") + } + return config +} diff --git a/main.go b/main.go index cc05c3c..1027abb 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,19 @@ package main import ( "database/sql" - login "fagott.pw/charakterin" - "fagott.pw/grilist/frontend" - "github.com/julienschmidt/httprouter" "log" "net/http" + login "fagott.pw/charakterin" + "fagott.pw/grilist/frontend" + + "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" ) func main() { - // Datenbankverbindung aufbauen - db, err := sql.Open("postgres", "host=grilist.moe user=grilist dbname=grilist password=grilist sslmode=disable") + config := LoadConfig() + db, err := sql.Open("postgres", config.DBConnectionString()) if err != nil { log.Fatal(err) } -- cgit v0.10.1