-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdojohub.go
70 lines (61 loc) · 1.86 KB
/
dojohub.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
package main
import (
"encoding/base64"
"flag"
"github.com/golang/glog"
"github.com/spastorelli/dojohub/components"
"github.com/spastorelli/dojohub/handlers"
"net/http"
"os"
"os/signal"
"syscall"
)
// Global variables to hold flags values.
var (
host string
port int
staticDir string
tlsCertFile, tlsPrivateKeyFile string
)
// handleTermination handles gracefully the termination via SIGINT, SIGTERM of the server.
func handleTermination() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-signals
glog.Infof("Signal %v intercepted. Exiting...", sig)
glog.Flush()
os.Exit(1)
}()
}
func init() {
flag.IntVar(&port, "port", 8080, "HTTP server port")
flag.StringVar(&host, "host", "", "HTTP server host")
flag.StringVar(&staticDir, "staticDir", "static/", "The static files directories")
flag.StringVar(
&tlsCertFile, "tlsCertFile", "",
"The file that contains the TLS/SSL certificate for the server.")
flag.StringVar(
&tlsPrivateKeyFile, "tlsPrivateKeyFile", "",
"The file that contains the TLS/SSL private key for the server.")
}
func main() {
flag.Parse()
handleTermination()
encSecret := base64.URLEncoding.EncodeToString([]byte(os.Getenv("APP_SECRET")))
chatExampleApp := components.NewApplication(
os.Getenv("APP_ID"),
"ChatExampleApp",
encSecret,
)
msgHub := components.NewMsgHub()
msgHub.RegisterApplication(chatExampleApp)
msgHub.Run()
app := components.NewAppServer(&host, &port, &staticDir, &tlsCertFile, &tlsPrivateKeyFile)
app.RegisterHandler("/", http.HandlerFunc(handlers.Home))
app.RegisterHandler("/example/chat/", http.HandlerFunc(handlers.ExampleChat))
app.RegisterHandler("/ws/", msgHub)
if err := app.Serve(); err != nil {
glog.Error("Error while starting the DojoHub: ", err)
}
}