blob: 4b5ecf05af1d9c00ec8f78916b261f20fd7a3f1f (
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
55
56
57
58
59
60
61
62
63
64
 | package main
import (
	"encoding/json"
	"flag"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
)
type RuntimeConfig struct {
	Path     string
	Http     HttpConfig     `json:"http"`
	Database DatabaseConfig `json:"database"`
}
type HttpConfig struct {
	Host      string `json:"bind_host"`
	Port      string `json:"bind_port"`
	Static    string `json:"static"`
	Templates string `json:"templates"`
	Storage   string `json:"storage"`
}
type DatabaseConfig struct {
	Socket     string `json:"socket"`
	User       string `json:"user"`
	Database   string `json:"database"`
	Migrations string `json:"migrations"`
}
func GetRuntimeConfig() RuntimeConfig {
	config := RuntimeConfig{}
	flag.StringVar(&config.Path, "c", "/etc/ceres/config.json",
		"Path to ceres configuration file")
	flag.Parse()
	configFile, err := os.Open(config.Path)
	defer configFile.Close()
	if err != nil {
		log.Fatalf("Could not open configuration file %s", config.Path)
	}
	configData, err := ioutil.ReadAll(configFile)
	if err != nil {
		log.Fatalf("Could not read configuration file %s", config.Path)
	}
	err = json.Unmarshal(configData, &config)
	if err != nil {
		log.Fatalf("Could not parse configuration file %s", config.Path)
	}
	abs, err := filepath.Abs(config.Path)
	if err != nil {
		log.Fatalf("Could not translate %s to absolute path.", config.Path)
	}
	log.Printf("Config file: %s\n", abs)
	return config
}
 |