-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
430 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 7 additions & 30 deletions
37
libs/api/core/data-access/src/lib/api-core-data-access.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,22 @@ | ||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo' | ||
import { Module } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { GraphQLModule } from '@nestjs/graphql' | ||
import { ScheduleModule } from '@nestjs/schedule' | ||
import { ServeStaticModule } from '@nestjs/serve-static' | ||
import { join } from 'path' | ||
|
||
import { ApiCoreConfigService } from './api-core-config.service' | ||
import { ApiCoreProvisionService } from './api-core-provision.service' | ||
import { ApiCoreService } from './api-core.service' | ||
import { configuration } from './config/configuration' | ||
import { validationSchema } from './config/validation-schema' | ||
import { AppContext } from './entity/app-context' | ||
import { ApiCoreConfigModule } from './config/api-core-config.module' | ||
import { ApiCoreGraphQLModule } from './graphql/api-core-graphql.module' | ||
import { serveStaticFactory } from './helpers/serve-static-factory' | ||
import { ApiCoreQueuesModule } from './queues/api-core-queues.module' | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
cache: true, | ||
load: [configuration], | ||
validationSchema, | ||
}), | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
autoSchemaFile: join(process.cwd(), 'api-schema.graphql'), | ||
sortSchema: true, | ||
driver: ApolloDriver, | ||
introspection: process.env['GRAPHQL_PLAYGROUND']?.toLowerCase() === 'true', | ||
playground: { | ||
settings: { | ||
'request.credentials': 'include', | ||
}, | ||
}, | ||
resolvers: { | ||
// JSON: GraphQLJSON, | ||
}, | ||
context: ({ req, res }: AppContext) => ({ req, res }), | ||
}), | ||
ApiCoreConfigModule, | ||
ApiCoreGraphQLModule, | ||
ApiCoreQueuesModule, | ||
ScheduleModule.forRoot(), | ||
ServeStaticModule.forRootAsync({ useFactory: serveStaticFactory() }), | ||
], | ||
providers: [ApiCoreService, ApiCoreConfigService, ApiCoreProvisionService], | ||
providers: [ApiCoreService, ApiCoreProvisionService], | ||
exports: [ApiCoreService], | ||
}) | ||
export class ApiCoreDataAccessModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
libs/api/core/data-access/src/lib/config/api-core-config.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Module } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
|
||
import { ApiCoreConfigService } from './api-core-config.service' | ||
import { configuration } from './configuration' | ||
import { validationSchema } from './validation-schema' | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
cache: true, | ||
load: [configuration], | ||
validationSchema, | ||
}), | ||
], | ||
providers: [ApiCoreConfigService], | ||
exports: [ApiCoreConfigService], | ||
}) | ||
export class ApiCoreConfigModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
libs/api/core/data-access/src/lib/graphql/api-core-graphql.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo' | ||
import { Module } from '@nestjs/common' | ||
import { GraphQLModule } from '@nestjs/graphql' | ||
import { AppContext } from '../entity/app-context' | ||
import { join } from 'path' | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
autoSchemaFile: join(process.cwd(), 'api-schema.graphql'), | ||
sortSchema: true, | ||
driver: ApolloDriver, | ||
introspection: process.env['GRAPHQL_PLAYGROUND']?.toLowerCase() === 'true', | ||
playground: { | ||
settings: { | ||
'request.credentials': 'include', | ||
}, | ||
}, | ||
resolvers: { | ||
// JSON: GraphQLJSON, | ||
}, | ||
context: ({ req, res }: AppContext) => ({ req, res }), | ||
}), | ||
], | ||
}) | ||
export class ApiCoreGraphQLModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
libs/api/core/data-access/src/lib/queues/api-core-queues.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ExpressAdapter } from '@bull-board/express' | ||
import { BullBoardModule } from '@bull-board/nestjs' | ||
import { BullModule } from '@nestjs/bullmq' | ||
import { Module } from '@nestjs/common' | ||
import { ApiCoreConfigModule } from '../config/api-core-config.module' | ||
import { ApiCoreConfigService } from '../config/api-core-config.service' | ||
import { BullDashboardMiddleware } from './bull-dashboard-middleware' | ||
|
||
const logoUrl = 'https://avatars.githubusercontent.com/u/125477168?v=4' | ||
@Module({ | ||
imports: [ | ||
BullModule.forRootAsync({ | ||
imports: [ApiCoreConfigModule], | ||
useFactory: async (config: ApiCoreConfigService) => ({ | ||
prefix: 'pubkey:api', | ||
connection: config.redisOptions, | ||
defaultJobOptions: { | ||
removeOnFail: { age: 24 * 3600 }, | ||
}, | ||
}), | ||
inject: [ApiCoreConfigService], | ||
}), | ||
BullBoardModule.forRoot({ | ||
route: '/queues', | ||
boardOptions: { | ||
uiConfig: { | ||
boardTitle: 'PubKey API', | ||
boardLogo: { path: logoUrl }, | ||
favIcon: { default: logoUrl, alternative: logoUrl }, | ||
}, | ||
}, | ||
adapter: ExpressAdapter, | ||
middleware: BullDashboardMiddleware, | ||
}), | ||
], | ||
}) | ||
export class ApiCoreQueuesModule {} |
22 changes: 22 additions & 0 deletions
22
libs/api/core/data-access/src/lib/queues/bull-dashboard-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HttpStatus, Logger, NestMiddleware } from '@nestjs/common' | ||
import { NextFunction, Request, Response } from 'express-serve-static-core' | ||
import * as process from 'process' | ||
|
||
export class BullDashboardMiddleware implements NestMiddleware { | ||
private readonly envCreds = process.env['BULL_ADMIN'] ?? `admin:${Math.random().toString(36).substring(7)}` | ||
private readonly encodedCreds = Buffer.from(this.envCreds).toString('base64') | ||
|
||
constructor() { | ||
Logger.verbose(`BullDashboardMiddleware: ${this.envCreds}`) | ||
} | ||
use(req: Request, res: Response, next: NextFunction) { | ||
const reqCreds = req.get('authorization')?.split('Basic ')?.[1] ?? null | ||
|
||
if (this.encodedCreds && reqCreds !== this.encodedCreds) { | ||
res.setHeader('WWW-Authenticate', 'Basic realm="realm", charset="UTF-8"') | ||
res.sendStatus(HttpStatus.UNAUTHORIZED) | ||
} else { | ||
next() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.