feat: add nestjs api and worker skeletons
This commit is contained in:
@@ -7,4 +7,4 @@ import { AppService } from './app.service';
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
export class ApiModule {}
|
||||
13
backend/apps/api/src/main.ts
Normal file
13
backend/apps/api/src/main.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ApiModule } from './api.module';
|
||||
|
||||
export async function bootstrapApi(): Promise<void> {
|
||||
const app = await NestFactory.create(ApiModule);
|
||||
app.enableShutdownHooks();
|
||||
app.setGlobalPrefix('api/v1');
|
||||
await app.listen(3000, '0.0.0.0');
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
void bootstrapApi();
|
||||
}
|
||||
@@ -2,14 +2,14 @@ import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { AppModule } from './../src/app.module';
|
||||
import { ApiModule } from './../src/api.module';
|
||||
|
||||
describe('AppController (e2e)', () => {
|
||||
describe('ApiModule (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
|
||||
beforeEach(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
imports: [ApiModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": ".",
|
||||
"rootDir": "../../..",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"testRegex": "apps/api/test/.*\\.e2e-spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
}
|
||||
8
backend/apps/api/tsconfig.app.json
Normal file
8
backend/apps/api/tsconfig.app.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"outDir": "../../dist/apps/api"
|
||||
},
|
||||
"exclude": ["node_modules", "dist", "test", "**/*.spec.ts"]
|
||||
}
|
||||
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 {}
|
||||
8
backend/apps/worker/tsconfig.app.json
Normal file
8
backend/apps/worker/tsconfig.app.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"outDir": "../../dist/apps/worker"
|
||||
},
|
||||
"exclude": ["node_modules", "dist", "test", "**/*.spec.ts"]
|
||||
}
|
||||
@@ -1,8 +1,30 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"monorepo": true,
|
||||
"root": "apps/api",
|
||||
"sourceRoot": "apps/api/src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
},
|
||||
"projects": {
|
||||
"api": {
|
||||
"type": "application",
|
||||
"root": "apps/api",
|
||||
"entryFile": "main",
|
||||
"sourceRoot": "apps/api/src",
|
||||
"compilerOptions": {
|
||||
"tsConfigPath": "apps/api/tsconfig.app.json"
|
||||
}
|
||||
},
|
||||
"worker": {
|
||||
"type": "application",
|
||||
"root": "apps/worker",
|
||||
"entryFile": "main",
|
||||
"sourceRoot": "apps/worker/src",
|
||||
"compilerOptions": {
|
||||
"tsConfigPath": "apps/worker/tsconfig.app.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,18 +6,21 @@
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest",
|
||||
"build": "pnpm run build:api && pnpm run build:worker",
|
||||
"build:api": "nest build api",
|
||||
"build:worker": "nest build worker",
|
||||
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
|
||||
"start": "nest start api",
|
||||
"start:dev": "nest start api --watch",
|
||||
"start:debug": "nest start api --debug --watch",
|
||||
"start:api": "node dist/apps/api/main.js",
|
||||
"start:worker": "node dist/apps/worker/main.js",
|
||||
"lint": "eslint \"{apps,libs}/**/*.ts\" --fix",
|
||||
"test": "jest --runInBand",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||
"test:e2e": "jest --config ./apps/api/test/jest-e2e.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^11.0.1",
|
||||
@@ -57,15 +60,21 @@
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"rootDir": ".",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/dist/"],
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"moduleNameMapper": {
|
||||
"^@travel/configuration$": "<rootDir>/libs/configuration/src",
|
||||
"^@travel/infrastructure$": "<rootDir>/libs/infrastructure/src"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
"apps/**/*.(t|j)s",
|
||||
"libs/**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"coverageDirectory": "coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
|
||||
}
|
||||
@@ -20,6 +20,10 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
"noFallthroughCasesInSwitch": false,
|
||||
"paths": {
|
||||
"@travel/configuration": ["libs/configuration/src"],
|
||||
"@travel/infrastructure": ["libs/infrastructure/src"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user