-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
74 lines (62 loc) · 1.67 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
72
73
74
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rs/cors"
"github.com/uchijo/walica-clone-backend/data"
"github.com/uchijo/walica-clone-backend/presenter"
apipb "github.com/uchijo/walica-clone-backend/proto/proto/api"
"github.com/uchijo/walica-clone-backend/util"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func init() {
util.LoadEnv()
util.ConnectToDB()
}
func main() {
lis, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalln("faled to listen:", err)
}
s := grpc.NewServer()
repository := data.RepositoryImpl{}
apipb.RegisterWalicaCloneApiServer(s, presenter.NewServer(repository))
log.Println("serving gRPC on 0.0.0.0:8080")
go func() {
log.Fatalln(s.Serve(lis))
}()
conn, err := grpc.DialContext(
context.Background(),
"0.0.0.0:8080",
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatalln("Failed to dial server:", err)
}
gwmux := runtime.NewServeMux()
err = apipb.RegisterWalicaCloneApiHandler(context.Background(), gwmux, conn)
if err != nil {
log.Fatalln("Failed to register gateway:", err)
}
corsOrigin := os.Getenv("CORS_ORIGIN")
if len(corsOrigin) <= 0 {
log.Fatalln("CORS_ORIGIN not set.")
}
withCors := cors.New(cors.Options{
AllowedOrigins: []string{corsOrigin},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
}).Handler(gwmux)
gwServer := &http.Server{
Addr: ":8090",
Handler: withCors,
}
log.Println("Serving gRPC-Gateway on http://0.0.0.0:8090")
log.Fatalln(gwServer.ListenAndServe())
}