use platform services in startup routines

This commit is contained in:
Hazelnoot 2025-10-22 23:36:40 -04:00
parent b751f9c96d
commit 86ca3921c9
14 changed files with 117 additions and 93 deletions

View file

@ -4,11 +4,11 @@ import Fastify from 'fastify';
import { NestFactory } from '@nestjs/core';
import { MainModule } from '@/MainModule.js';
import { ServerService } from '@/server/ServerService.js';
import { loadConfig } from '@/config.js';
import type { Config } from '@/config.js';
import { NestLogger } from '@/NestLogger.js';
import { DI } from '@/di-symbols.js';
import { INestApplicationContext } from '@nestjs/common';
const config = loadConfig();
const originEnv = JSON.stringify(process.env);
process.env.NODE_ENV = 'test';
@ -20,18 +20,21 @@ let serverService: ServerService;
*
*/
async function launch() {
await killTestServer();
console.log('starting application...');
app = await NestFactory.createApplicationContext(MainModule, {
logger: new NestLogger(),
});
app.enableShutdownHooks();
const config = app.get<Config>(DI.config);
await killTestServer(config);
console.log('starting application...');
serverService = app.get(ServerService);
await serverService.launch();
await startControllerEndpoints();
await startControllerEndpoints(config);
// ジョブキューは必要な時にテストコード側で起動する
// ジョブキューが動くとテスト結果の確認に支障が出ることがあるので意図的に動かさないでいる
@ -42,7 +45,7 @@ async function launch() {
/**
* killする
*/
async function killTestServer() {
async function killTestServer(config: Config) {
//
try {
const pid = await portToPid(config.port);
@ -56,9 +59,10 @@ async function killTestServer() {
/**
*
* @param port
* @param config
*/
async function startControllerEndpoints(port = config.port + 1000) {
async function startControllerEndpoints(config: Config) {
const port = config.port + 1000;
const fastify = Fastify();
fastify.post<{ Body: { key?: string, value?: string } }>('/env', async (req, res) => {
@ -80,7 +84,7 @@ async function startControllerEndpoints(port = config.port + 1000) {
await serverService.dispose();
await app.close();
await killTestServer();
await killTestServer(config);
console.log('starting application...');