feat: 创建数据库
This commit is contained in:
@@ -2,6 +2,6 @@ import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health/health.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [HealthController],
|
||||
controllers: [HealthController],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
export function configureApplication(app: INestApplication): void {
|
||||
app.setGlobalPrefix('api/v1');
|
||||
app.enableShutdownHooks();
|
||||
app.setGlobalPrefix('api/v1');
|
||||
app.enableShutdownHooks();
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
export interface RuntimeOptions {
|
||||
host: string;
|
||||
port: number;
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
export function getRuntimeOptions(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): RuntimeOptions {
|
||||
const rawPort = env.PORT ?? '3000';
|
||||
const port = Number(rawPort);
|
||||
const host = (env.HOST ?? '127.0.0.1').trim();
|
||||
const rawPort = env.PORT ?? '3000';
|
||||
const port = Number(rawPort);
|
||||
const host = (env.HOST ?? '127.0.0.1').trim();
|
||||
|
||||
if (
|
||||
!/^\d+$/.test(rawPort) ||
|
||||
!Number.isInteger(port) ||
|
||||
port < 1 ||
|
||||
port > 65535
|
||||
) {
|
||||
throw new Error('PORT must be an integer between 1 and 65535');
|
||||
}
|
||||
if (
|
||||
!/^\d+$/.test(rawPort) ||
|
||||
!Number.isInteger(port) ||
|
||||
port < 1 ||
|
||||
port > 65535
|
||||
) {
|
||||
throw new Error('PORT must be an integer between 1 and 65535');
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
throw new Error('HOST must not be empty');
|
||||
}
|
||||
if (!host) {
|
||||
throw new Error('HOST must not be empty');
|
||||
}
|
||||
|
||||
return { host, port };
|
||||
return { host, port };
|
||||
}
|
||||
|
||||
@@ -3,15 +3,15 @@ import type { ApiSuccess, HealthStatus } from '@doodle/contracts';
|
||||
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
@Get()
|
||||
getHealth(): ApiSuccess<HealthStatus> {
|
||||
return {
|
||||
code: 0,
|
||||
message: 'ok',
|
||||
data: {
|
||||
status: 'ok',
|
||||
service: 'doodle-store-server',
|
||||
},
|
||||
};
|
||||
}
|
||||
@Get()
|
||||
getHealth(): ApiSuccess<HealthStatus> {
|
||||
return {
|
||||
code: 0,
|
||||
message: 'ok',
|
||||
data: {
|
||||
status: 'ok',
|
||||
service: 'doodle-store-server',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+20
-17
@@ -6,26 +6,29 @@ import { configureApplication } from './app.setup';
|
||||
import { getRuntimeOptions } from './common/runtime-options';
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
try {
|
||||
process.loadEnvFile();
|
||||
} catch (error) {
|
||||
if (!(
|
||||
error instanceof Error &&
|
||||
'code' in error &&
|
||||
error.code === 'ENOENT'
|
||||
)) {
|
||||
throw error;
|
||||
try {
|
||||
process.loadEnvFile();
|
||||
} catch (error) {
|
||||
if (!(
|
||||
error instanceof Error &&
|
||||
'code' in error &&
|
||||
error.code === 'ENOENT'
|
||||
)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { port, host } = getRuntimeOptions();
|
||||
const app = await NestFactory.create(AppModule);
|
||||
configureApplication(app);
|
||||
await app.listen(port, host);
|
||||
Logger.log(`Listening on http://${host}:${port}/api/v1/health`, 'Bootstrap');
|
||||
const { port, host } = getRuntimeOptions();
|
||||
const app = await NestFactory.create(AppModule);
|
||||
configureApplication(app);
|
||||
await app.listen(port, host);
|
||||
Logger.log(
|
||||
`Listening on http://${host}:${port}/api/v1/health`,
|
||||
'Bootstrap',
|
||||
);
|
||||
}
|
||||
|
||||
void bootstrap().catch((error: unknown) => {
|
||||
Logger.error(error, undefined, 'Bootstrap');
|
||||
process.exit(1);
|
||||
Logger.error(error, undefined, 'Bootstrap');
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user