From 912c186982f5c897af97a222907733fc513c61c0 Mon Sep 17 00:00:00 2001 From: portuu3 Date: Mon, 8 Apr 2024 12:21:39 +0200 Subject: [PATCH 1/2] reputation oracle serverless --- .../apps/reputation-oracle/server/api/app.ts | 31 +++++++ .../reputation-oracle/server/src/app-init.ts | 42 ++++++++++ .../apps/reputation-oracle/server/src/main.ts | 56 +------------ .../apps/reputation-oracle/server/vercel.json | 80 ++++++++++++++++--- 4 files changed, 143 insertions(+), 66 deletions(-) create mode 100644 packages/apps/reputation-oracle/server/api/app.ts create mode 100644 packages/apps/reputation-oracle/server/src/app-init.ts diff --git a/packages/apps/reputation-oracle/server/api/app.ts b/packages/apps/reputation-oracle/server/api/app.ts new file mode 100644 index 0000000000..7a0debf042 --- /dev/null +++ b/packages/apps/reputation-oracle/server/api/app.ts @@ -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); +} diff --git a/packages/apps/reputation-oracle/server/src/app-init.ts b/packages/apps/reputation-oracle/server/src/app-init.ts new file mode 100644 index 0000000000..db17b11307 --- /dev/null +++ b/packages/apps/reputation-oracle/server/src/app-init.ts @@ -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('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()); +} diff --git a/packages/apps/reputation-oracle/server/src/main.ts b/packages/apps/reputation-oracle/server/src/main.ts index 7f5eed0a62..0a503e36bf 100644 --- a/packages/apps/reputation-oracle/server/src/main.ts +++ b/packages/apps/reputation-oracle/server/src/main.ts @@ -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(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('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}`); diff --git a/packages/apps/reputation-oracle/server/vercel.json b/packages/apps/reputation-oracle/server/vercel.json index 1d24153cb1..d664746577 100644 --- a/packages/apps/reputation-oracle/server/vercel.json +++ b/packages/apps/reputation-oracle/server/vercel.json @@ -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": [ @@ -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 ." } From e9ae1a81cab2a01622bf0abb994537a3a1e0f3cd Mon Sep 17 00:00:00 2001 From: portuu3 Date: Mon, 8 Apr 2024 12:29:49 +0200 Subject: [PATCH 2/2] fix get reputation serverless function --- packages/apps/reputation-oracle/server/vercel.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/apps/reputation-oracle/server/vercel.json b/packages/apps/reputation-oracle/server/vercel.json index d664746577..d52856a5dc 100644 --- a/packages/apps/reputation-oracle/server/vercel.json +++ b/packages/apps/reputation-oracle/server/vercel.json @@ -24,7 +24,7 @@ "destination": "/api/app.ts" }, { - "source": "/reputation/(.*)", + "source": "/reputation(.*)", "destination": "/api/app.ts" }, {