use elysia instead of express

This commit is contained in:
Fran Jurmanović
2023-10-04 20:19:38 +02:00
parent aea2acbdf8
commit 82acbf93fc
21 changed files with 276 additions and 455 deletions

View File

@@ -1,60 +1,126 @@
import { Client } from "discord.js";
import { Chat } from "@common";
import { Controller } from "@core";
import { ClientController } from "@controllers";
import express from "express";
import { APP_VERSION, config } from "@constants";
import bodyParser from "body-parser";
import redoc from "redoc-express";
import path from "path";
import { config } from "@constants";
import { CronJob } from "cron";
import { sendDiscordMessage, sendNextMessage } from "@common";
import { Elysia, t } from "elysia";
import { swagger } from "@elysiajs/swagger";
import { basicAuth } from "@core";
import pino from "pino";
import staticPlugin from "@elysiajs/static";
const client: Client = new Client();
const chat: Chat = new Chat(client);
const app = express();
app.use(bodyParser.json());
const fileTransport = pino.transport({
target: "pino/file",
app.get("/docs/swagger.json", (req, res) => {
res.sendFile("swagger.json", { root: path.join(__dirname, "..") });
options: { destination: `app.log` },
});
app.get(
"/docs",
redoc({
title: "API Docs",
specUrl: "/docs/swagger.json",
nonce: "",
redocOptions: {
theme: {
colors: {
primary: {
main: "#6EC5AB",
},
const logger = pino(
{
level: "error",
},
fileTransport
);
const taskPlugin = new Elysia({ prefix: "/job" })
.state("job", null as CronJob | null)
.onStart(({ store }) => {
client.on("ready", (): void => {
if (store.job) {
store.job.stop();
}
store.job = new CronJob(
config.CRON_LEGICA,
() => sendNextMessage(client),
null,
true,
"utc"
);
});
})
.onBeforeHandle(({ store: { job }, set }) => {
if (!job) {
set.status = 400;
return "Job is not running.";
}
})
.use(
basicAuth({
users: [
{
username: "admin",
password: config.PASSWORD,
},
typography: {
fontFamily: `"museo-sans", 'Helvetica Neue', Helvetica, Arial, sans-serif`,
fontSize: "15px",
lineHeight: "1.5",
code: {
code: "#87E8C7",
backgroundColor: "#4D4D4E",
},
},
menu: {
backgroundColor: "#ffffff",
],
errorMessage: "Unauthorized",
})
)
.get("/", ({ store: { job } }) => ({
running: job?.running ?? false,
next: job?.nextDate().toISO(),
}))
.post("/", ({ store: { job }, set }) => {
if (job?.running) {
set.status = 400;
return "Task already running";
}
job?.start();
return "Task started";
})
.delete("/", ({ store: { job }, set }) => {
if (!job?.running) {
set.status = 400;
return "Task already stopped";
}
job?.stop();
return "Task stopped";
})
.post(
"/send",
async ({ set, body }) => {
try {
const url = body.url;
if (url) {
await sendDiscordMessage(client, url);
} else {
await sendNextMessage(client);
}
return true;
} catch (err) {
set.status = 400;
return err;
}
},
{
body: t.Object({
url: t.String(),
}),
}
)
.get("/log", () => Bun.file("app.log"));
client.login(config.TOKEN);
const app = new Elysia()
.onError(({ error }) => {
logger.error(error);
return new Response(error.toString());
})
.get("/", () => config.APP_VERSION)
.use(
swagger({
documentation: {
info: {
title: "Legica Bot",
version: config.APP_VERSION,
},
},
},
})
);
app.get("/version", (_, res) => {
res.send(APP_VERSION);
});
const controllers = new Controller(app, [new ClientController(client)]);
controllers.register();
chat.register(config.TOKEN || "");
app.listen(config.PORT, () =>
console.log(`Legica bot API listening on port ${config.PORT}!`)
})
)
.use(staticPlugin())
.use(taskPlugin)
.listen(config.PORT);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);