-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (59 loc) · 1.8 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
package main
import (
"context"
"fmt"
cosmetic "github.com/JAY-Chan9yu/go-gin-gam-rae/apps/cosmetic"
hello "github.com/JAY-Chan9yu/go-gin-gam-rae/apps/hello"
cosmeticPb "github.com/JAY-Chan9yu/go-gin-gam-rae/proto/cosmetic"
helloPb "github.com/JAY-Chan9yu/go-gin-gam-rae/proto/hello"
_ "github.com/go-sql-driver/mysql"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"log"
"net"
"net/http"
)
func main() {
// TCP 소켓 열기
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 9000))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
cosmeticPb.RegisterCosmeticServiceServer(s, &cosmetic.Cosmetic{})
helloPb.RegisterGreeterServer(s, &hello.Hello{})
/****************************
Serving gRPC
****************************/
log.Println("Serving gRPC on 127.0.0.1:9000")
go func() {
log.Fatalln(s.Serve(lis))
}()
conn, err := grpc.DialContext(
context.Background(),
"127.0.0.1:9000",
grpc.WithBlock(),
grpc.WithInsecure(),
)
if err != nil {
log.Fatalln("Failed to dial server:", err)
}
gwmux := runtime.NewServeMux() // mux.Handle에 gwmux를 등록시켜서 rest 요청이 들어올 때 마다 proto에 작성해준 요청에 맞게 연결
err = cosmeticPb.RegisterCosmeticServiceHandler(context.Background(), gwmux, conn)
if err != nil {
log.Fatalln("Failed to register Cosmetic Proto gateway:", err)
}
err = helloPb.RegisterGreeterHandler(context.Background(), gwmux, conn)
if err != nil {
log.Fatalln("Failed to register Hello Proto gateway:", err)
}
/****************************
Serving gRPC-Gateway
****************************/
gwServer := &http.Server{
Addr: ":8090",
Handler: gwmux,
}
log.Println("Serving gRPC-Gateway on http://127.0.0.1:8090")
log.Fatalln(gwServer.ListenAndServe())
}