diff --git a/README.md b/README.md index 73efe806..29b007b0 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ BOT_ID=id-of-your-bot CLIENT_SECRET=your-client-secret DISCORD_REDIRECT_URL=your-client-oauth2-redirect-url-with-identyfy-guilds.join DISCORD_SERVER_INVITE=your-discord-server-invite +DISCORD_WEBHOOK_URL=discord-webhook-used-in-fullstack-open-course +DISCORD_WEBHOOK_TOKEN=token-for-the-discord-webhook PORT=your-custom-backend-port SESSION_SECRET=server-session-secret BACKEND_SERVER_URL=backend-server-url-without-port diff --git a/src/server/app.js b/src/server/app.js index 337f8e69..acd23bf7 100644 --- a/src/server/app.js +++ b/src/server/app.js @@ -6,6 +6,7 @@ const discordAuthRoute = require("./routes/discordAuth"); const discordJoinRoute = require("./routes/join"); const facultyAuthRoute = require("./routes/authenticateFaculty"); const metricsRoute = require("./routes/metrics"); +const webhookProxyRoute = require("./routes/webhookProxy"); const defaultRouteHandler = require("./routes/defaultRouteHandler"); const defaultRouteErrorHandler = require("./routes/defaultRouteErrorHandler"); const flash = require("connect-flash"); @@ -38,6 +39,7 @@ module.exports = (sequelize) => { app.use(flash()); app.use("/discordAuth", discordAuthRoute); app.use("/join", discordJoinRoute); + app.use("/webhooks", webhookProxyRoute); app.use("/authenticate_faculty", facultyAuthRoute); app.use("/metrics", metricsRoute); diff --git a/src/server/routes/webhookProxy.js b/src/server/routes/webhookProxy.js new file mode 100644 index 00000000..32a39c56 --- /dev/null +++ b/src/server/routes/webhookProxy.js @@ -0,0 +1,22 @@ +require("dotenv").config() +const axios = require("axios") +const router = require("express").Router() + +router.get("/:webhookId", async (req, res) => { + // only allow POST + if (req.method === "POST") { + const webhookUrl = `${process.env.DISCORD_WEBHOOK_URL}/${req.params.webhookId}/${process.env.DISCORD_WEBHOOK_TOKEN}` + const webhookData = req.body + try { + await axios.post(webhookUrl, webhookData) + res.status(200).send("Webhook sent successfully") + } catch (error) { + console.error(error) + res.status(500).send("Internal server error") + } + } else { + res.status(405).send("Method not allowed") + } +}) + +module.exports = router