Skip to content

Commit

Permalink
Improved subpath support, start env var docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed May 13, 2024
1 parent be027bd commit 100c2ea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
20 changes: 18 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# 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"

# needed by ./bot
DISCORD_TOKEN=""

# needed by ./portal
PORT=80
CAS_LINK="https://cas.my-org.com/login"
DISCORD_CLIENT_ID=""
DISCORD_SECRET=""
Expand Down
10 changes: 9 additions & 1 deletion bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=".")
Expand Down
16 changes: 8 additions & 8 deletions portal/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion portal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
12 changes: 10 additions & 2 deletions portal/utils/config.js
Original file line number Diff line number Diff line change
@@ -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}`;

Expand All @@ -17,6 +24,7 @@ module.exports = {
SECRET,
CAS_LINK,
PORT,
SUBPATH,
BASE_URL,
ATLAS_URL,
DISCORD_CLIENT_ID,
Expand Down

0 comments on commit 100c2ea

Please sign in to comment.