This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
157 lines (132 loc) · 3.95 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/plugins/cors"
"github.com/davecgh/go-spew/spew"
"github.com/fsnotify/fsnotify"
"github.com/giovannicammarata/gc-simple-home/configurator"
"github.com/giovannicammarata/gc-simple-home/controllers"
"github.com/giovannicammarata/gc-simple-home/models"
"log"
"os"
"strings"
)
var config *models.SystemConfiguration
var configFile string
func main() {
dir, _ := os.Getwd()
log.Println("working dir is ", dir)
argsWithoutProg := os.Args[1:]
configFile = "conf/configuration.json"
if len(argsWithoutProg) > 0 {
configFile = argsWithoutProg[0]
}
// Authorization filter
var authFilter = func(ctx *context.Context) {
if strings.TrimSpace(config.Network.Token) != "" { // Check authorization
var token string
if token = ctx.Input.Header("Authorization"); token == "" { // Fetch the token from the header
body := ctx.Input.RequestBody
var request models.AuthorizedRequest
_ = json.Unmarshal(body, &request)
token = request.Token // Fetch the token from the body
}
if strings.Compare(config.Network.Token, token) != 0 {
log.Println("received not authorized request from ", ctx.Request.RemoteAddr, "request was:", string(ctx.Input.RequestBody))
ctx.Abort(404, "")
}
}
}
// setup the beego router
beego.InsertFilter("/*", beego.BeforeRouter, authFilter)
beego.Router("/", &controllers.DomoticaController{})
loadConfiguration()
go startFileWatcher()
// Enable HTTP/HTTPs
if config.Network.HTTPPort > 0 {
beego.BConfig.Listen.HTTPPort = config.Network.HTTPPort
}
if config.Network.HTTPSPort > 0 {
beego.BConfig.Listen.EnableHTTPS = true
beego.BConfig.Listen.HTTPSPort = config.Network.HTTPSPort
beego.BConfig.Listen.HTTPSCertFile = config.Network.HTTPSCertFile
beego.BConfig.Listen.HTTPSKeyFile = config.Network.HTTPSKeyFile
}
beego.BConfig.CopyRequestBody = true
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin"},
AllowCredentials: true,
}))
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
if len(config.Network.Token) > 0 {
log.Println("enable authorization with token ", config.Network.Token)
}
controllers.Config = &config.Domotica
beego.Run() // start the router
}
func startFileWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("error starting the watcher", err)
os.Exit(2)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case _, ok := <-watcher.Events:
if !ok {
return
}
loadConfiguration()
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error in watcher:", err)
}
}
}()
err = watcher.Add(configFile)
if err != nil {
log.Fatal("error starting the watcher", err)
}
<-done
}
func loadConfiguration() {
log.Println("loading the configuration from", configFile)
// load configuration from file
configRaw := models.SystemConfiguration{}
err := configurator.LoadConfiguration(configFile, &configRaw)
if err != nil && config == nil {
log.Println("unable to load the configuration. Exiting")
os.Exit(1)
}
m2 := make(map[string]models.EntityConfiguration)
entities := configRaw.Domotica.Entities
for k, v := range *entities {
env := v.Env
m := make(map[string]map[string]string)
for k2, v2 := range env {
keys := strings.Split(k2, "|")
for _, key := range keys {
m[key] = v2
}
}
env = m
m2[k] = models.EntityConfiguration{Env: env, Commands: v.Commands}
}
configRaw.Domotica.Entities = &m2
config = &configRaw
log.Println("loaded config")
spew.Dump(config)
}