initialize redoc

This commit is contained in:
Fran Jurmanović
2023-10-03 00:08:14 +02:00
parent 224c2f32c7
commit fb98df8f98
7 changed files with 226 additions and 25 deletions

View File

@@ -3,9 +3,10 @@ import { Chat } from "@common";
import { Controller } from "@core";
import { ClientController } from "@controllers";
import express from "express";
import { config } from "@constants";
import basicAuth from "express-basic-auth";
import { APP_VERSION, config } from "@constants";
import bodyParser from "body-parser";
import redoc from "redoc-express";
import path from "path";
const client: Client = new Client();
const chat: Chat = new Chat(client);
@@ -13,16 +14,47 @@ const app = express();
app.use(bodyParser.json());
app.use(
basicAuth({
users: {
admin: config.PASSWORD,
app.get("/docs/swagger.json", (req, res) => {
res.sendFile("swagger.json", { root: path.join(__dirname, "..") });
});
app.get(
"/docs",
redoc({
title: "API Docs",
specUrl: "/docs/swagger.json",
nonce: "",
redocOptions: {
theme: {
colors: {
primary: {
main: "#6EC5AB",
},
},
typography: {
fontFamily: `"museo-sans", 'Helvetica Neue', Helvetica, Arial, sans-serif`,
fontSize: "15px",
lineHeight: "1.5",
code: {
code: "#87E8C7",
backgroundColor: "#4D4D4E",
},
},
menu: {
backgroundColor: "#ffffff",
},
},
},
})
);
const controllers = new Controller([new ClientController(client, app)]);
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);
app.listen(config.PORT, () =>
console.log(`Legica bot API listening on port ${config.PORT}!`)
);

View File

@@ -2,13 +2,15 @@ import { Client, MessageEmbed, TextChannel } from "discord.js";
import * as cron from "cron";
import axios from "axios";
import cheerio from "cheerio";
import { Express } from "express";
import { Router } from "express";
import { IController, Legica } from "@models";
import { APP_VERSION, config } from "@constants";
import { config } from "@constants";
import basicAuth from "express-basic-auth";
class ClientController implements IController {
private legicaTask: cron.CronJob | null = null;
constructor(private client: Client, private app: Express) {}
public path: string = "task";
constructor(private client: Client) {}
public register = (): void => {
this.client.on("ready", (): void => {
@@ -20,24 +22,30 @@ class ClientController implements IController {
"utc"
);
});
};
this.app.get("", (_, res) => {
public registerRouter = (): Router => {
const router = Router();
router.use(
basicAuth({
users: {
admin: config.PASSWORD,
},
})
);
router.get("", (_, res) => {
res.send(this.legicaTask?.running);
});
this.app.get("/next", (_, res) => {
router.get("next", (_, res) => {
if (!this.legicaTask?.running) {
res.status(400).send("Task is not running.");
} else {
res.send(this.legicaTask.nextDate().toISO());
}
});
this.app.get("/version", (_, res) => {
res.send(APP_VERSION);
});
this.app.post("/start", (_, res) => {
router.post("", (_, res) => {
if (this.legicaTask?.running) {
res.status(400).send("Task already running.");
} else {
@@ -45,8 +53,7 @@ class ClientController implements IController {
res.send("Task started.");
}
});
this.app.post("/stop", (_, res) => {
router.delete("", (_, res) => {
if (!this.legicaTask?.running) {
res.status(400).send("Task already stopped.");
} else {
@@ -55,7 +62,7 @@ class ClientController implements IController {
}
});
this.app.post("/post-next", async (_, res) => {
router.post("send-latest", async (_, res) => {
try {
await this.sendNextMessage();
res.send(true);
@@ -64,7 +71,7 @@ class ClientController implements IController {
}
});
this.app.post("/post", async (req, res) => {
router.post("send", async (req, res) => {
try {
const url = req.body.url;
await this.sendMessage(url);
@@ -73,6 +80,7 @@ class ClientController implements IController {
res.status(400).send(err);
}
});
return router;
};
private sendNextMessage = async (): Promise<void> => {

View File

@@ -1,11 +1,13 @@
import { IController } from "models";
import { Express } from "express";
class Controller {
constructor(private controllers: IController[]) {}
constructor(private app: Express, private controllers: IController[]) {}
public register = (): void => {
this.controllers?.forEach((controller) => {
controller.register();
this.app.use(controller.path || "", controller.registerRouter());
});
};
}

View File

@@ -1,3 +1,7 @@
import { Router } from "express";
export interface IController {
register(): void;
registerRouter(): Router;
path: string;
}