-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (99 loc) · 2.72 KB
/
main.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/alext/heating-controller/config"
"github.com/alext/heating-controller/controller"
"github.com/alext/heating-controller/metrics"
"github.com/alext/heating-controller/webserver"
)
const (
defaultConfigFile = "./config.json"
defaultDataDir = "./data"
defaultTemplateDir = "webserver/templates"
)
type ZoneAdder interface {
AddZone(*controller.Zone)
}
func main() {
var (
logDest = flag.String("log", "STDERR", "Where to log to - STDOUT, STDERR or a filename")
dataDir = flag.String("datadir", filepath.FromSlash(defaultDataDir), "The directory to save state information in")
templateDir = flag.String("templatedir", filepath.FromSlash(defaultTemplateDir), "The directory containing the templates")
configFile = flag.String("config-file", filepath.FromSlash(defaultConfigFile), "Path to the config file")
returnVersion = flag.Bool("version", false, "Return version and exit")
)
flag.Parse()
if *returnVersion {
fmt.Printf("heating-controller %s\n", versionInfo())
os.Exit(0)
}
err := setupLogging(*logDest)
if err != nil {
log.Fatal(err)
}
log.Println("[main] heating-controller starting")
config, err := loadConfigFile(*configFile)
if err != nil {
log.Fatalln("[main] Error reading config file:", err)
}
setupDataDir(*dataDir)
ctrl := controller.New()
err = ctrl.Setup(config)
if err != nil {
log.Fatalln("[main] Error setting up controller:", err)
}
m := metrics.New(ctrl)
m.AddInfo(version)
srv := webserver.New(ctrl, config.Port, filepath.FromSlash(*templateDir), m.Handler())
err = srv.Run()
if err != nil {
log.Fatalln("[main] Server.Run:", err)
}
}
func setupLogging(destination string) error {
switch destination {
case "STDERR":
log.SetOutput(os.Stderr)
case "STDOUT":
log.SetOutput(os.Stdout)
default:
file, err := os.OpenFile(destination, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("Error opening log %s: %s", destination, err.Error())
}
log.SetOutput(file)
}
return nil
}
func loadConfigFile(filename string) (*config.Config, error) {
file, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
log.Printf("[main] Config file '%s' not found, ignoring", filename)
return config.New(), nil
}
return nil, err
}
defer file.Close()
return config.LoadConfig(file)
}
func setupDataDir(dir string) {
controller.DataDir = dir
fi, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(dir, 0777)
if err == nil {
return
}
}
log.Fatalf("[main] Error using data dir '%s': %s", dir, err.Error())
}
if !fi.IsDir() {
log.Fatalf("[main] Error, data dir '%s' is not a directory", dir)
}
}