forked from vmware-archive/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (107 loc) · 3.12 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
124
125
126
127
128
129
130
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package main
import (
"os"
"time"
"github.com/docker/libkv"
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
"github.com/go-openapi/loads"
"github.com/go-openapi/loads/fmts"
"github.com/go-openapi/swag"
"github.com/jessevdk/go-flags"
"github.com/justinas/alice"
log "github.com/sirupsen/logrus"
"github.com/vmware/dispatch/pkg/entity-store"
"github.com/vmware/dispatch/pkg/middleware"
"github.com/vmware/dispatch/pkg/secret-store/gen/restapi"
"github.com/vmware/dispatch/pkg/secret-store/gen/restapi/operations"
"github.com/vmware/dispatch/pkg/secret-store/web"
"github.com/vmware/dispatch/pkg/trace"
)
func init() {
loads.AddLoader(fmts.YAMLMatcher, fmts.YAMLDoc)
boltdb.Register()
}
var debugFlags = struct {
DebugEnabled bool `long:"debug" description:"Enable debugging messages"`
TracingEnabled bool `long:"trace" description:"Enable tracing messages (enables debugging)"`
}{}
func configureFlags() []swag.CommandLineOptionsGroup {
return []swag.CommandLineOptionsGroup{
swag.CommandLineOptionsGroup{
ShortDescription: "Secret Store Flags",
LongDescription: "",
Options: &web.SecretStoreFlags,
},
swag.CommandLineOptionsGroup{
ShortDescription: "Debug options",
LongDescription: "",
Options: &debugFlags,
},
}
}
func main() {
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
log.Fatalln(err)
}
api := operations.NewSecretStoreAPI(swaggerSpec)
server := restapi.NewServer(api)
defer server.Shutdown()
parser := flags.NewParser(server, flags.Default)
parser.ShortDescription = "Secret Store"
parser.LongDescription = "An API for managing secrets for Dispatch."
optsGroups := configureFlags()
for _, optsGroup := range optsGroups {
_, err := parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
if err != nil {
log.Fatalln(err)
}
}
if debugFlags.DebugEnabled {
log.SetLevel(log.DebugLevel)
}
if debugFlags.TracingEnabled {
log.SetLevel(log.DebugLevel)
trace.Enable()
}
if _, err := parser.Parse(); err != nil {
code := 1
if fe, ok := err.(*flags.Error); ok {
if fe.Type == flags.ErrHelp {
code = 0
}
}
os.Exit(code)
}
kv, err := libkv.NewStore(
store.BOLTDB,
[]string{web.SecretStoreFlags.DbFile},
&store.Config{
Bucket: "secret",
ConnectionTimeout: 1 * time.Second,
PersistConnection: true,
},
)
if err != nil {
log.Fatalf("Error creating/opening the entity store: %v", err)
}
entityStore := entitystore.New(kv)
handlers, err := web.NewHandlers(entityStore)
web.ConfigureHandlers(api, handlers)
healthChecker := func() error {
// TODO: implement service-specific healthchecking
return nil
}
handler := alice.New(
middleware.NewHealthCheckMW("", healthChecker),
).Then(api.Serve(nil))
server.SetHandler(handler)
if err := server.Serve(); err != nil {
log.Fatalln(err)
}
}