/
main.go
67 lines (49 loc) · 1.16 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
package main
import (
"fmt"
"net/http"
"os"
"context"
"github.com/gin-gonic/gin"
)
var (
redisURL = os.Getenv("REDIS_URL")
redisSecretName = os.Getenv("REDIS_SECRET_NAME")
environment = os.Getenv("ENV")
listenAddr = os.Getenv("LISTEN_ADDRESS")
)
func main() {
fmt.Println("Starting looking binormal ...")
ctx := context.TODO()
database, err := newDatabase(ctx, redisURL, redisSecretName)
if err != nil {
fmt.Println("FATAL ERROR: couldn't connect to redis:", err)
}
if listenAddr == "" {
listenAddr = "127.0.0.1:8080"
}
router := initRouter(database)
router.Run(listenAddr)
fmt.Println("Stopped looking binormal ...")
}
func initRouter(database *database) *gin.Engine {
r := gin.Default()
hub := newHub()
go hub.run()
r.LoadHTMLGlob("static/*.html")
r.SetTrustedProxies(nil)
if environment != "dev" {
gin.SetMode(gin.ReleaseMode)
}
r.GET("/ping", pingHandler)
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.POST("/discrete", func(c *gin.Context) {
discreteHandler(c, database, hub)
})
r.GET("/websocket", func(c *gin.Context) {
serveWs(hub, c.Writer, c.Request)
})
return r
}