-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.53 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
package main
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
pb "grpcdemo/proto"
"grpcdemo/server"
"log"
"net/http"
"strings"
)
const PORT = "8888"
func main() {
err := RunServer()
if err != nil {
panic(err)
}
}
func RunServer() error {
httpMux := runHttpServer()
grpcS := runGrpcServer()
gatewayMux := runGatewayServer()
httpMux.Handle("/", gatewayMux)
return http.ListenAndServe(":"+PORT, grpcHandlerFunc(grpcS, httpMux))
}
func runGrpcServer() *grpc.Server {
s := grpc.NewServer()
pb.RegisterHelloServer(s, server.NewHelloServer())
reflection.Register(s)
return s
}
func runHttpServer() *http.ServeMux {
s := http.NewServeMux()
return s
}
func runGatewayServer() *runtime.ServeMux {
endpoint := ":" + PORT
gwmux := runtime.NewServeMux()
options := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := pb.RegisterHelloHandlerFromEndpoint(context.Background(), gwmux, endpoint, options)
if err != nil {
log.Fatal(err)
}
return gwmux
}
func grpcHandlerFunc(grpcServer *grpc.Server, httpHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
httpHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
}