twitch_bot/web/index.js

35 lines
764 B
JavaScript
Raw Normal View History

2023-02-18 12:51:03 +01:00
const fastify = require("fastify")({ logger: true });
2023-02-04 12:56:15 +01:00
const { client } = require("../clients/twitch.js");
2023-02-18 12:51:03 +01:00
const path = require("path");
2023-02-04 17:06:41 +01:00
2023-02-18 12:51:03 +01:00
fastify.register(require("@fastify/static"), {
root: path.join(__dirname, "front/dist"),
wildcard: false,
2023-02-04 12:56:15 +01:00
});
2023-02-18 12:51:03 +01:00
fastify.get("/api/v1/commands", (req, res) => {
return client.commands;
2023-02-04 17:06:41 +01:00
});
2023-02-18 12:51:03 +01:00
fastify.get("/api/v1/commands/:name", (req, res) => {
if (!(req.params.name in client.commands)) {
return { error: "Command not found" };
}
2023-02-18 12:51:03 +01:00
return client.commands[req.params.name];
2023-02-04 17:06:41 +01:00
});
2023-02-18 12:51:03 +01:00
fastify.get("*", (req, res) => {
return res.sendFile("index.html");
2023-02-04 12:56:15 +01:00
});
2023-02-18 12:51:03 +01:00
const start = async () => {
try {
await fastify.listen({ port: 3000 });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();