forked from link1st/gowebsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (66 loc) · 1.68 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
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package main 实现一个基于websocket分布式聊天(IM)系统。
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/link1st/gowebsocket/v2/lib/redislib"
"github.com/link1st/gowebsocket/v2/routers"
"github.com/link1st/gowebsocket/v2/servers/grpcserver"
"github.com/link1st/gowebsocket/v2/servers/task"
"github.com/link1st/gowebsocket/v2/servers/websocket"
)
func main() {
initConfig()
initFile()
initRedis()
router := gin.Default()
// 初始化路由
routers.Init(router)
routers.WebsocketInit()
// 定时任务
task.Init()
// 服务注册
task.ServerInit()
go websocket.StartWebSocket()
// grpc
go grpcserver.Init()
go open()
httpPort := viper.GetString("app.httpPort")
_ = http.ListenAndServe(":"+httpPort, router)
}
// initFile 初始化日志
func initFile() {
// Disable Console Color, you don't need console color when writing the logs to file.
gin.DisableConsoleColor()
// Logging to a file.
logFile := viper.GetString("app.logFile")
f, _ := os.Create(logFile)
gin.DefaultWriter = io.MultiWriter(f)
}
func initConfig() {
viper.SetConfigName("config/app")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
fmt.Println("config app:", viper.Get("app"))
fmt.Println("config redis:", viper.Get("redis"))
}
func initRedis() {
redislib.NewClient()
}
func open() {
time.Sleep(1000 * time.Millisecond)
httpUrl := viper.GetString("app.httpUrl")
httpUrl = "http://" + httpUrl + "/home/index"
fmt.Println("访问页面体验:", httpUrl)
cmd := exec.Command("open", httpUrl)
_, _ = cmd.Output()
}