diff --git a/.env.template b/.env.template index 8e2e9be..2450354 100644 --- a/.env.template +++ b/.env.template @@ -1,6 +1,23 @@ # To configure the project, rename this file to .env and fill in your secrets # configuration variables needed both ./bot and ./portal -BASE_URL="https://coolwebsite.com" + +# this parameter must be either http or https. For local testing this can be +# http, but on production it must be https +PROTOCOL="http" + +# just the domain name part of the URL, like "example.com". If under a subdomain, must +# also be included here. Can also be an IP address like "xxx.xxx.xxx.xxx" +HOST="172.20.0.5" + +# the port must be specified here. Leave the field empty for the system to pick +# the default port (i.e 80) +PORT="" + +# any subpath used must be entered here. If this is configured, must have a leading +# slash and no trailing slash (for example: "/casbot"). Must be left empty if no +# subpath is used. +SUBPATH="" + MONGO_DATABASE="casbot" MONGO_URI="mongodb://127.0.0.1:100" @@ -8,7 +25,6 @@ MONGO_URI="mongodb://127.0.0.1:100" DISCORD_TOKEN="" # needed by ./portal -PORT=80 CAS_LINK="https://cas.my-org.com/login" DISCORD_CLIENT_ID="" DISCORD_SECRET="" diff --git a/bot/main.py b/bot/main.py index b4ff3d4..027682b 100644 --- a/bot/main.py +++ b/bot/main.py @@ -44,7 +44,15 @@ TOKEN = os.getenv("DISCORD_TOKEN") MONGO_DATABASE = os.getenv("MONGO_DATABASE") MONGO_URI = os.getenv("MONGO_URI") -BASE_URL = os.getenv("BASE_URL") + +PROTOCOL = os.getenv("PROTOCOL") +HOST = os.getenv("HOST") + +PORT = os.getenv("PORT") if os.getenv("PORT") else "80" +_PORT_AS_SUFFIX = f":{PORT}" if os.getenv("PORT") else "" + +SUBPATH = os.getenv("SUBPATH") +BASE_URL = f"{PROTOCOL}://{HOST}{_PORT_AS_SUFFIX}{SUBPATH}" SERVER_CONFIG = ConfigParser() bot = commands.Bot(command_prefix=".") diff --git a/portal/app.js b/portal/app.js index 8bf0452..b81db7c 100644 --- a/portal/app.js +++ b/portal/app.js @@ -45,19 +45,19 @@ app.use( }), ); -app.get("/test", (req, res) => { +app.get(`${config.SUBPATH}/test`, (req, res) => { res.send("Hello World!"); }); -app.post("/webhooks/update", async (req, res) => { +app.post(`${config.SUBPATH}/webhooks/update`, async (req, res) => { res.send("This endpoint has been removed."); }); -app.get("/", (req, res) => { - res.redirect(`${config.BASE_URL}/discord`); +app.get(`${config.SUBPATH}/`, (req, res) => { + res.redirect(`${config.SUBPATH}/discord`); }); -app.get("/discord", (req, res) => { +app.get(`${config.SUBPATH}/discord`, (req, res) => { res.redirect( `https://discordapp.com/api/oauth2/authorize?client_id=${config.DISCORD_CLIENT_ID}&scope=identify&response_type=code&redirect_uri=${config.DISCORD_REDIRECT}`, ); @@ -96,7 +96,7 @@ async function makeQuery(code, redirect_uri) { return await response.json(); } -app.get("/discord/callback", async (req, res) => { +app.get(`${config.SUBPATH}/discord/callback`, async (req, res) => { /* Get user from discord */ if (!req.query.code) { res.send("You are not discord :angry:", 400); @@ -126,7 +126,7 @@ app.get("/discord/callback", async (req, res) => { req.session.discordId = user.id; - res.redirect(`${config.BASE_URL}/cas`); + res.redirect(`${config.SUBPATH}/cas`); }); const CAS = require("cas"); @@ -137,7 +137,7 @@ const cas = new CAS({ version: 2.0, }); -app.get("/cas", async (req, res) => { +app.get(`${config.SUBPATH}/cas`, async (req, res) => { if (!req.session.discordId) { res.send("Please first authenticate from Discord :angry:", 500); return; diff --git a/portal/index.js b/portal/index.js index 9b99462..1ad7fc4 100644 --- a/portal/index.js +++ b/portal/index.js @@ -13,5 +13,5 @@ mongoose.connect( ); app.listen(config.PORT, () => { - logger.info(`Server running on port ${config.PORT}`); + logger.info(`Server running on ${config.BASE_URL}`); }); diff --git a/portal/utils/config.js b/portal/utils/config.js index e62cfa4..c1bc74c 100644 --- a/portal/utils/config.js +++ b/portal/utils/config.js @@ -1,5 +1,12 @@ -const PORT = process.env.PORT; -const BASE_URL = process.env.BASE_URL; +const PROTOCOL = process.env.PROTOCOL; +const HOST = process.env.HOST; + +const PORT = process.env.PORT ? process.env.PORT : "80"; +const _PORT_AS_SUFFIX = process.env.PORT ? `:${PORT}` : ""; + +const SUBPATH = process.env.SUBPATH; + +const BASE_URL = `${PROTOCOL}://${HOST}${_PORT_AS_SUFFIX}${SUBPATH}`; const ATLAS_URL = `${process.env.MONGO_URI}/${process.env.MONGO_DATABASE}`; @@ -17,6 +24,7 @@ module.exports = { SECRET, CAS_LINK, PORT, + SUBPATH, BASE_URL, ATLAS_URL, DISCORD_CLIENT_ID,