-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
51 lines (47 loc) · 1.19 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
package main
import (
"net/http"
"os"
_ "github.com/joho/godotenv/autoload"
log "github.com/sirupsen/logrus"
)
var (
address = os.Getenv("WEBSOCKET_HOST") + ":" + os.Getenv("WEBSOCKET_PORT")
wsPath = os.Getenv("WEBSOCKET_PATH")
tlsEnabled = os.Getenv("TLS_ENABLED")
tlsKeyFile = os.Getenv("TLS_KEY_FILE")
tlsCertFile = os.Getenv("TLS_CERT_FILE")
)
func main() {
// 启动redis
go SubscribeChannel()
go func() {
for message := range SubscribeMessages {
// 收到消息进行推送
go ChannelsRegister.Broadcast(message)
}
}()
// 数据定时输出
go metrics.Report()
// 心跳
go HeartbeatTimer()
// 启动http服务
log.Info(address + wsPath)
http.HandleFunc(wsPath, func(w http.ResponseWriter, r *http.Request) {
NewWebsocket(w, r)
})
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(metrics.GetJson()))
})
var err error
if tlsEnabled == "true" {
// HTTPS
err = http.ListenAndServeTLS(address, tlsCertFile, tlsKeyFile, nil)
} else {
err = http.ListenAndServe(address, nil)
}
if err != nil {
log.WithField("address", address).Fatal("ListenAndServe:", err)
}
}