server/index.ts

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-04-07 10:56:15 +02:00
import { exists, mkdir, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
2024-04-07 07:30:49 +02:00
import { connectMeili } from "@meilisearch";
import { moduleIsEntry } from "@module";
2023-12-02 00:00:00 +01:00
import { initializeRedisCache } from "@redis";
2024-04-07 06:16:54 +02:00
import { config } from "config-manager";
import { LogLevel, LogManager, MultiLogManager } from "log-manager";
import { createServer } from "~server";
import { db, client as pgClient } from "~drizzle/db";
import { status } from "~drizzle/schema";
import { count, sql } from "drizzle-orm";
await pgClient.connect();
2023-11-30 05:16:58 +01:00
const timeAtStart = performance.now();
2023-09-11 05:54:14 +02:00
2024-04-07 10:56:15 +02:00
// Create requests file if it doesnt exist
if (
!(await exists(
`${process.cwd()}/${dirname(config.logging.storage.requests)}`,
))
) {
await mkdir(`${process.cwd()}/${dirname(config.logging.storage.requests)}`);
await writeFile(`${process.cwd()}/${config.logging.storage.requests}`, "");
}
const requests_log = Bun.file(
`${process.cwd()}/${config.logging.storage.requests}`,
);
const isEntry = moduleIsEntry(import.meta.url);
// If imported as a module, redirect logs to /dev/null to not pollute console (e.g. in tests)
2024-04-07 07:30:49 +02:00
const logger = new LogManager(isEntry ? requests_log : Bun.file("/dev/null"));
const consoleLogger = new LogManager(
2024-04-07 07:30:49 +02:00
isEntry ? Bun.stdout : Bun.file("/dev/null"),
);
const dualLogger = new MultiLogManager([logger, consoleLogger]);
2023-09-15 05:21:38 +02:00
await dualLogger.log(LogLevel.INFO, "Lysand", "Starting Lysand...");
// NODE_ENV seems to be broken and output `development` even when set to production, so use the flag instead
const isProd =
2024-04-07 07:30:49 +02:00
process.env.NODE_ENV === "production" || process.argv.includes("--prod");
2023-12-02 00:00:00 +01:00
const redisCache = await initializeRedisCache();
if (config.meilisearch.enabled) {
2024-04-07 07:30:49 +02:00
await connectMeili(dualLogger);
}
2023-11-21 00:58:39 +01:00
// Check if database is reachable
2023-11-29 04:57:35 +01:00
let postCount = 0;
2023-11-21 00:58:39 +01:00
try {
postCount = (
await db
.select({
count: count(),
})
.from(status)
)[0].count;
2023-11-21 00:58:39 +01:00
} catch (e) {
const error = e as Error;
2024-04-07 07:30:49 +02:00
await logger.logError(LogLevel.CRITICAL, "Database", error);
await consoleLogger.logError(LogLevel.CRITICAL, "Database", error);
process.exit(1);
2023-11-21 00:58:39 +01:00
}
2024-04-07 06:16:54 +02:00
const server = createServer(config, dualLogger, isProd);
await dualLogger.log(
2024-04-07 07:30:49 +02:00
LogLevel.INFO,
"Server",
`Lysand started at ${config.http.bind}:${config.http.bind_port} in ${(
performance.now() - timeAtStart
).toFixed(0)}ms`,
2023-10-23 02:32:17 +02:00
);
2023-11-21 00:58:39 +01:00
await dualLogger.log(
2024-04-07 07:30:49 +02:00
LogLevel.INFO,
"Database",
`Database is online, now serving ${postCount} posts`,
2023-11-21 00:58:39 +01:00
);
export { config, server };