-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (53 loc) · 2.18 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"
"log"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/knowdateapp/knowdateapp/backend/gateway/internal/pb/knowledge/api/v1/collection"
"github.com/knowdateapp/knowdateapp/backend/gateway/internal/pb/knowledge/api/v1/knowledge_base"
"github.com/knowdateapp/knowdateapp/backend/gateway/internal/pb/knowledge/api/v1/topic"
)
func main() {
const (
ServerAddressHTTP = ":80"
)
var (
knowledgeServiceAddressGRPC = flag.String("knowledge-service-address", ":8012", "Knowledge Base service address")
userServiceAddressGRPC = flag.String("user-service-address", ":8022", "User service address")
_ = userServiceAddressGRPC
)
flag.Parse()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
options := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
services := []RegisterFunc{
CaptureRegisterFuncWithEndpoint(*knowledgeServiceAddressGRPC, knowledge_base.RegisterKnowledgeBaseServiceHandlerFromEndpoint),
CaptureRegisterFuncWithEndpoint(*knowledgeServiceAddressGRPC, collection.RegisterCollectionServiceHandlerFromEndpoint),
CaptureRegisterFuncWithEndpoint(*knowledgeServiceAddressGRPC, topic.RegisterTopicServiceHandlerFromEndpoint),
}
for _, registerFunc := range services {
err := registerFunc(ctx, mux, options)
if err != nil {
log.Fatalln("Can't start application:", err)
}
}
// Start application.
log.Println("Gateway server startup on port:", ServerAddressHTTP)
err := http.ListenAndServe(ServerAddressHTTP, mux)
if err != nil {
log.Fatalln("Can't start application:", err)
}
}
type RegisterFuncWithEndpoint = func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) error
type RegisterFunc = func(context.Context, *runtime.ServeMux, []grpc.DialOption) error
func CaptureRegisterFuncWithEndpoint(endpoint string, register RegisterFuncWithEndpoint) RegisterFunc {
return func(ctx context.Context, mux *runtime.ServeMux, options []grpc.DialOption) error {
return register(ctx, mux, endpoint, options)
}
}