2024-04-14 13:20:55 +02:00
|
|
|
import { dualLogger } from "@loggers";
|
2024-04-07 07:30:49 +02:00
|
|
|
import { connectMeili } from "@meilisearch";
|
2024-04-07 06:16:54 +02:00
|
|
|
import { config } from "config-manager";
|
2024-04-14 10:12:41 +02:00
|
|
|
import { count } from "drizzle-orm";
|
2024-04-14 13:20:55 +02:00
|
|
|
import { LogLevel } from "log-manager";
|
2024-04-14 10:12:41 +02:00
|
|
|
import { db, setupDatabase } from "~drizzle/db";
|
2024-04-11 13:39:07 +02:00
|
|
|
import { status } from "~drizzle/schema";
|
2024-04-13 14:20:12 +02:00
|
|
|
import { createServer } from "~server";
|
2023-11-23 00:04:31 +01:00
|
|
|
|
2023-11-30 05:16:58 +01:00
|
|
|
const timeAtStart = performance.now();
|
2023-09-11 05:54:14 +02:00
|
|
|
|
2024-03-11 03:04:14 +01:00
|
|
|
// If imported as a module, redirect logs to /dev/null to not pollute console (e.g. in tests)
|
|
|
|
|
await dualLogger.log(LogLevel.INFO, "Lysand", "Starting Lysand...");
|
2023-12-09 04:32:45 +01:00
|
|
|
|
2024-04-14 10:12:41 +02:00
|
|
|
await setupDatabase(dualLogger);
|
2023-12-09 02:51:48 +01:00
|
|
|
|
2023-12-03 05:11:30 +01:00
|
|
|
if (config.meilisearch.enabled) {
|
2024-04-07 07:30:49 +02:00
|
|
|
await connectMeili(dualLogger);
|
2023-12-03 05:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2024-04-11 13:39:07 +02:00
|
|
|
postCount = (
|
|
|
|
|
await db
|
|
|
|
|
.select({
|
|
|
|
|
count: count(),
|
|
|
|
|
})
|
|
|
|
|
.from(status)
|
|
|
|
|
)[0].count;
|
2023-11-21 00:58:39 +01:00
|
|
|
} catch (e) {
|
2024-04-11 13:39:07 +02:00
|
|
|
const error = e as Error;
|
2024-04-14 13:20:55 +02:00
|
|
|
await dualLogger.logError(LogLevel.CRITICAL, "Database", error);
|
2024-04-07 07:30:49 +02:00
|
|
|
process.exit(1);
|
2023-11-21 00:58:39 +01:00
|
|
|
}
|
2024-04-14 10:12:41 +02:00
|
|
|
const server = createServer(config, dualLogger, true);
|
2023-11-11 03:36:06 +01:00
|
|
|
|
2024-03-11 03:04:14 +01:00
|
|
|
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
|
|
|
|
2024-03-11 03:04:14 +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
|
|
|
);
|
|
|
|
|
|
2024-03-11 03:04:14 +01:00
|
|
|
export { config, server };
|