-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (56 loc) · 1.48 KB
/
main.go
File metadata and controls
71 lines (56 loc) · 1.48 KB
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
package main
import (
"HouseController/alarm"
"HouseController/light"
"HouseController/tools"
"fmt"
"io"
"log"
"net/http"
"time"
)
// muxer for the hhtp server, its a blobal
var mux map[string]func(http.ResponseWriter, *http.Request)
// Hello function only exists for debug
func Hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
fmt.Println("we arrive on hello")
}
func ConfigServer(settings tools.Config) (*http.Server, error) {
fmt.Printf("Current Unix Time: %v\n", time.Now().Unix())
server := http.Server{
Addr: settings.HttpPort,
Handler: &myHandler{},
}
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/"] = Hello
mux["/alarm"] = alarm.Alarm
mux["/light"] = light.GetInstance().Light //light.Light
return &server, nil
}
func main() {
instanceTools := tools.GetInstance()
settings, err := instanceTools.LoadConfiguration(tools.SettingsFilename)
if err != nil || settings == nil {
log.Fatalf("Fail to open settings file: %v", err)
return
}
//nitializes the serial port
light.GetInstance().Initialize(settings.SerialPort)
httpServer, err := ConfigServer(*settings)
if err != nil {
log.Fatalf("Fail to open the HTTP server: %v", err)
return
}
//Blocking call
httpServer.ListenAndServe()
}
type myHandler struct{}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h, ok := mux[r.URL.String()]
if ok != false {
h(w, r)
return
}
io.WriteString(w, "My server: "+r.URL.String())
}