refactor(server): use proxy mechanisms to remove server_url config

This commit is contained in:
Amruth Pillai
2022-03-08 10:36:04 +01:00
parent e52edaa552
commit 5a2594eb88
18 changed files with 76 additions and 82 deletions

View File

@ -53,6 +53,7 @@
"@nestjs/schematics": "^8.0.7",
"@reactive-resume/schema": "workspace:*",
"@types/bcrypt": "^5.0.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.13",
"@types/multer": "^1.4.7",
"@types/node": "^17.0.21",

View File

@ -18,6 +18,7 @@ import { UsersModule } from './users/users.module';
@Module({
imports: [
ServeStaticModule.forRoot({
serveRoot: '/api',
rootPath: join(__dirname, 'assets'),
}),
ConfigModule,

View File

@ -5,6 +5,5 @@ export default registerAs('app', () => ({
environment: process.env.NODE_ENV,
secretKey: process.env.SECRET_KEY,
port: parseInt(process.env.PORT, 10) || 3100,
url: process.env.PUBLIC_APP_URL || 'http://localhost:3000',
serverUrl: process.env.PUBLIC_SERVER_URL || 'http://localhost:3100',
url: process.env.PUBLIC_URL || 'http://localhost:3000',
}));

View File

@ -16,8 +16,7 @@ const validationSchema = Joi.object({
NODE_ENV: Joi.string().valid('development', 'production').default('development'),
// URLs
PUBLIC_APP_URL: Joi.string().default('http://localhost:3000'),
PUBLIC_SERVER_URL: Joi.string().default('http://localhost:3100'),
PUBLIC_URL: Joi.string().default('http://localhost:3000'),
// Database
POSTGRES_HOST: Joi.string().required(),
@ -38,6 +37,9 @@ const validationSchema = Joi.object({
// SendGrid
SENDGRID_API_KEY: Joi.string().allow(''),
SENDGRID_FORGOT_PASSWORD_TEMPLATE_ID: Joi.string().allow(''),
SENDGRID_FROM_NAME: Joi.string().allow(''),
SENDGRID_FROM_EMAIL: Joi.string().allow(''),
});
@Module({

View File

@ -3,7 +3,6 @@ import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import cookieParser from 'cookie-parser';
import { join } from 'path';
import { AppModule } from './app.module';
@ -15,24 +14,18 @@ const bootstrap = async () => {
app.setGlobalPrefix(globalPrefix);
// Middleware
app.enableCors({ credentials: true });
app.enableShutdownHooks();
app.use(cookieParser());
// Pipes
app.useGlobalPipes(new ValidationPipe({ transform: true }));
// Email Templates
app.setBaseViewsDir(join(__dirname, 'mail/templates'));
app.setViewEngine('hbs');
const configService = app.get(ConfigService);
const serverUrl = configService.get<number>('app.serverUrl');
const port = configService.get<number>('app.port');
await app.listen(port);
Logger.log(`🚀 Server is running on: ${serverUrl}/${globalPrefix}`);
Logger.log(`🚀 Server is up and running!`);
};
bootstrap();

View File

@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config';
import { SchedulerRegistry } from '@nestjs/schedule';
import { mkdir, unlink, writeFile } from 'fs/promises';
import { nanoid } from 'nanoid';
import { join, resolve } from 'path';
import { join } from 'path';
import { PDFDocument } from 'pdf-lib';
import { Browser, chromium } from 'playwright-chromium';
@ -28,7 +28,6 @@ export class PrinterService implements OnModuleInit, OnModuleDestroy {
async printAsPdf(username: string, slug: string): Promise<string> {
const url = this.configService.get<string>('app.url');
const secretKey = this.configService.get<string>('app.secretKey');
const serverUrl = this.configService.get<string>('app.serverUrl');
const page = await this.browser.newPage();
@ -44,9 +43,9 @@ export class PrinterService implements OnModuleInit, OnModuleDestroy {
});
const pdf = await PDFDocument.create();
const directory = resolve('dist/assets/resumes');
const directory = join(__dirname, '..', 'assets/exports');
const filename = `RxResume_PDFExport_${nanoid()}.pdf`;
const publicUrl = `${serverUrl}/resumes/${filename}`;
const publicUrl = `/api/exports/${filename}`;
for (let index = 0; index < resumePages.length; index++) {
await page.evaluate((page) => (document.body.innerHTML = page.innerHTML), resumePages[index]);

View File

@ -22,8 +22,8 @@ import { ResumeService } from './resume.service';
storage: diskStorage({
destination: async (req, _, cb) => {
const userId = (req.user as User).id;
const resumeId = req.params.id;
const destination = join(__dirname, `assets/uploads/${userId}/${resumeId}`);
const resumeId = +req.params.id;
const destination = join(__dirname, '..', `assets/uploads/${userId}/${resumeId}`);
await mkdir(destination, { recursive: true });

View File

@ -218,9 +218,8 @@ export class ResumeService {
async uploadPhoto(id: number, userId: number, filename: string) {
const resume = await this.findOne(id, userId);
const serverUrl = this.configService.get<string>('app.serverUrl');
const url = `${serverUrl}/uploads/${userId}/${id}/${filename}`;
const url = `/api/uploads/${userId}/${id}/${filename}`;
const updatedResume = set(resume, 'basics.photo.url', url);
return this.resumeRepository.save<Resume>(updatedResume);
@ -228,8 +227,8 @@ export class ResumeService {
async deletePhoto(id: number, userId: number) {
const resume = await this.findOne(id, userId);
const key = new URL(resume.basics.photo.url).pathname;
const photoPath = join(__dirname, 'assets', key);
const filepath = new URL(resume.basics.photo.url).pathname;
const photoPath = join(__dirname, '..', `assets/${filepath}`);
const updatedResume = set(resume, 'basics.photo.url', '');
await unlink(photoPath);