refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -1,39 +1,39 @@
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { AppModule } from './app.module';
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { clerkMiddleware } from '@clerk/express';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js';
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { AppModule } from './app.module'
import { Logger } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { clerkMiddleware } from '@clerk/express'
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js'
import path from 'node:path'
import { readFileSync } from 'node:fs'
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule)
// load private key and public key
const privateKey = readFileSync(
path.join(__dirname, 'KeyStore', 'private_key.pem'),
'utf8',
);
)
const publicKey = readFileSync(
path.join(__dirname, 'KeyStore', 'public_key.pem'),
'utf8',
);
)
// set private key and public key to env
process.env.JWT_RS256_PRIVATE_KEY = privateKey;
process.env.JWT_RS256_PUBLIC_KEY = publicKey;
process.env.JWT_RS256_PRIVATE_KEY = privateKey
process.env.JWT_RS256_PUBLIC_KEY = publicKey
Logger.log(
`Private key: ${privateKey.slice(0, 10).replace(/\n/g, '')}...`,
'Bootstrap',
);
)
Logger.log(
`Public key: ${publicKey.slice(0, 10).replace(/\n/g, '')}...`,
'Bootstrap',
);
)
const corsOrigin = (process.env.CORS_ORIGIN ?? '').split(','); // split by comma to array
const corsOrigin = (process.env.CORS_ORIGIN ?? '').split(',') // split by comma to array
app.enableCors({
origin: corsOrigin,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
@@ -44,20 +44,20 @@ async function bootstrap() {
'x-session-id',
],
credentials: true,
});
})
// set base path for api
app.setGlobalPrefix(process.env.API_PATH ?? '/v1');
app.setGlobalPrefix(process.env.API_PATH ?? '/v1')
const config = new DocumentBuilder()
.setTitle('EPESS API')
.setDescription('API documentation for EPESS application')
.setVersion('0.0.1')
.addBearerAuth()
.build();
.build()
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup(process.env.SWAGGER_PATH ?? 'v1', app, document);
const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup(process.env.SWAGGER_PATH ?? 'v1', app, document)
document.paths[process.env.API_PATH + '/graphql'] = {
get: {
@@ -71,10 +71,10 @@ async function bootstrap() {
},
},
},
};
}
// clerk middleware
app.use(clerkMiddleware({}));
app.use(clerkMiddleware({}))
// graphql upload
app.use(
@@ -82,11 +82,11 @@ async function bootstrap() {
maxFileSize: 100 * 1024 * 1024, // 100 MB
maxFiles: 10,
}),
);
const host = process.env.LISTEN_HOST ?? '0.0.0.0';
const port = process.env.LISTEN_PORT ?? 3000; // Default to 3000 if LISTEN_PORT is not set
)
const host = process.env.LISTEN_HOST ?? '0.0.0.0'
const port = process.env.LISTEN_PORT ?? 3000 // Default to 3000 if LISTEN_PORT is not set
await app.listen(port, host, () => {
Logger.log(`Server is running on http://${host}:${port}`, 'Bootstrap');
});
Logger.log(`Server is running on http://${host}:${port}`, 'Bootstrap')
})
}
bootstrap();
bootstrap()