-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
77 lines (63 loc) · 1.99 KB
/
server.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
75
76
77
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rulanugrh/saturnus/configs"
"github.com/rulanugrh/saturnus/helper"
"github.com/rulanugrh/saturnus/pb"
"github.com/rulanugrh/saturnus/repository"
"github.com/rulanugrh/saturnus/services"
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
conf := configs.GetConfig()
dsnGRPC := fmt.Sprintf("%s:%s", conf.Server.Host, conf.Server.GRPCPort)
dsnHTTP := fmt.Sprintf("%s:%s", conf.Server.Host, conf.Server.HTTPort)
listener, err := net.Listen("tcp", dsnGRPC)
if err != nil {
helper.PrintError(err)
}
// Create COLLECTION and repository for services
var todoColl *mongo.Collection = configs.MongoColl("todo", configs.DB)
todoRepository := repository.TodoRepository(todoColl)
srv := services.NewGrpcPost(todoRepository, todoColl)
// Starting grpc serevr
serv := grpc.NewServer()
pb.RegisterTodoServiceServer(serv, srv)
go func() {
log.Fatalln(serv.Serve(listener))
}()
log.Printf("GRPC Server running on %s:%s", conf.Server.Host, conf.Server.GRPCPort)
// create grpc connection for http
maxMsgSize := 1024 * 1024 * 20
connGrpc, errCon := grpc.DialContext(
context.Background(),
dsnGRPC,
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize), grpc.MaxCallSendMsgSize(maxMsgSize)),
)
if errCon != nil {
helper.PrintError(errCon)
}
// server for running http server
srvHttp := runtime.NewServeMux()
errHttp := pb.RegisterTodoServiceHandler(context.Background(), srvHttp, connGrpc)
if errHttp != nil {
helper.PrintError(errHttp)
}
srvHttpServ := &http.Server {
Addr: dsnHTTP,
Handler: srvHttp,
}
log.Printf("HTTP Server running on %s:%s", conf.Server.Host, conf.Server.HTTPort)
if errServHttp := srvHttpServ.ListenAndServe(); errServHttp != nil {
helper.PrintError(errServHttp)
}
}