/
cinefade.go
64 lines (52 loc) · 1.23 KB
/
cinefade.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
package main
import (
"cinefade/cinefade"
"flag"
"github.com/blackjack/syslog"
"github.com/stretchr/goweb"
"net"
"net/http"
"os"
"os/signal"
"time"
)
const (
Address string = ":9090"
)
func main() {
syslog.Openlog("cinefade", syslog.LOG_PID, syslog.LOG_USER)
var action string
flag.StringVar(&action, "action", "on", "lights on/off")
flag.Parse()
bridge := cinefade.GetBridge(false)
cinefade.MapRoutes(bridge)
// make a http server using the goweb.DefaultHttpHandler()
s := &http.Server{
Addr: Address,
Handler: goweb.DefaultHttpHandler(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
listener, listenErr := net.Listen("tcp", Address)
syslog.Info("Server cinefade started")
syslog.Infof("visit: %s", Address)
if listenErr != nil {
syslog.Critf("Could not listen: %s", listenErr)
os.Exit(1)
}
go func() {
for _ = range c {
// sig is a ^C, handle it
// stop the HTTP server
syslog.Info("Stopping the cinefade server...")
listener.Close()
syslog.Info("Tearing down...")
os.Exit(0)
}
}()
syslog.Critf("Error in Serve: %s", s.Serve(listener))
os.Exit(1)
}