aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 c0b1d23..b7c6e49 100644
--- a/main.go
+++ b/main.go
@@ -2,12 +2,13 @@ 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
@@ -18,8 +19,8 @@ func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
18} 19}
19 20
20func main() { 21func main() {
21 // Datenbankverbindung aufbauen 22 config := LoadConfig()
22 db, err := sql.Open("postgres", "host=grilist.moe user=grilist dbname=grilist password=grilist sslmode=disable") 23 db, err := sql.Open("postgres", config.DBConnectionString())
23 if err != nil { 24 if err != nil {
24 log.Fatal(err) 25 log.Fatal(err)
25 } 26 }