From 46ccdd89fffce2a50b06e113843856f81936eb31 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Sun, 20 Jul 2025 12:32:23 +0200 Subject: [PATCH] allow configurable base url --- docker-composer.override.example.yml | 1 + src/main/java/me/youhavetrouble/inviter/Main.java | 11 +++++++++++ .../http/endpoints/GetDiscordInviteByGuildId.java | 2 ++ .../inviter/http/endpoints/MainEndpoint.java | 1 + src/main/resources/template/guild-not-supported.html | 2 +- src/main/resources/template/index.html | 4 ++-- src/main/resources/template/invites-paused.html | 2 +- 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docker-composer.override.example.yml b/docker-composer.override.example.yml index 6f7c04a..b6fd4d4 100644 --- a/docker-composer.override.example.yml +++ b/docker-composer.override.example.yml @@ -7,3 +7,4 @@ services: # - ./logs:/app/logs # uncomment to get access to logs more easily environment: DISCORD_TOKEN: "your_discord_token_here" + BASE_URL: "https://example.com" # replace with your actual base URL. THis will display on the template pages diff --git a/src/main/java/me/youhavetrouble/inviter/Main.java b/src/main/java/me/youhavetrouble/inviter/Main.java index 2e347a5..9df7b82 100644 --- a/src/main/java/me/youhavetrouble/inviter/Main.java +++ b/src/main/java/me/youhavetrouble/inviter/Main.java @@ -22,6 +22,8 @@ public class Main { public static final Logger LOGGER = LoggerFactory.getLogger("Inviter"); + public static String baseUrl; + private static JDA jda; private static DiscordInviteManager discordInviteManager; private static ApiServer apiServer; @@ -31,6 +33,15 @@ public class Main { String token = System.getenv("DISCORD_TOKEN"); + String baseUrlRaw = System.getenv("BASE_URL"); + if (baseUrlRaw == null || baseUrlRaw.isEmpty()) { + baseUrl = "https://example.com"; + LOGGER.warn("BASE_URL environment variable is not set. Using default: {}", baseUrl); + } else { + if (baseUrlRaw.endsWith("/")) baseUrlRaw = baseUrlRaw.substring(0, baseUrlRaw.length() - 1); + baseUrl = baseUrlRaw; + } + if (token == null || token.isEmpty()) { LOGGER.error("Discord token is not set. Please set the DISCORD_TOKEN environment variable."); System.exit(1); diff --git a/src/main/java/me/youhavetrouble/inviter/http/endpoints/GetDiscordInviteByGuildId.java b/src/main/java/me/youhavetrouble/inviter/http/endpoints/GetDiscordInviteByGuildId.java index b10d412..d52fed9 100644 --- a/src/main/java/me/youhavetrouble/inviter/http/endpoints/GetDiscordInviteByGuildId.java +++ b/src/main/java/me/youhavetrouble/inviter/http/endpoints/GetDiscordInviteByGuildId.java @@ -26,6 +26,7 @@ public class GetDiscordInviteByGuildId implements EndpointHandler { try (InputStream resource = this.getClass().getResourceAsStream("/template/invites-paused.html")) { assert resource != null; invitesDisabledTemplate = new String(resource.readAllBytes()); + invitesDisabledTemplate = invitesDisabledTemplate.replaceAll("\\{\\{base_url}}", Main.baseUrl); } catch (IOException | NullPointerException e) { Main.LOGGER.warn("Failed to load template for invites disabled page", e); } @@ -35,6 +36,7 @@ public class GetDiscordInviteByGuildId implements EndpointHandler { try (InputStream resource = this.getClass().getResourceAsStream("/template/guild-not-supported.html")) { assert resource != null; botNotInGuildTemplate = new String(resource.readAllBytes()); + botNotInGuildTemplate = botNotInGuildTemplate.replaceAll("\\{\\{base_url}}", Main.baseUrl); } catch (IOException | NullPointerException e) { Main.LOGGER.warn("Failed to load template for guild not supported page", e); } diff --git a/src/main/java/me/youhavetrouble/inviter/http/endpoints/MainEndpoint.java b/src/main/java/me/youhavetrouble/inviter/http/endpoints/MainEndpoint.java index 88cc1a1..34794b8 100644 --- a/src/main/java/me/youhavetrouble/inviter/http/endpoints/MainEndpoint.java +++ b/src/main/java/me/youhavetrouble/inviter/http/endpoints/MainEndpoint.java @@ -20,6 +20,7 @@ public class MainEndpoint implements EndpointHandler { try (InputStream resource = this.getClass().getResourceAsStream("/template/index.html")) { rawTemplate = new String(resource.readAllBytes()); rawTemplate = rawTemplate.replaceAll("\\{\\{discord_app_id}}", Main.getJda().getSelfUser().getApplicationId()); + rawTemplate = rawTemplate.replaceAll("\\{\\{base_url}}", Main.baseUrl); } catch (IOException | NullPointerException e) { Main.LOGGER.warn("Failed to load template for main endpoint", e); } diff --git a/src/main/resources/template/guild-not-supported.html b/src/main/resources/template/guild-not-supported.html index d08fe3d..46a60b1 100644 --- a/src/main/resources/template/guild-not-supported.html +++ b/src/main/resources/template/guild-not-supported.html @@ -10,7 +10,7 @@ - + Discord Inviter diff --git a/src/main/resources/template/index.html b/src/main/resources/template/index.html index a03818b..6b11223 100644 --- a/src/main/resources/template/index.html +++ b/src/main/resources/template/index.html @@ -10,7 +10,7 @@ - + Discord Inviter @@ -117,7 +117,7 @@ Enable developer mode in your discord client and copy your guilds ID. -
  • Now you can use https://inviter.yht.one/invite/<your_guild_id>
  • +
  • Now you can use {{base_url}}/invite/<your_guild_id>
  • diff --git a/src/main/resources/template/invites-paused.html b/src/main/resources/template/invites-paused.html index ed552cf..e50d20b 100644 --- a/src/main/resources/template/invites-paused.html +++ b/src/main/resources/template/invites-paused.html @@ -10,7 +10,7 @@ - + Discord Inviter