52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { differenceInSeconds } from "date-fns";
|
|
import { Elysia } from "elysia";
|
|
import cors from "@elysiajs/cors";
|
|
import authController from "./controllers/authController";
|
|
import usersController from "./controllers/usersController";
|
|
import sessionsController from "./controllers/sessionsController";
|
|
import serversController from "./controllers/serversController";
|
|
import appsController from "./controllers/appsController";
|
|
import { clientsController } from "./controllers/clientsController";
|
|
import db from "./db";
|
|
import { serversTable } from "./db/schema/servers";
|
|
import { eq } from "drizzle-orm";
|
|
import cron from "node-cron";
|
|
import { commentsController } from "./controllers/commentsController";
|
|
import { swagger } from "@elysiajs/swagger";
|
|
import { filesController } from "./controllers/filesController";
|
|
|
|
const app = new Elysia();
|
|
|
|
app.use(
|
|
cors({
|
|
origin: "*",
|
|
})
|
|
);
|
|
app.use(authController);
|
|
app.use(usersController);
|
|
app.use(serversController);
|
|
app.use(sessionsController);
|
|
app.use(appsController);
|
|
app.use(clientsController);
|
|
app.use(commentsController);
|
|
app.use(filesController);
|
|
|
|
app.use(swagger()).listen(3000);
|
|
|
|
async function checkServersStatus() {
|
|
const servers = await db.query.serversTable.findMany();
|
|
for (const { status, updatedAt, id } of servers) {
|
|
if (status === "online" && differenceInSeconds(new Date(), updatedAt) > 20)
|
|
await db
|
|
.update(serversTable)
|
|
.set({ status: "offline" })
|
|
.where(eq(serversTable.id, id));
|
|
}
|
|
}
|
|
|
|
cron.schedule("*/10 * * * * *", checkServersStatus);
|
|
|
|
console.log(
|
|
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
|
|
);
|