diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | config.go | 63 | ||||
-rw-r--r-- | main.go | 11 |
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 @@ | |||
1 | grilist.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 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "log" | ||
6 | "os" | ||
7 | |||
8 | "github.com/BurntSushi/toml" | ||
9 | ) | ||
10 | |||
11 | type 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 | |||
22 | func (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 | |||
37 | func 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 | } | ||
@@ -2,18 +2,19 @@ package main | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
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 | ||
14 | func main() { | 15 | func 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 | } |