-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (90 loc) · 2.58 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"github.com/golang/glog"
"github.com/binchencoder/gateway-proto/data"
"github.com/binchencoder/janus-gateway/gateway/runtime"
"github.com/binchencoder/janus-gateway/integrate"
"github.com/binchencoder/janus-gateway/util"
"github.com/binchencoder/letsgo"
"github.com/binchencoder/letsgo/service/naming"
)
var (
host = flag.String("host", "", "The janus-gateway service host ")
port = flag.Int("port", 8080, "The janus-gateway service port")
enableHTTPS = flag.Bool("enable-https", false, "Whether to enable https.")
certFile = flag.String("cert-file", "", "The TLS cert file.")
keyFile = flag.String("key-file", "", "The TLS key file.")
)
func usage() {
fmt.Println(`Janus Gateway - Universal Gateway of xxx Inc.
Usage:
janus-gateway [options]
Options:`)
flag.PrintDefaults()
os.Exit(2)
}
func checkFlags() {
if *enableHTTPS {
if *certFile == "" {
fmt.Println("Flag --cert-file is required to enable HTTPS.")
os.Exit(2)
}
if *keyFile == "" {
fmt.Println("Flag --key-file is required to enable HTTPS.")
os.Exit(2)
}
}
}
func startHTTPGateway(mux *runtime.ServeMux, hostPort string) {
if err := http.ListenAndServe(hostPort, integrate.HttpMux(mux)); err != nil {
glog.Errorf("Start http gateway error: %v", err)
shutdown()
panic(err)
}
}
func startHTTPSGateway(mux *runtime.ServeMux, hostPort string) {
if err := http.ListenAndServeTLS(hostPort, *certFile, *keyFile, integrate.HttpMux(mux)); err != nil {
glog.Errorf("Start https gateway error: %v", err)
shutdown()
panic(err)
}
}
func main() {
defer letsgo.Cleanup()
letsgo.Init(letsgo.FlagUsage(usage))
checkFlags()
hostPort := fmt.Sprintf("%s:%d", *host, *port)
runtime.CallerServiceId = data.ServiceId_JANUS_GATEWAY
serviceName, err := naming.ServiceIdToName(runtime.CallerServiceId)
if err != nil {
glog.Errorf("Invalid service id %d", runtime.CallerServiceId)
panic(err)
}
util.Logf(util.DefaultLogger, "*****%s init.*****", serviceName)
// 为了开发测试方便,支持flag传参.
if *port > 0 {
hostPort = fmt.Sprintf("%s:%d", *host, *port)
}
mux := runtime.NewServeMux()
runtime.SetGatewayServiceHook(integrate.NewGatewayHook(mux, hostPort))
util.Logf(util.DefaultLogger, "*****Starting %s at %s.*****", serviceName, hostPort)
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
if *enableHTTPS {
go startHTTPSGateway(mux, hostPort)
} else {
go startHTTPGateway(mux, hostPort)
}
select {
case <-signals:
shutdown()
}
}
func shutdown() {
util.Flush()
}