-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
91 lines (72 loc) · 2.29 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
// Copyright (c) 2019 Leonardo Faoro. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"os"
"time"
"cloud.google.com/go/errorreporting"
"github.com/lfaoro/pkg/logger"
"github.com/lfaoro/spark/internal"
"github.com/lfaoro/spark/internal/user"
"github.com/lfaoro/spark/internal/vault"
"github.com/lfaoro/spark/pkg/iin/binlist"
"github.com/lfaoro/spark/pkg/kms"
"github.com/lfaoro/spark/pkg/kms/aesgcm"
"github.com/lfaoro/spark/pkg/kms/gcpkms"
)
var (
log = logger.New("[vault]")
grpcPort = flag.String("grpcPort", ":50051", "host & port the grpc server listens on.")
httpPort = flag.String("httpPort", ":3001", "host & port the http server listens on.")
// use during development only, enforces database migrations.
debugFlag = flag.Bool("debug", false, "prints debug logs.")
)
func main() {
flag.Parse()
internal.LoadEnv()
envDatabase := os.Getenv("DB_CONNECTION")
envProjectID := os.Getenv("GCP_PROJECTID")
envLocationID := os.Getenv("GCP_LOCATIONID")
envAuthHost := os.Getenv("FB_AUTH_HOST")
envServiceName := os.Getenv("FB_SERVICE_NAME")
envServiceVersion := os.Getenv("FB_SERVICE_VERSION")
log.Println("starting grpc server on", *grpcPort)
log.Println("starting http server on", *httpPort)
var _kms kms.Service
var err error
if *debugFlag {
_kms, err = aesgcm.New("")
if err != nil {
log.Fatal(err)
}
} else {
_kms = gcpkms.New(envProjectID, envLocationID, gcpkms.Debug(*debugFlag))
}
_ctx := context.Background()
cfg := errorreporting.Config{
ServiceName: envServiceName,
ServiceVersion: envServiceVersion,
}
_reporting, err := errorreporting.NewClient(_ctx, envProjectID, cfg)
if err != nil {
log.Println(err)
}
defer _reporting.Close()
_db := internal.NewDBClient(_ctx, envDatabase, *debugFlag)
defer _db.Close()
_iin := binlist.New("")
_auth := user.NewClient(envAuthHost)
srv := vault.New(_ctx, _db, _kms,
vault.WithIIN(_iin),
vault.WithAuth(_auth),
vault.WithReporting(_reporting),
vault.WithDebug(*debugFlag),
)
go func() { log.Fatal(srv.ServeGRPC(*grpcPort)) }()
go func() { log.Fatal(srv.ServeHTTP(*httpPort, *grpcPort)) }()
internal.GracefulShutdown(time.Now())
// todo: use mux to run http/grpc on same port 80
}