-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (57 loc) · 1.68 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
package main
import (
"context"
"fmt"
"log"
"net"
_ "embed"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
usersvcv1 "github.com/mlukasik-dev/usersvc/gen/go/usersvc/v1"
"github.com/mlukasik-dev/usersvc/internal/appconfig"
"github.com/mlukasik-dev/usersvc/internal/controller"
"github.com/mlukasik-dev/usersvc/internal/events"
"github.com/mlukasik-dev/usersvc/internal/store"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
//go:embed configs/config.yaml
var configFile []byte
func main() {
if err := appconfig.Init(configFile); err != nil {
log.Fatal(err)
}
logger, err := zap.NewProduction()
if err != nil {
log.Fatal(err)
}
defer logger.Sync()
client, err := store.Connect(appconfig.AppConfig.Mongodb.URI)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.Background())
s := store.New(client)
if err := s.CreateIndexes(context.Background()); err != nil {
log.Fatal(err)
}
e := events.New()
ctr := controller.New(s, logger, e)
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", appconfig.AppConfig.Port))
if err != nil {
log.Fatal(err)
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_recovery.UnaryServerInterceptor(),
grpc_zap.UnaryServerInterceptor(logger),
)),
)
usersvcv1.RegisterServiceServer(grpcServer, ctr)
// setup reflection so evens-cli REPL mode can be used for testing.
reflection.Register(grpcServer)
fmt.Printf("Listening at %s\n", lis.Addr().String())
log.Fatal(grpcServer.Serve(lis))
}