- Updated biome.json to include "graphql.d.ts" in the ignored files list. - Updated subproject commit reference in epess-database to the latest version. - Removed unused script from package.json and streamlined module file extensions in tsconfig.json. - Consolidated exclude patterns in tsconfig.build.json for clarity. - Refactored imports across multiple schema files for consistency and improved readability. - Enhanced various schema files by ensuring proper import order and removing redundant code. - Improved error handling and data integrity checks in several service and schema files.
88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
|
|
|
|
import { readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { clerkMiddleware } from '@clerk/express'
|
|
import { Logger } from '@nestjs/common'
|
|
import { NestFactory } from '@nestjs/core'
|
|
import { json } from 'express'
|
|
// @ts-expect-error
|
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs'
|
|
import { AppModule } from './app.module'
|
|
|
|
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')
|
|
})
|
|
}
|
|
|
|
bootstrap()
|