-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 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
package main
import (
"context"
"flag"
"fmt"
"github.com/ghfli/gym-jinni/services/gen/go/usermgt/v1"
"github.com/ghfli/gym-jinni/services/usermgt"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"net"
"net/http"
)
func main() {
flag.Parse()
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
go runGRPCServer()
return runGatewayServer()
}
var (
grpcServerEndpoint = flag.String("grpc-server-endpoint",
"127.0.0.1:8080", "gRPC server endpoint")
)
func runGRPCServer() error {
listener, err := net.Listen("tcp", *grpcServerEndpoint)
if err != nil {
return fmt.Errorf("failed to listen on %s: %w",
*grpcServerEndpoint, err)
}
server := grpc.NewServer()
usermgtv1.RegisterUserMgtServiceServer(server, &usermgt.ImUserMgtServiceServer{})
log.Println("gRPC server listening on", *grpcServerEndpoint)
if err := server.Serve(listener); err != nil {
return fmt.Errorf("failed to serve gRPC server: %w", err)
}
return nil
}
func runGatewayServer() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := usermgtv1.RegisterUserMgtServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
if err != nil {
return err
}
log.Println("gRPC gateway server listening on :8081")
return http.ListenAndServe(":8081", mux)
}