endpoint returning invites based on hostname

This commit is contained in:
2025-07-11 20:09:14 +02:00
parent add142864e
commit 1affc4d3bc
7 changed files with 84 additions and 5 deletions
@@ -109,4 +109,8 @@ public class Main {
return discordInviteManager;
}
public static Storage getStorage() {
return storage;
}
}
@@ -3,6 +3,7 @@ package me.youhavetrouble.inviter.discord;
import org.jetbrains.annotations.Nullable;
public record GuildSettings(
long guildId,
boolean apiEnabled,
@Nullable String apiHostname
) {
@@ -38,8 +38,8 @@ public class GetDiscordInviteByGuildId implements EndpointHandler {
return;
}
DiscordInviteManager storage = Main.getDiscordInviteMenager();
DiscordInvite invite = storage.getInvite(guildIdLong);
DiscordInviteManager inviteManager = Main.getDiscordInviteMenager();
DiscordInvite invite = inviteManager.getInvite(guildIdLong);
if (invite == null) {
exchange.sendResponseHeaders(404, -1); // Not Found
@@ -13,6 +13,7 @@ public class HandlerKernel implements HttpHandler {
private final Set<EndpointHandler> handlers = new HashSet<>();
public HandlerKernel() {
handlers.add(new MainEndpoint());
handlers.add(new GetDiscordInviteByGuildId());
}
@@ -20,7 +21,7 @@ public class HandlerKernel implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
if (path.endsWith("/") && path.length() > 1) path = path.substring(0, path.length() - 1);
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", "*");
exchange.getResponseHeaders().set("Access-Control-Allow-Methods", "*");
exchange.getResponseHeaders().set("Access-Control-Allow-Headers", "Content-Type");
@@ -0,0 +1,49 @@
package me.youhavetrouble.inviter.http.endpoints;
import com.sun.net.httpserver.HttpExchange;
import me.youhavetrouble.inviter.Main;
import me.youhavetrouble.inviter.discord.DiscordInvite;
import me.youhavetrouble.inviter.discord.DiscordInviteManager;
import me.youhavetrouble.inviter.discord.GuildSettings;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.regex.Pattern;
public class MainEndpoint implements EndpointHandler {
private final Pattern pathPattern = Pattern.compile("^/$");
@NotNull
@Override
public Pattern pathPattern() {
return pathPattern;
}
@Override
public void handle(@NotNull HttpExchange exchange) throws IOException {
if (!exchange.getRequestMethod().equals("GET")) {
exchange.sendResponseHeaders(405, -1); // Method Not Allowed
return;
}
String host = exchange.getRemoteAddress().getHostName();
GuildSettings guildSettings = Main.getStorage().getGuildSettings(host);
if (guildSettings == null) {
exchange.sendResponseHeaders(404, -1); // Not Found
return;
}
DiscordInviteManager inviteManager = Main.getDiscordInviteMenager();
DiscordInvite invite = inviteManager.getInvite(guildSettings.guildId());
if (invite == null) {
exchange.sendResponseHeaders(404, -1); // Not Found
return;
}
exchange.getResponseHeaders().set("Location", "https://discord.gg/" + invite.code());
exchange.sendResponseHeaders(307, -1);
}
}
@@ -64,15 +64,37 @@ public class SqliteStorage implements Storage {
if (resultSet.next()) {
boolean apiEnabled = resultSet.getBoolean("api_enabled");
String apiHostname = resultSet.getString("api_hostname");
return new GuildSettings(apiEnabled, apiHostname);
return new GuildSettings(guildId, apiEnabled, apiHostname);
}
return new GuildSettings(false, null);
return new GuildSettings(guildId,false, null);
} catch (SQLException e) {
throw new RuntimeException("Failed to retrieve guild settings", e);
}
}
@Nullable
@Override
public GuildSettings getGuildSettings(@NotNull String hostname) {
try (Connection connection = dataSource.getConnection()) {
var statement = connection.prepareStatement(
"SELECT * FROM guild_settings WHERE api_hostname = ?"
);
statement.setString(1, hostname);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
long guildId = resultSet.getLong("guild_id");
boolean apiEnabled = resultSet.getBoolean("api_enabled");
return new GuildSettings(guildId, apiEnabled, hostname);
}
return null; // No settings found for the given hostname
} catch (SQLException e) {
throw new RuntimeException("Failed to retrieve guild settings by hostname", e);
}
}
@Override
public void saveDefaultGuildSettings(long guildId) {
try (Connection connection = dataSource.getConnection()) {
@@ -9,6 +9,8 @@ public interface Storage {
@NotNull GuildSettings getGuildSettings(long guildId);
@Nullable GuildSettings getGuildSettings(@NotNull String hostname);
void saveDefaultGuildSettings(long guildId);
void updateDiscordApiEnabled(long guildId, boolean enabled);