-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
main.go
147 lines (123 loc) · 3.28 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
136
137
138
139
140
141
142
143
144
145
146
147
// Package cmd contains an entrypoint for running an ion-sfu instance.
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
log "github.com/pion/ion-log"
pb "github.com/pion/ion-sfu/cmd/signal/grpc/proto"
"github.com/pion/ion-sfu/cmd/signal/grpc/server"
"github.com/pion/ion-sfu/pkg/sfu"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
type grpcConfig struct {
Port string `mapstructure:"port"`
}
// Config defines parameters for configuring the sfu instance
type Config struct {
sfu.Config `mapstructure:",squash"`
GRPC grpcConfig `mapstructure:"grpc"`
}
var (
conf = Config{}
file string
addr string
metricsAddr string
)
const (
portRangeLimit = 100
)
func showHelp() {
fmt.Printf("Usage:%s {params}\n", os.Args[0])
fmt.Println(" -c {config file}")
fmt.Println(" -a {listen addr}")
fmt.Println(" -h (show help info)")
}
func load() bool {
_, err := os.Stat(file)
if err != nil {
return false
}
viper.SetConfigFile(file)
viper.SetConfigType("toml")
err = viper.ReadInConfig()
if err != nil {
fmt.Printf("config file %s read failed. %v\n", file, err)
return false
}
err = viper.GetViper().Unmarshal(&conf)
if err != nil {
fmt.Printf("sfu config file %s loaded failed. %v\n", file, err)
return false
}
if len(conf.WebRTC.ICEPortRange) > 2 {
fmt.Printf("config file %s loaded failed. range port must be [min,max]\n", file)
return false
}
if len(conf.WebRTC.ICEPortRange) != 0 && conf.WebRTC.ICEPortRange[1]-conf.WebRTC.ICEPortRange[0] < portRangeLimit {
fmt.Printf("config file %s loaded failed. range port must be [min, max] and max - min >= %d\n", file, portRangeLimit)
return false
}
fmt.Printf("config %s load ok!\n", file)
return true
}
func parse() bool {
flag.StringVar(&file, "c", "config.toml", "config file")
flag.StringVar(&addr, "a", ":50051", "address to use")
flag.StringVar(&metricsAddr, "m", ":8100", "merics to use")
help := flag.Bool("h", false, "help info")
flag.Parse()
if !load() {
return false
}
if *help {
return false
}
return true
}
func startMetrics(addr string) {
// start metrics server
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Handler: m,
}
metricsLis, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("cannot bind to metrics endpoint %s. err: %s", addr, err)
}
log.Infof("Metrics Listening at %s", addr)
err = srv.Serve(metricsLis)
if err != nil {
log.Errorf("debug server stopped. got err: %s", err)
}
}
func main() {
if !parse() {
showHelp()
os.Exit(-1)
}
fixByFile := []string{"asm_amd64.s", "proc.go", "icegatherer.go"}
fixByFunc := []string{}
log.Init(conf.Log.Level, fixByFile, fixByFunc)
log.Infof("--- Starting SFU Node ---")
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("failed to listen: %v", err)
}
s := grpc.NewServer(
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
)
pb.RegisterSFUServer(s, server.NewServer(sfu.NewSFU(conf.Config)))
grpc_prometheus.Register(s)
go startMetrics(metricsAddr)
log.Infof("SFU Listening at %s", addr)
if err := s.Serve(lis); err != nil {
log.Panicf("failed to serve: %v", err)
}
}