Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reputation Oracle - Vercel serverless #1832

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/apps/reputation-oracle/server/api/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { AppModule } from '../src/app.module';
import init from '../src/app-init';

const expressApp = express();
const adapter = new ExpressAdapter(expressApp);
let nestAppInitialized = false;

async function bootstrapNestApp() {
if (!nestAppInitialized) {
const app = await NestFactory.create(AppModule, adapter);
await init(app); // Initialize additional settings
await app.init();
nestAppInitialized = true;
}
}

export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method === 'OPTIONS') {
res.statusCode = 200;
res.end();
return;
}

await bootstrapNestApp(); // Ensure NestJS app is bootstrapped

return expressApp(req, res);
}
42 changes: 42 additions & 0 deletions packages/apps/reputation-oracle/server/src/app-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import session from 'express-session';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'body-parser';
import { useContainer } from 'class-validator';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';

import { AppModule } from './app.module';

export default async function init(app: any) {
useContainer(app.select(AppModule), { fallbackOnErrors: true });
const configService: ConfigService = app.get(ConfigService);

app.use(cookieParser());

const sessionSecret = configService.get<string>('SESSION_SECRET', '');

app.use(
session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
secure: true,
},
}),
);
app.use(json({ limit: '5mb' }));
app.use(urlencoded({ limit: '5mb', extended: true }));

const config = new DocumentBuilder()
.addBearerAuth()
.setTitle('Reputation Oracle API')
.setDescription('Swagger Reputation Oracle API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document);

app.use(helmet());
}
56 changes: 3 additions & 53 deletions packages/apps/reputation-oracle/server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,22 @@
import session from 'express-session';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'body-parser';
import { useContainer } from 'class-validator';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';

import { AppModule } from './app.module';
import { INestApplication } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ServerConfigService } from './common/config/server-config.service';
import init from './app-init';

async function bootstrap() {
const app = await NestFactory.create<INestApplication>(AppModule, {
cors: true,
});

await init(app);

const configService: ConfigService = app.get(ConfigService);
const serverConfigService = new ServerConfigService(configService);

// const baseUrl = serverConfigService.feURL;

// app.enableCors({
// origin:
// process.env.NODE_ENV === 'development' ||
// process.env.NODE_ENV === 'staging'
// ? [
// `http://localhost:3001`,
// `http://127.0.0.1:3001`,
// `http://0.0.0.0:3001`,
// baseUrl,
// ]
// : [baseUrl],
// credentials: true,
// exposedHeaders: ['Content-Disposition'],
// });

useContainer(app.select(AppModule), { fallbackOnErrors: true });

app.use(cookieParser());

const sessionSecret = configService.get<string>('SESSION_SECRET', '');

app.use(
session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
secure: true,
},
}),
);
app.use(json({ limit: '5mb' }));
app.use(urlencoded({ limit: '5mb', extended: true }));

const config = new DocumentBuilder()
.addBearerAuth()
.setTitle('Reputation Oracle API')
.setDescription('Swagger Reputation Oracle API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document);

const host = serverConfigService.host;
const port = serverConfigService.port;
app.use(helmet());

await app.listen(port, host, async () => {
console.info(`API server is running on http://${host}:${port}`);
Expand Down
80 changes: 67 additions & 13 deletions packages/apps/reputation-oracle/server/vercel.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
{
"version": 2,
"builds": [
{
"src": "src/main.ts",
"use": "@vercel/node"
"buildCommand": "yarn workspace @human-protocol/sdk build && yarn build",
"outputDirectory": "dist",
"functions": {
"api/app.ts":{
"maxDuration": 300
}
},
"redirects": [
{ "source": "/", "destination": "/swagger" }
],
"routes": [
{
"src": "/(.*)",
"dest": "src/main.ts",
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "X-Requested-With,Content-Type,Accept"
}
"rewrites": [
{
"source": "/swagger",
"destination": "/api/app.ts"
},
{
"source": "/swagger/(.*)",
"destination": "/api/app.ts"
},
{
"source": "/health",
"destination": "/api/app.ts"
},
{
"source": "/reputation(.*)",
"destination": "/api/app.ts"
},
{
"source": "/web3/prepare-signature",
"destination": "/api/app.ts"
},
{
"source": "/webhook",
"destination": "/api/app.ts"
},
{
"source": "/auth/(.*)",
"destination": "/api/app.ts"
},
{
"source": "/user/(.*)",
"destination": "/api/app.ts"
},
{
"source": "/kyc/(.*)",
"destination": "/api/app.ts"
},
{
"source": "/cron/(.*)",
"destination": "/api/app.ts"
}
],
"crons": [
Expand All @@ -27,5 +62,24 @@
"schedule": "*/5 * * * *"
}
],
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "*"
},
{
"key": "Access-Control-Allow-Headers",
"value": "*"
}
]
}
],
"ignoreCommand": "git diff HEAD^ HEAD --quiet ."
}
Loading