aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2015-12-19 19:10:42 (UTC)
committerrtz12 <koenig@fagott.pw>2015-12-19 19:10:42 (UTC)
commita4114307d2d2527be6b33f7ef06615764f80a118 (patch)
tree707e6cb07fcea2d3f054e82cbe11fd8c82631073
parent9c05353c64da4ccdf62525986e67c95e482d6071 (diff)
Config hinzugefuegt
-rw-r--r--.gitignore2
-rw-r--r--config.go63
-rw-r--r--main.go11
3 files changed, 71 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3546183
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
1grilist.conf
2
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..65e15ba
--- /dev/null
+++ b/config.go
@@ -0,0 +1,63 @@
1package main
2
3import (
4 "fmt"
5 "log"
6 "os"
7
8 "github.com/BurntSushi/toml"
9)
10
11type Config struct {
12 Database struct {
13 Hostname string `toml:"hostname"`
14 //Port int `toml:"port"`
15 Database string `toml:"database"`
16 User string `toml:"user"`
17 Password string `toml:"password"`
18 SSLEnabled bool `toml:ssl_enabled"`
19 } `toml:"database"`
20}
21
22func (c *Config) DBConnectionString() string {
23 var sslmode string
24 if c.Database.SSLEnabled {
25 sslmode = "enabled"
26 } else {
27 sslmode = "disabled"
28 }
29 return fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=%s",
30 c.Database.Hostname,
31 c.Database.User,
32 c.Database.Database,
33 c.Database.Password,
34 sslmode)
35}
36
37func LoadConfig() Config {
38 var config Config
39 var path string
40 log.Println("Loading config...")
41 for _, v := range []string{
42 "grilist.conf",
43 "~/grilist.conf",
44 "/etc/grilist.conf",
45 } {
46 log.Printf("Try \"%s\"...", v)
47 if _, err := os.Stat(v); err != nil {
48 log.Printf("Loading config at \"%s\" failed!", v)
49 } else {
50 log.Printf("Config file found!")
51 path = v
52 break
53 }
54 }
55 if path == "" {
56 log.Fatal("No config file found at the possible locations!")
57 }
58 if _, err := toml.DecodeFile(path, &config); err != nil {
59 log.Printf("%v", err)
60 log.Fatal("Fatal error while reading the config!")
61 }
62 return config
63}
diff --git a/main.go b/main.go
index cc05c3c..1027abb 100644
--- a/main.go
+++ b/main.go
@@ -2,18 +2,19 @@ package main
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 login "fagott.pw/charakterin"
6 "fagott.pw/grilist/frontend"
7 "github.com/julienschmidt/httprouter"
8 "log" 5 "log"
9 "net/http" 6 "net/http"
10 7
8 login "fagott.pw/charakterin"
9 "fagott.pw/grilist/frontend"
10
11 "github.com/julienschmidt/httprouter"
11 _ "github.com/lib/pq" 12 _ "github.com/lib/pq"
12) 13)
13 14
14func main() { 15func main() {
15 // Datenbankverbindung aufbauen 16 config := LoadConfig()
16 db, err := sql.Open("postgres", "host=grilist.moe user=grilist dbname=grilist password=grilist sslmode=disable") 17 db, err := sql.Open("postgres", config.DBConnectionString())
17 if err != nil { 18 if err != nil {
18 log.Fatal(err) 19 log.Fatal(err)
19 } 20 }