-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
135 lines (111 loc) · 2.95 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"github.com/gomodule/redigo/redis"
"google.golang.org/grpc"
"github.com/praveen001/go-rtmp-grpc/pkg/api/v1"
"github.com/praveen001/go-rtmp-web-server/controllers"
"github.com/praveen001/go-rtmp-web-server/models"
"github.com/praveen001/go-rtmp-web-server/router"
"github.com/praveen001/go-rtmp-web-server/rtmp"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
switch os.Args[1] {
case "rtmp":
rtmpServer()
case "web":
webServer()
default:
fmt.Println("Server mode should be streamer or web")
}
}
func rtmpServer() {
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", os.Getenv("GRPC_HOST"), "4005"), grpc.WithInsecure())
if err != nil {
fmt.Println("GRPC Connection Error")
return
}
rpcClient := v1.NewUserChannelServiceClient(conn)
redisClient, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), "6379"))
if err != nil {
panic("Unable to connect to Redis" + err.Error())
}
ctx := &rtmp.StreamerContext{
RPC: rpcClient,
Redis: redisClient,
}
rtmp.InitServer(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block till we receive an interrupt
<-c
os.Exit(0)
}
func webServer() {
controllers.InitYoutube()
controllers.InitTwitch()
fmt.Println(os.Environ())
// MySQL DB
db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASSWORD"), os.Getenv("MYSQL_HOST"), os.Getenv("MYSQL_DATABASE")))
if err != nil {
panic("Unable to connect to database " + err.Error())
}
db.AutoMigrate(&models.User{}, &models.Stream{}, &models.Channel{})
defer db.Close()
// Websocket
hub := controllers.NewHub()
go hub.Run()
// Redis PubSub
redisPool := &redis.Pool{
MaxIdle: 80,
MaxActive: 100,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), "6379"))
if err != nil {
panic("Redis connection failed " + err.Error())
}
return c, err
},
}
pubsub := controllers.NewPubSub(redisPool.Get(), "Stream")
go pubsub.Listen(hub)
defer pubsub.Close()
appContext := &controllers.ApplicationContext{
DB: db,
Hub: hub,
PubSub: pubsub,
RedisPool: redisPool,
}
// Grpc
go controllers.NewRPCServer(appContext)
// Create a HTTP Server instance
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:5000"),
Handler: router.New(appContext),
}
// Run the server in a goroutine
go func() {
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}()
// go rtmp.InitServer(db)
fmt.Println("Started server at port 5000")
// Create a channel to listen for OS Interrupts
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block till we receive an interrupt
<-c
// Wait for timeout to complete
ctx, cancel := context.WithTimeout(context.Background(), 30)
defer cancel()
// Shutdown the server
srv.Shutdown(ctx)
os.Exit(0)
}