Files
epess-web-backend/src/main.ts

89 lines
2.9 KiB
TypeScript

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 { json } from 'express'
async function bootstrap() {
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
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
app.enableCors({
origin: corsOrigin,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', '*', 'x-apollo-operation-name', 'x-session-id'],
credentials: true,
})
// set base path for api
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()
const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup(process.env.SWAGGER_PATH ?? 'v1', app, document)
document.paths[process.env.API_PATH + '/graphql'] = {
get: {
tags: ['GraphQL'],
summary: 'GraphQL Playground',
description: 'Access the GraphQL Playground to interact with the GraphQL API.',
responses: {
'200': {
description: 'GraphQL Playground',
},
},
},
}
try {
// body parser
app.use(json({ limit: '50mb' }))
// clerk middleware
app.use(clerkMiddleware({}))
// graphql upload
app.use(
graphqlUploadExpress({
maxFileSize: 1024 * 1024 * 1024, // 1 GB
maxFiles: 10,
}),
)
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
} catch (error: any) {
Logger.error(`Error in file upload middleware: ${error.message}`, 'Bootstrap')
// Optionally, you can handle the error further or rethrow it
}
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')
})
}
// IIFE
;(async () => {
await bootstrap()
})()