aboutsummaryrefslogtreecommitdiff
path: root/src/conf/conf.go
blob: 8abb8a82d5748a4366642c4b387b6a333a2a5dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package conf

import (
	"log"
	"os"

	"github.com/BurntSushi/toml"
)

type (
	Conf struct {
		Db     Db     `toml:"database"`
		Server Server `toml:"server"`
	}

	Db struct {
		Adapter  string `toml:"adapter"`
		User     string `toml:"user"`
		Password string `toml:"pass"`
	}

	Server struct {
		Hostname     string `toml:"hostname"`
		Port         string `toml:"port"`
		TemplatePath string `toml:"template_path"`
	}
)

func NewConf() *Conf {

	workingdir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	filepath := os.Getenv("NEWSSTAND_CONFIG_PATH")
	if filepath == "" {
		filepath = workingdir + "/.newsstandrc.toml"
	}
	log.Printf("Config file path: %s", filepath)

	c := Conf{
		Db{},
		Server{
			TemplatePath: workingdir + "/views",
		},
	}
	_, err = toml.DecodeFile(filepath, &c)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("Config loaded: %s", c)

	return &c
}