69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
// First, init config.
|
|
import { config, configPath, loadConfig } from "@/src/services/configService";
|
|
import fs from "fs";
|
|
try {
|
|
loadConfig();
|
|
} catch (e) {
|
|
if (fs.existsSync("config.json")) {
|
|
console.log("Failed to load " + configPath + ": " + (e as Error).message);
|
|
} else {
|
|
console.log("Failed to load " + configPath + ". You can copy config.json.example to create your config file.");
|
|
}
|
|
process.exit(1);
|
|
}
|
|
|
|
// Now we can init the logger with the settings provided in the config.
|
|
import { logger } from "@/src/utils/logger";
|
|
logger.info("Starting up...");
|
|
|
|
// Proceed with normal startup: bring up config watcher service, validate config, connect to MongoDB, and finally start listening for HTTP.
|
|
import mongoose from "mongoose";
|
|
import { JSONStringify } from "json-with-bigint";
|
|
import { startWebServer } from "./services/webService";
|
|
|
|
import { syncConfigWithDatabase, validateConfig } from "@/src/services/configWatcherService";
|
|
import { updateWorldStateCollections } from "./services/worldStateService";
|
|
import { pluginManager } from "@/src/managers/pluginManager";
|
|
|
|
// Patch JSON.stringify to work flawlessly with Bigints.
|
|
JSON.stringify = JSONStringify;
|
|
|
|
validateConfig();
|
|
|
|
// Handle graceful shutdown
|
|
process.on('SIGINT', async () => {
|
|
logger.info('Received SIGINT, starting graceful shutdown...');
|
|
await pluginManager.cleanup();
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGTERM', async () => {
|
|
logger.info('Received SIGTERM, starting graceful shutdown...');
|
|
await pluginManager.cleanup();
|
|
process.exit(0);
|
|
});
|
|
|
|
mongoose
|
|
.connect(config.mongodbUrl)
|
|
.then(async () => {
|
|
logger.info("Connected to MongoDB");
|
|
syncConfigWithDatabase();
|
|
|
|
// Initialize plugins before starting the web server
|
|
logger.info("Loading plugins...");
|
|
await pluginManager.loadPlugins();
|
|
|
|
startWebServer();
|
|
|
|
void updateWorldStateCollections();
|
|
setInterval(() => {
|
|
void updateWorldStateCollections();
|
|
}, 60_000);
|
|
})
|
|
.catch(error => {
|
|
if (error instanceof Error) {
|
|
logger.error(`Error connecting to MongoDB server: ${error.message}`);
|
|
}
|
|
process.exit(1);
|
|
});
|