-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
37 lines (36 loc) · 1.17 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
package main
import (
"fmt"
"github.com/spf13/viper"
"go-websocket-cluster/service"
"log"
"net/http"
)
func main() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
port := viper.GetString("server.port")
redisHost := viper.GetString("redis.host")
redisPort := viper.GetString("redis.port")
redisPassword := viper.GetString("redis.password")
redisDB := viper.GetInt("redis.db")
redisMessageChannel := viper.GetString("message.channel")
redisAddr := redisHost+":"+redisPort
redisService := service.NewRedisService(redisAddr, redisPassword, redisDB)
hubService := service.NewHubService(redisService)
go hubService.SubscribeMessage(redisMessageChannel)
go hubService.Run()
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
service.ServeWs(hubService, writer, request, redisMessageChannel)
})
fmt.Println("server run on port:"+port)
err = http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}