-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
381 lines (305 loc) · 8.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"io/ioutil"
"log"
"net"
"os"
"github.com/uptrace/bun/dbfixture"
"github.com/uptrace/bun/migrate"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials"
grpclog "google.golang.org/grpc/grpclog"
"github.com/resonatecoop/user-api-template/app"
"github.com/resonatecoop/user-api-template/migrations"
"github.com/resonatecoop/user-api-template/model"
acc "github.com/resonatecoop/user-api-template/pkg/access"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/resonatecoop/user-api-template/gateway"
"github.com/resonatecoop/user-api-template/insecure"
pbUser "github.com/resonatecoop/user-api-template/proto/user"
authorization "github.com/resonatecoop/user-api-template/authorization"
userserver "github.com/resonatecoop/user-api-template/server"
// Static files
_ "github.com/resonatecoop/user-api-template/statik"
cli "github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "userapi",
Commands: []*cli.Command{
runServerCommand,
newDBCommand(migrations.Migrations),
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
var runServerCommand = &cli.Command{
Name: "runserver",
Usage: "start User API server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "env",
Value: "dev",
Usage: "runtime environment (prod uses env variables for connections)",
},
&cli.StringFlag{
Name: "dbdebug",
Value: "false",
Usage: "show database queries true/false",
},
},
Action: func(c *cli.Context) error {
_, apiapp, err := app.Start(c.Context, "api", c.String("env"))
//Adds gRPC internal logs. This is quite verbose, so adjust as desired!
log := grpclog.NewLoggerV2(os.Stdout, ioutil.Discard, ioutil.Discard)
grpclog.SetLoggerV2(log)
checkErr(log, err)
cfg := apiapp.Cfg
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
db := apiapp.DB(c.String("env"), dbdebug)
accService := acc.New(cfg.Access.NoTokenMethods, cfg.Access.PublicMethods, cfg.Access.WriteMethods)
interceptorAuth := authorization.NewAuthInterceptor(db, cfg.RefreshToken.Lifetime, accService)
addr := "0.0.0.0:10000"
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalln("Failed to listen:", err)
}
opts := make([]grpc.ServerOption, 0)
opts = append(opts, grpc.Creds(credentials.NewServerTLSFromCert(&insecure.Cert)))
opts = append(opts, grpc.UnaryInterceptor(
grpc_middleware.ChainUnaryServer(
interceptorAuth.Unary(),
)))
s := grpc.NewServer(
opts...,
)
pbUser.RegisterResonateUserServer(s, userserver.New(db))
// Serve gRPC Server
log.Info("Serving gRPC on https://", addr)
go func() {
log.Fatal(s.Serve(lis))
}()
err = gateway.Run("dns:///" + addr)
log.Fatal(err)
return err
},
}
func newDBCommand(migrations *migrate.Migrations) *cli.Command {
return &cli.Command{
Name: "db",
Usage: "manage database migrations",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "env",
Value: "dev",
Usage: "runtime environment (dev, test, prod) (defaults to dev, prod uses env variables for connections)",
},
&cli.StringFlag{
Name: "dbdebug",
Value: "false",
Usage: "show database queries true/false",
},
},
Subcommands: []*cli.Command{
{
Name: "init",
Usage: "create migration tables",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
return migrator.Init(ctx)
},
},
{
Name: "migrate",
Usage: "migrate database",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
_, err = migrator.Migrate(ctx)
return err
},
},
{
Name: "rollback",
Usage: "rollback the last migration group",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
_, err = migrator.Rollback(ctx)
return err
},
},
{
Name: "lock",
Usage: "lock migrations",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
return migrator.Lock(ctx)
},
},
{
Name: "unlock",
Usage: "unlock migrations",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
return migrator.Unlock(ctx)
},
},
{
Name: "create_go",
Usage: "create Go migration",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
_, err = migrator.CreateGoMigration(ctx, c.Args().Get(0))
return err
},
},
{
Name: "create_sql",
Usage: "create SQL migration",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
migrator := migrate.NewMigrator(app.DB(c.String("env"), dbdebug), migrations)
_, err = migrator.CreateSQLMigrations(ctx, c.Args().Get(0))
return err
},
},
{
Name: "load_default_fixtures",
Usage: "load default data",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
db := app.DB(c.String("env"), dbdebug)
// Let the db know about the models.
models := []interface{}{
(*model.Role)(nil),
(*model.Scope)(nil),
(*model.GroupType)(nil),
}
for _, this_model := range models {
db.RegisterModel(this_model)
}
fixture := dbfixture.New(db)
return fixture.Load(ctx, os.DirFS("fixtures/default"), "default_fixtures.yaml")
},
},
{
Name: "load_test_fixtures",
Usage: "load test data",
Action: func(c *cli.Context) error {
ctx, app, err := app.StartCLI(c)
if err != nil {
return err
}
defer app.Stop()
dbdebug := false
if c.String("dbdebug") == "true" {
dbdebug = true
}
db := app.DB(c.String("env"), dbdebug)
// Let the db know about the models.
models := []interface{}{
(*model.UserGroup)(nil),
(*model.StreetAddress)(nil),
(*model.Tag)(nil),
(*model.Role)(nil),
(*model.User)(nil),
(*model.Link)(nil),
// (*model.UserGroupPrivacy)(nil),
(*model.GroupType)(nil),
// (*model.UserGroupMember)(nil),
(*model.EmailToken)(nil),
(*model.Client)(nil),
(*model.Scope)(nil),
(*model.RefreshToken)(nil),
(*model.AuthorizationCode)(nil),
(*model.AccessToken)(nil),
}
for _, this_model := range models {
db.RegisterModel(this_model)
}
fixture := dbfixture.New(db)
return fixture.Load(ctx, os.DirFS("fixtures/test"), "test_fixtures.yaml")
},
},
},
}
}
func checkErr(log grpclog.LoggerV2, err error) {
if err != nil {
log.Fatal(err)
}
}