feat: add nestjs api and worker skeletons
This commit is contained in:
20
backend/apps/worker/src/main.ts
Normal file
20
backend/apps/worker/src/main.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { INestApplicationContext, Type } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { WorkerModule } from './worker.module';
|
||||
|
||||
type ContextFactory = (
|
||||
module: Type<unknown>,
|
||||
) => Promise<INestApplicationContext>;
|
||||
|
||||
export async function bootstrapWorker(
|
||||
createContext: ContextFactory = (module) =>
|
||||
NestFactory.createApplicationContext(module),
|
||||
): Promise<INestApplicationContext> {
|
||||
const app = await createContext(WorkerModule);
|
||||
app.enableShutdownHooks();
|
||||
return app;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
void bootstrapWorker();
|
||||
}
|
||||
16
backend/apps/worker/src/worker.bootstrap.spec.ts
Normal file
16
backend/apps/worker/src/worker.bootstrap.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { bootstrapWorker } from './main';
|
||||
|
||||
describe('bootstrapWorker', () => {
|
||||
it('creates an application context without starting an HTTP listener', async () => {
|
||||
const close = jest.fn().mockResolvedValue(undefined);
|
||||
const enableShutdownHooks = jest.fn();
|
||||
const appContext = { close, enableShutdownHooks };
|
||||
const createContext = jest.fn().mockResolvedValue(appContext);
|
||||
|
||||
const app = await bootstrapWorker(createContext as never);
|
||||
|
||||
expect(createContext).toHaveBeenCalledTimes(1);
|
||||
expect(enableShutdownHooks).toHaveBeenCalledTimes(1);
|
||||
expect(app).toBe(appContext);
|
||||
});
|
||||
});
|
||||
4
backend/apps/worker/src/worker.module.ts
Normal file
4
backend/apps/worker/src/worker.module.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
@Module({})
|
||||
export class WorkerModule {}
|
||||
Reference in New Issue
Block a user