-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (104 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/Moranilt/jwt-http2/certs"
"github.com/Moranilt/jwt-http2/clients"
"github.com/Moranilt/jwt-http2/config"
"github.com/Moranilt/jwt-http2/logger"
"github.com/Moranilt/jwt-http2/middleware"
"github.com/Moranilt/jwt-http2/server"
"github.com/Moranilt/jwt-http2/tracer"
grpc_transport "github.com/Moranilt/jwt-http2/transport/grpc"
http_transport "github.com/Moranilt/jwt-http2/transport/http"
"golang.org/x/sync/errgroup"
)
func main() {
log := logger.New()
log.Info("starting application...")
ctx, cancel := context.WithCancel(context.Background())
go func() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
cancel()
}()
env, err := config.ReadEnv()
if err != nil {
log.Fatalf("error while reading env: %v", err)
}
tp, err := tracer.NewProvider(env.Jaeger.URL, env.Jaeger.Name)
if err != nil {
log.Fatal("jaeger: ", err)
}
defer func(ctx context.Context) {
if err := tp.Shutdown(ctx); err != nil {
log.Printf("shutting down tracer provider: %v", err)
}
}(ctx)
vaultClient, err := clients.Vault(env.Vault)
if err != nil {
log.Fatalf("vault client: %v", err)
}
// use only in local or dev modes
if !env.Production {
certGenerator := certs.NewKeys(vaultClient.GetClient(), env.Vault)
err := certGenerator.StoreToVault()
if err != nil {
log.Fatalf("create certificates: %v", err)
}
}
redisCreds, err := vaultClient.GetRedisCreds(ctx)
if err != nil {
log.Fatalf("vault client: %v", err)
}
publicCert, err := vaultClient.GetPublicCert(ctx)
if err != nil {
log.Fatalf("vault public cert: %v", err)
}
privateCert, err := vaultClient.GetPrivateCert(ctx)
if err != nil {
log.Fatalf("vault private cert: %v", err)
}
redis, err := clients.Redis(ctx, redisCreds)
if err != nil {
log.Fatalf("redis client: %v", err)
}
consulClient, err := clients.Consul(ctx, env.Consul)
if err != nil {
log.Fatalf("consul client: %v", err)
}
mainConfig := config.New(log)
err = mainConfig.ReadConsul(ctx, env.Consul.Key(), consulClient)
if err != nil {
log.Fatal("read from consul: ", err)
}
serverREST := http_transport.New(fmt.Sprintf(":%s", env.PortREST), log, mainConfig, env.Consul.Key())
mw := middleware.New(log)
server := server.New(log, mainConfig.App, redis, publicCert, privateCert)
serverGRPC := grpc_transport.New(server, mw)
lis, err := serverGRPC.MakeListener(env.PortGRPC)
if err != nil {
log.Fatal(err)
}
g, gCtx := errgroup.WithContext(ctx)
g.Go(func() error {
<-gCtx.Done()
serverGRPC.GracefulStop()
lis.Close()
serverREST.Shutdown(context.Background())
return fmt.Errorf("shutdown application")
})
g.Go(func() error {
return serverGRPC.Serve(lis)
})
g.Go(func() error {
return serverREST.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Debugf("exit with: %s", err)
}
}