-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (66 loc) · 1.8 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/nunnatsa/piHatDraw/controller"
"github.com/nunnatsa/piHatDraw/notifier"
"github.com/nunnatsa/piHatDraw/webapp"
)
var (
canvasWidth, canvasHeight uint8
port uint16
)
func init() {
var width, height, prt uint
flag.UintVar(&width, "width", 24, "Canvas width in pixels")
flag.UintVar(&height, "height", 24, "Canvas height in pixels")
flag.UintVar(&prt, "port", 8080, "The application port")
flag.Parse()
if width < 8 {
fmt.Println("The minimum width of the canvas is 8 pixels; setting it for you")
width = 8
}
if width > 40 {
log.Fatal("ERROR: The maximum width of the canvas is 40 pixels")
}
canvasWidth = uint8(width)
if height < 8 {
fmt.Println("The minimum height of the canvas is 8 pixels; setting it for you")
height = 8
}
if height > 40 {
log.Fatal("ERROR: The maximum height of the canvas is 40 pixels")
}
canvasHeight = uint8(height)
port = uint16(prt)
hostname, err := os.Hostname()
if err != nil {
log.Panic(err)
}
fmt.Printf("In your web browser, go to http://%s:%d\n", hostname, port)
}
func main() {
n := notifier.NewNotifier()
defer n.Close()
clientEvents := make(chan webapp.ClientEvent, 1)
defer close(clientEvents)
webApplication := webapp.NewWebApplication(n, clientEvents)
portStr := fmt.Sprintf(":%d", port)
server := http.Server{Addr: portStr, Handler: webApplication.GetMux()}
control := controller.NewController(n, clientEvents, canvasWidth, canvasHeight)
done := control.Start()
go func() {
<-done
if err := server.Shutdown(context.Background()); err != nil {
log.Fatalf("Failed to shutdown the server; %v", err)
}
}()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Panic(err)
}
fmt.Println("\nGood Bye!")
}