-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathapp.ts
539 lines (488 loc) · 17.8 KB
/
app.ts
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { join } from 'path'
import { Server } from 'http'
import { EventEmitter } from 'events'
import { ParsedUrlQuery } from 'querystring'
import { IocContract } from '@adonisjs/fold'
import { Knex } from 'knex'
import Koa, { DefaultState } from 'koa'
import bodyParser from 'koa-bodyparser'
import { Logger } from 'pino'
import Router from '@koa/router'
import { ApolloServer } from 'apollo-server-koa'
import { IAppConfig } from './config/app'
import { addResolversToSchema } from '@graphql-tools/schema'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
import { loadSchemaSync } from '@graphql-tools/load'
import { resolvers } from './graphql/resolvers'
import { HttpTokenService } from './httpToken/service'
import { AssetService } from './asset/service'
import { AccountingService } from './accounting/service'
import { PeerService } from './peer/service'
import { createPaymentPointerMiddleware } from './open_payments/payment_pointer/middleware'
import { PaymentPointer } from './open_payments/payment_pointer/model'
import { PaymentPointerService } from './open_payments/payment_pointer/service'
import { AccessType, AccessAction, Grant } from './open_payments/auth/grant'
import { createAuthMiddleware } from './open_payments/auth/middleware'
import { AuthService } from './open_payments/auth/service'
import { RatesService } from './rates/service'
import { SPSPRoutes } from './spsp/routes'
import { IncomingPaymentRoutes } from './open_payments/payment/incoming/routes'
import { PaymentPointerKeyRoutes } from './paymentPointerKey/routes'
import { PaymentPointerRoutes } from './open_payments/payment_pointer/routes'
import { IncomingPaymentService } from './open_payments/payment/incoming/service'
import { StreamServer } from '@interledger/stream-receiver'
import { WebhookService } from './webhook/service'
import { QuoteRoutes } from './open_payments/quote/routes'
import { QuoteService } from './open_payments/quote/service'
import { OutgoingPaymentRoutes } from './open_payments/payment/outgoing/routes'
import { OutgoingPaymentService } from './open_payments/payment/outgoing/service'
import { PageQueryParams } from './shared/pagination'
import { IlpPlugin, IlpPluginOptions } from './shared/ilp_plugin'
import { ApiKeyService } from './apiKey/service'
import { SessionService } from './session/service'
import { addDirectivesToSchema } from './graphql/directives'
import { Session } from './session/util'
import { createValidatorMiddleware, HttpMethod, isHttpMethod } from 'openapi'
import { PaymentPointerKeyService } from './paymentPointerKey/service'
import { AuthenticatedClient } from 'open-payments'
export interface AppContextData {
logger: Logger
closeEmitter: EventEmitter
container: AppContainer
// Set by @koa/router.
params: { [key: string]: string }
paymentPointer?: PaymentPointer
paymentPointerUrl?: string
}
export interface ApolloContext {
container: IocContract<AppServices>
logger: Logger
admin: boolean
session: Session | undefined
}
export type AppContext = Koa.ParameterizedContext<DefaultState, AppContextData>
export type AppRequest<ParamsT extends string = string> = Omit<
AppContext['request'],
'params'
> & {
params: Record<ParamsT, string>
}
export interface PaymentPointerContext extends AppContext {
paymentPointer: PaymentPointer
grant?: Grant
clientId?: string
}
// Payment pointer subresources
type CollectionRequest<BodyT = never, QueryT = ParsedUrlQuery> = Omit<
PaymentPointerContext['request'],
'body'
> & {
body: BodyT
query: ParsedUrlQuery & QueryT
}
type CollectionContext<BodyT = never, QueryT = ParsedUrlQuery> = Omit<
PaymentPointerContext,
'request'
> & {
request: CollectionRequest<BodyT, QueryT>
}
type SubresourceRequest = Omit<AppContext['request'], 'params'> & {
params: Record<'id', string>
}
type SubresourceContext = Omit<PaymentPointerContext, 'request'> & {
request: SubresourceRequest
}
export type CreateContext<BodyT> = CollectionContext<BodyT>
export type ReadContext = SubresourceContext
export type CompleteContext = SubresourceContext
export type ListContext = CollectionContext<never, PageQueryParams>
type ContextType<T> = T extends (
ctx: infer Context
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => any
? Context
: never
const PAYMENT_POINTER_PATH = '/:paymentPointerPath+'
export interface AppServices {
logger: Promise<Logger>
knex: Promise<Knex>
closeEmitter: Promise<EventEmitter>
config: Promise<IAppConfig>
httpTokenService: Promise<HttpTokenService>
assetService: Promise<AssetService>
accountingService: Promise<AccountingService>
peerService: Promise<PeerService>
authService: Promise<AuthService>
paymentPointerService: Promise<PaymentPointerService>
spspRoutes: Promise<SPSPRoutes>
incomingPaymentRoutes: Promise<IncomingPaymentRoutes>
outgoingPaymentRoutes: Promise<OutgoingPaymentRoutes>
quoteRoutes: Promise<QuoteRoutes>
paymentPointerKeyRoutes: Promise<PaymentPointerKeyRoutes>
paymentPointerRoutes: Promise<PaymentPointerRoutes>
incomingPaymentService: Promise<IncomingPaymentService>
streamServer: Promise<StreamServer>
webhookService: Promise<WebhookService>
quoteService: Promise<QuoteService>
outgoingPaymentService: Promise<OutgoingPaymentService>
makeIlpPlugin: Promise<(options: IlpPluginOptions) => IlpPlugin>
ratesService: Promise<RatesService>
apiKeyService: Promise<ApiKeyService>
sessionService: Promise<SessionService>
paymentPointerKeyService: Promise<PaymentPointerKeyService>
openPaymentsClient: Promise<AuthenticatedClient>
}
export type AppContainer = IocContract<AppServices>
export class App {
private openPaymentsServer!: Server
private adminServer!: Server
public apolloServer!: ApolloServer
public closeEmitter!: EventEmitter
public isShuttingDown = false
private logger!: Logger
private config!: IAppConfig
private outgoingPaymentTimer!: NodeJS.Timer
private deactivateInvoiceTimer!: NodeJS.Timer
public constructor(private container: IocContract<AppServices>) {}
/**
* The boot function exists because the functions that we register on the container with the `bind` method are async.
* We then need to await this function when we call use - which can't be done in the constructor. This is a first pass to
* get the container working. We can refactor this in future. Perhaps don't use private members and just pass around the container?
* Or provide start / shutdown methods on the services in the container?
*/
public async boot(): Promise<void> {
this.config = await this.container.use('config')
this.closeEmitter = await this.container.use('closeEmitter')
this.logger = await this.container.use('logger')
// Workers are in the way during tests.
if (this.config.env !== 'test') {
for (let i = 0; i < this.config.paymentPointerWorkers; i++) {
process.nextTick(() => this.processPaymentPointer())
}
for (let i = 0; i < this.config.outgoingPaymentWorkers; i++) {
process.nextTick(() => this.processOutgoingPayment())
}
for (let i = 0; i < this.config.incomingPaymentWorkers; i++) {
process.nextTick(() => this.processIncomingPayment())
}
for (let i = 0; i < this.config.webhookWorkers; i++) {
process.nextTick(() => this.processWebhook())
}
}
}
public async startAdminServer(port: number | string): Promise<void> {
const koa = await this.createKoaServer()
// Load schema from the file
const schema = loadSchemaSync(join(__dirname, './graphql/schema.graphql'), {
loaders: [new GraphQLFileLoader()]
})
// Add resolvers to the schema
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers
})
let schemaWithDirectives = schemaWithResolvers
// Add directives to schema
if (this.config.env !== 'test') {
schemaWithDirectives = addDirectivesToSchema(schemaWithResolvers)
}
// Setup Apollo on graphql endpoint
this.apolloServer = new ApolloServer({
schema: schemaWithDirectives,
context: async ({ ctx }: Koa.Context): Promise<ApolloContext> => {
const admin = this._isAdmin(ctx)
const session = await this._getSession(ctx)
return {
container: this.container,
logger: await this.container.use('logger'),
admin,
session
}
}
})
await this.apolloServer.start()
koa.use(this.apolloServer.getMiddleware())
this.adminServer = koa.listen(port)
}
public async startOpenPaymentsServer(port: number | string): Promise<void> {
const koa = await this.createKoaServer()
const router = new Router<DefaultState, AppContext>()
router.use(bodyParser())
router.get('/healthz', (ctx: AppContext): void => {
ctx.status = 200
})
const spspRoutes = await this.container.use('spspRoutes')
const paymentPointerKeyRoutes = await this.container.use(
'paymentPointerKeyRoutes'
)
const paymentPointerRoutes = await this.container.use(
'paymentPointerRoutes'
)
const incomingPaymentRoutes = await this.container.use(
'incomingPaymentRoutes'
)
const outgoingPaymentRoutes = await this.container.use(
'outgoingPaymentRoutes'
)
const quoteRoutes = await this.container.use('quoteRoutes')
const connectionRoutes = await this.container.use('connectionRoutes')
const openApi = await this.container.use('openApi')
const toRouterPath = (path: string): string =>
path.replace(/{/g, ':').replace(/}/g, '')
const toAction = ({
path,
method
}: {
path: string
method: HttpMethod
}): AccessAction | undefined => {
switch (method) {
case HttpMethod.GET:
return path.endsWith('{id}') ? AccessAction.Read : AccessAction.List
case HttpMethod.POST:
return path.endsWith('/complete')
? AccessAction.Complete
: AccessAction.Create
default:
return undefined
}
}
const actionToRoute: {
[key in AccessAction]: string
} = {
[AccessAction.Create]: 'create',
[AccessAction.Read]: 'get',
[AccessAction.ReadAll]: 'get',
[AccessAction.Complete]: 'complete',
[AccessAction.List]: 'list',
[AccessAction.ListAll]: 'list'
}
for (const path in openApi.paths) {
for (const method in openApi.paths[path]) {
if (isHttpMethod(method)) {
const action = toAction({ path, method })
if (!action) {
throw new Error()
}
let type: AccessType
let route: (ctx: AppContext) => Promise<void>
if (path.includes('incoming-payments')) {
type = AccessType.IncomingPayment
route = incomingPaymentRoutes[actionToRoute[action]]
} else if (path.includes('outgoing-payments')) {
type = AccessType.OutgoingPayment
route = outgoingPaymentRoutes[actionToRoute[action]]
} else if (path.includes('quotes')) {
type = AccessType.Quote
route = quoteRoutes[actionToRoute[action]]
} else {
if (path.includes('connections')) {
route = connectionRoutes.get
router[method](
toRouterPath(path),
createValidatorMiddleware<ContextType<typeof route>>(openApi, {
path,
method
}),
route
)
} else if (path !== '/' || method !== HttpMethod.GET) {
// The payment pointer query route is added last below
this.logger.warn({ path, method }, 'unexpected path/method')
}
continue
}
router[method](
PAYMENT_POINTER_PATH + toRouterPath(path),
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<typeof route>>(openApi, {
path,
method
}),
createAuthMiddleware({
type,
action
}),
route
)
}
}
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
router.get(
PAYMENT_POINTER_PATH + '/jwks.json',
createPaymentPointerMiddleware(),
createValidatorMiddleware<PaymentPointerContext>(openApi, {
path: '/jwks.json',
method: HttpMethod.GET
}),
async (ctx: PaymentPointerContext): Promise<void> =>
await paymentPointerKeyRoutes.getKeysByPaymentPointerId(ctx)
)
// Add the payment pointer query route last.
// Otherwise it will be matched instead of other Open Payments endpoints.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
router.get(
PAYMENT_POINTER_PATH,
createPaymentPointerMiddleware(),
createValidatorMiddleware<PaymentPointerContext>(openApi, {
path: '/',
method: HttpMethod.GET
}),
async (ctx: PaymentPointerContext): Promise<void> => {
// Fall back to legacy protocols if client doesn't support Open Payments.
if (ctx.accepts('application/json')) await paymentPointerRoutes.get(ctx)
//else if (ctx.accepts('application/ilp-stream+json')) // TODO https://docs.openpayments.dev/accounts#payment-details
else if (ctx.accepts('application/spsp4+json'))
await spspRoutes.get(ctx)
else ctx.throw(406, 'no accepted Content-Type available')
}
)
koa.use(router.routes())
this.openPaymentsServer = koa.listen(port)
}
public async shutdown(): Promise<void> {
return new Promise((resolve): void => {
if (this.openPaymentsServer) {
this.isShuttingDown = true
this.closeEmitter.emit('shutdown')
this.adminServer.close((): void => {
resolve()
})
this.openPaymentsServer.close((): void => {
resolve()
})
} else {
resolve()
}
})
}
public getAdminPort(): number {
const address = this.adminServer?.address()
if (address && !(typeof address == 'string')) {
return address.port
}
return 0
}
public getOpenPaymentsPort(): number {
const address = this.openPaymentsServer.address()
if (address && !(typeof address == 'string')) {
return address.port
}
return 0
}
private _isAdmin(ctx: Koa.Context): boolean {
return ctx.request.header['x-api-key'] == this.config.adminKey
}
private async _getSession(ctx: Koa.Context): Promise<Session | undefined> {
const key = ctx.request.header.authorization || ''
if (key && key.length) {
const sessionService = await this.container.use('sessionService')
return await sessionService.get(key)
} else {
return undefined
}
}
private async processPaymentPointer(): Promise<void> {
const paymentPointerService = await this.container.use(
'paymentPointerService'
)
return paymentPointerService
.processNext()
.catch((err) => {
this.logger.warn({ error: err.message }, 'processPaymentPointer error')
return true
})
.then((hasMoreWork) => {
if (hasMoreWork) process.nextTick(() => this.processPaymentPointer())
else
setTimeout(
() => this.processPaymentPointer(),
this.config.paymentPointerWorkerIdle
).unref()
})
}
private async processOutgoingPayment(): Promise<void> {
if (this.isShuttingDown) return
const outgoingPaymentService = await this.container.use(
'outgoingPaymentService'
)
return outgoingPaymentService
.processNext()
.catch((err) => {
this.logger.warn({ error: err.message }, 'processOutgoingPayment error')
return true
})
.then((hasMoreWork) => {
if (hasMoreWork) process.nextTick(() => this.processOutgoingPayment())
else
setTimeout(
() => this.processOutgoingPayment(),
this.config.outgoingPaymentWorkerIdle
).unref()
})
}
private async createKoaServer(): Promise<Koa<Koa.DefaultState, AppContext>> {
const koa = new Koa<DefaultState, AppContext>()
koa.context.container = this.container
koa.context.closeEmitter = await this.container.use('closeEmitter')
koa.context.logger = await this.container.use('logger')
koa.use(
async (
ctx: {
status: number
set: (arg0: string, arg1: string) => void
body: string
},
next: () => void | PromiseLike<void>
): Promise<void> => {
if (this.isShuttingDown) {
ctx.status = 503
ctx.set('Connection', 'close')
ctx.body = 'Server is in the process of restarting'
} else {
return next()
}
}
)
return koa
}
private async processIncomingPayment(): Promise<void> {
const incomingPaymentService = await this.container.use(
'incomingPaymentService'
)
return incomingPaymentService
.processNext()
.catch((err: Error) => {
this.logger.warn({ error: err.message }, 'processIncomingPayment error')
return true
})
.then((hasMoreWork) => {
if (hasMoreWork) process.nextTick(() => this.processIncomingPayment())
else
setTimeout(
() => this.processIncomingPayment(),
this.config.incomingPaymentWorkerIdle
).unref()
})
}
private async processWebhook(): Promise<void> {
const webhookService = await this.container.use('webhookService')
return webhookService
.processNext()
.catch((err) => {
this.logger.warn({ error: err.message }, 'processWebhook error')
return true
})
.then((hasMoreWork) => {
if (hasMoreWork) process.nextTick(() => this.processWebhook())
else
setTimeout(
() => this.processWebhook(),
this.config.webhookWorkerIdle
).unref()
})
}
}