Skip to content

Commit

Permalink
Enable nest start script to be run as well as serverless offline
Browse files Browse the repository at this point in the history
  • Loading branch information
StraightOuttaCrompton committed Apr 15, 2023
1 parent 28a0ffa commit bf2b559
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 25 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"packageManager": "yarn@3.4.1",
"scripts": {
"build": "nest build --webpack",
"deploy": "yarn build && cross-env NODE_ENV=production serverless deploy",
"deploy": "yarn build && cross-env NODE_ENV=production DEPLOYMENT_ENV=serverless serverless deploy",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "yarn build && cross-env NODE_ENV=development serverless offline",
"start": "nest start --watch",
"start:serverless": "yarn build && cross-env NODE_ENV=development DEPLOYMENT_ENV=serverless serverless offline",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ provider:
environment:
DATABASE_URL: ${env:DATABASE_URL}
NODE_ENV: ${env:NODE_ENV}
DEPLOYMENT_ENV: ${env:DEPLOYMENT_ENV}

useDotenv: true

Expand Down
3 changes: 3 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export const SITE_TITLE = "Ergast API";
export const SITE_DESCRIPTION =
"The Ergast API provides a historical record of motor racing data for non-commercial purposes. The API provides data for the Formula One series, from the beginning of the world championships in 1950.";
export const API_VERSION = "1.0";

export const isProduction = process.env.NODE_ENV === "production";
export const isServerless = process.env.DEPLOYMENT_ENV === "serverless";
12 changes: 12 additions & 0 deletions src/getApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import mainConfig from "./main.config";
import setupSwagger from "./setupSwagger";

export default async function getApp() {
const app = await NestFactory.create(AppModule);
mainConfig(app);
setupSwagger(app);

return app;
}
17 changes: 17 additions & 0 deletions src/main.serverless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import serverlessExpress from "@vendia/serverless-express";
import { Callback, Context, Handler } from "aws-lambda";
import getApp from "./getApp";

let server: Handler;
async function bootstrapServerLess(): Promise<Handler> {
const app = await getApp();
await app.init();

const expressApp = app.getHttpAdapter().getInstance();
return serverlessExpress({ app: expressApp });
}

export const handler: Handler = async (event: any, context: Context, callback: Callback) => {
server = server ?? (await bootstrapServerLess());
return server(event, context, callback);
};
28 changes: 7 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { NestFactory } from "@nestjs/core";
import serverlessExpress from "@vendia/serverless-express";
import { Callback, Context, Handler } from "aws-lambda";
import { AppModule } from "./app.module";
import mainConfig from "./main.config";
import setupSwagger from "./setupSwagger";
import { API_PORT } from "./consts";
import getApp from "./getApp";

let server: Handler;

async function bootstrap(): Promise<Handler> {
const app = await NestFactory.create(AppModule);

mainConfig(app);
setupSwagger(app);
await app.init();

const expressApp = app.getHttpAdapter().getInstance();
return serverlessExpress({ app: expressApp });
async function bootstrap() {
const app = await getApp();
console.log(`Server is listening on port ${API_PORT}\n`);
await app.listen(API_PORT);
}

export const handler: Handler = async (event: any, context: Context, callback: Callback) => {
server = server ?? (await bootstrap());
return server(event, context, callback);
};
bootstrap();
11 changes: 9 additions & 2 deletions src/setupSwagger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { INestApplication } from "@nestjs/common";
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
import { API_VERSION, SITE_DESCRIPTION, SITE_TITLE } from "./consts";
import { API_VERSION, SITE_DESCRIPTION, SITE_TITLE, isProduction, isServerless } from "./consts";

export default function setupSwagger(app: INestApplication) {
const config = new DocumentBuilder()
Expand All @@ -10,7 +10,14 @@ export default function setupSwagger(app: INestApplication) {
.build();
const document = SwaggerModule.createDocument(app, config);

const path = process.env.NODE_ENV === "production" ? "/" : "/docs";
const path = (() => {
if (isProduction || !isServerless) {
return "/";
}

return "/docs";
})();

SwaggerModule.setup(path, app, document, {
// explorer?: boolean;
// swaggerOptions?: Record<string, any>;
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (options, webpack) => {

return {
...options,
entry: "./src/main.serverless.ts",
externals: [],
optimization: {
minimize: true,
Expand Down

0 comments on commit bf2b559

Please sign in to comment.