-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (47 loc) · 1.24 KB
/
main.go
File metadata and controls
60 lines (47 loc) · 1.24 KB
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
package main
import (
"context"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/Junedayday/grpc-gateway-buf-example/idl/proto"
"github.com/Junedayday/grpc-gateway-buf-example/internal/server"
)
const (
gRPCEndPoint = ":8080"
gRPCGatewayEndPoint = ":8082"
)
func main() {
go runGRPC()
if err := runHTTP(); err != nil {
panic(err)
}
}
func runGRPC() {
lis, err := net.Listen("tcp", gRPCEndPoint)
if err != nil {
panic(err)
}
s := grpc.NewServer()
proto.RegisterEchoServiceServer(s, &server.Server{})
if err := s.Serve(lis); err != nil {
panic(err)
}
}
func runHTTP() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// gRPC-Gateway mux
gwMux := runtime.NewServeMux()
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
if err := proto.RegisterEchoServiceHandlerFromEndpoint(ctx, gwMux, gRPCEndPoint, opts); err != nil {
return errors.Wrap(err, "RegisterEchoServiceHandlerFromEndpoint error")
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
return http.ListenAndServe(gRPCGatewayEndPoint, gwMux)
}