Skip to content

Commit

Permalink
chore: set webpack and lambda setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marades committed Jun 27, 2024
1 parent d60cde1 commit 6130a90
Show file tree
Hide file tree
Showing 9 changed files with 7,034 additions and 3,957 deletions.
10,768 changes: 6,834 additions & 3,934 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
service: dorabangs-node
plugins:
- serverless-dotenv-plugin
- serverless-offline

provider:
name: aws
runtime: nodejs20.x
region: ap-northeast-2
architecture: arm64

custom:
dotenv:
basePath: ./
optimize:
external: ['swagger-ui-dist']

functions:
server:
handler: dist/index.handler
events:
- http:
method: any
path: '/'
cors:
origin: '*'
- http:
method: any
path: '{proxy+}'
cors:
origin: '*'

package:
exclude:
- '**/*'
include:
- 'dist/**'
2 changes: 1 addition & 1 deletion src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('AppController', () => {

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
expect(appController.getHello()).toBe('Hello Dorabangs!');
});
});
});
2 changes: 1 addition & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
return 'Hello Dorabangs!';
}
}
28 changes: 28 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';
import { AppModule } from './app.module';
import { nestSwaggerConfig } from './app.swagger';
import { nestAppConfig } from './app.config';

export async function bootstrap() {
const expressInstance: express.Express = express();
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(expressInstance),
{ cors: true },
);

// Config Nest.js Application
nestAppConfig(app);
// Config application Swagger
nestSwaggerConfig(app);

return { app, expressInstance };
}

export async function runServer() {
const { app } = await bootstrap();
await app.listen(3000);
return app;
}
28 changes: 28 additions & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Context, Handler } from 'aws-lambda';
import { createServer, proxy } from 'aws-serverless-express';
import { Server } from 'http';
import { bootstrap, runServer } from './bootstrap';
let cachedServer: Server;
export const handler: Handler = async (event: any, context: Context) => {
if (!cachedServer) {
const { app, expressInstance } = await bootstrap();
await app.init();
cachedServer = createServer(expressInstance);
}

if (event.path === '/docs') {
event.path = '/docs/';
}
if (event.path) {
event.path = event.path.includes('swagger-ui')
? `/docs${event.path}`
: event.path;
}
return proxy(cachedServer, event, context, 'PROMISE').promise;
};

if (process.env.NODE_ENV === 'local') {
runServer()
.then(() => console.log('Nest Ready'))
.catch((error) => console.log(error));
}
19 changes: 0 additions & 19 deletions src/main.ts

This file was deleted.

10 changes: 8 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
"noFallthroughCasesInSwitch": false,
"strict": true,
"esModuleInterop": true,
"paths": {
"@src/*": ["./src/*"]
}
},
"exclude": ["./test", "./dist"]
}
97 changes: 97 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import CopyPlugin from 'copy-webpack-plugin';
import * as path from 'path';
import * as webpack from 'webpack';
const swaggerUiModulePath = path.dirname(require.resolve('swagger-ui-dist'));
module.exports = {
entry: './src/handler.ts',
mode: 'none',
target: 'node',
devtool: 'source-map',
plugins: [
new webpack.IgnorePlugin({
checkResource(resource) {
const lazyImports = [
'./swagger-ui-bundle.js',
'./swagger-ui-standalone-preset.js',
'class-validator',
'class-transformer/storage',
'@nestjs/swagger',
'fastify-swagger',
'@nestjs/microservices',
'@nestjs/platform-fastify',
'@nestjs/websockets/socket-module',
'@nestjs/microservices/microservices-module',
// mongodb optionals
'mongodb-client-encryption',
'bson-ext',
'kerberos',
'aws4',
'snappy',
'gcp-metadata',
'@mongodb-js/zstd',
'snappy/package.json',
];
if (!lazyImports.includes(resource)) {
return false;
}
try {
require.resolve(resource);
} catch (err) {
return true;
}
return false;
},
}),
new CopyPlugin({
patterns: [
{
from: `${swaggerUiModulePath}/swagger-ui.css`,
to: 'swagger-ui.css',
},
{
from: `${swaggerUiModulePath}/swagger-ui-bundle.js`,
to: 'swagger-ui-bundle.js',
},
{
from: `${swaggerUiModulePath}/swagger-ui-standalone-preset.js`,
to: 'swagger-ui-standalone-preset.js',
},
{
from: `${swaggerUiModulePath}/favicon-32x32.png`,
to: 'favicon-32x32.png',
},
{
from: `${swaggerUiModulePath}/favicon-16x16.png`,
to: 'favicon-16x16.png',
},
{
from: `${swaggerUiModulePath}/oauth2-redirect.html`,
to: 'src/oauth2-redirect.html',
},
],
}),
],
externals: {},
resolve: {
extensions: ['.ts', '.js'],
alias: {
'@src': path.resolve(__dirname, 'src'),
},
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'commonjs',
},
module: {
rules: [{ test: /\.ts$/, loader: 'ts-loader' }],
},
stats: {
warningsFilter: [
'optional-require',
'load-package.util',
'load-adapter',
() => false,
],
},
};

0 comments on commit 6130a90

Please sign in to comment.