-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmain.go
71 lines (60 loc) · 1.49 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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
_ "golang.org/x/net/trace"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/tmc/grpc-websocket-proxy/examples/cmd/wsechoserver/echoserver"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
grpcAddr = flag.String("grpcaddr", ":8001", "listen grpc addr")
httpAddr = flag.String("addr", ":8000", "listen http addr")
debugAddr = flag.String("debugaddr", ":8002", "listen debug addr")
)
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := listenGRPC(*grpcAddr); err != nil {
return err
}
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := echoserver.RegisterEchoServiceHandlerFromEndpoint(ctx, mux, *grpcAddr, opts)
if err != nil {
return err
}
go http.ListenAndServe(*debugAddr, nil)
fmt.Println("listening")
http.ListenAndServe(*httpAddr, wsproxy.WebsocketProxy(mux))
return nil
}
func listenGRPC(listenAddr string) error {
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
return err
}
grpcServer := grpc.NewServer()
echoserver.RegisterEchoServiceServer(grpcServer, &Server{})
go func() {
if err := grpcServer.Serve(lis); err != nil {
log.Println("serveGRPC err:", err)
}
}()
return nil
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}