diff --git a/src/main/java/eu/m724/mstats/Startup.java b/src/main/java/eu/m724/mstats/Startup.java index 36b6ab3..555c53e 100644 --- a/src/main/java/eu/m724/mstats/Startup.java +++ b/src/main/java/eu/m724/mstats/Startup.java @@ -4,14 +4,23 @@ import eu.m724.mstats.api.service.PluginService; import io.quarkus.runtime.StartupEvent; import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; -import jakarta.transaction.Transactional; +import jakarta.persistence.EntityExistsException; public class Startup { @Inject PluginService pluginService; - @Transactional public void onStartup(@Observes StartupEvent event) { - pluginService.createPlugin("ploogin"); + try { + pluginService.createPlugin(1, "Tweaks724"); + } catch (EntityExistsException e) { + System.out.println("exists1"); + } + + try { + pluginService.createPlugin(2, "Giants"); + } catch (EntityExistsException e) { + System.out.println("exists2"); + } } } diff --git a/src/main/java/eu/m724/mstats/api/resource/PluginApiResource.java b/src/main/java/eu/m724/mstats/api/resource/PluginApiResource.java index 1bf0c40..ef10fa1 100644 --- a/src/main/java/eu/m724/mstats/api/resource/PluginApiResource.java +++ b/src/main/java/eu/m724/mstats/api/resource/PluginApiResource.java @@ -63,7 +63,7 @@ public class PluginApiResource { } - class StatsResponse { + static class StatsResponse { @JsonProperty("id") public Long id; @@ -80,7 +80,7 @@ public class PluginApiResource { public List pluginVersions; } - class Version { + static class Version { public Version(String version, int servers) { this.version = version; this.servers = servers; diff --git a/src/main/java/eu/m724/mstats/api/resource/ServerApiResource.java b/src/main/java/eu/m724/mstats/api/resource/ServerApiResource.java index 2400ae0..758933e 100644 --- a/src/main/java/eu/m724/mstats/api/resource/ServerApiResource.java +++ b/src/main/java/eu/m724/mstats/api/resource/ServerApiResource.java @@ -52,6 +52,17 @@ public class ServerApiResource { return Response.ok(heartbeatResponse).build(); } + @Path("/remove") + @POST + public Response remove() { + if (!identity.isAnonymous()) { + Server server = identity.getAttribute("server"); + serverService.removeServer(server); + } + + return Response.ok().build(); + } + public static class HeartbeatRequest { /** List of registered plugins, this is sent only on boot */ @JsonProperty("plugins") diff --git a/src/main/java/eu/m724/mstats/api/service/PluginService.java b/src/main/java/eu/m724/mstats/api/service/PluginService.java index 71dcb92..406a7ae 100644 --- a/src/main/java/eu/m724/mstats/api/service/PluginService.java +++ b/src/main/java/eu/m724/mstats/api/service/PluginService.java @@ -7,10 +7,11 @@ import jakarta.transaction.Transactional; @ApplicationScoped public class PluginService { @Transactional - public Plugin createPlugin(String name) { - return Plugin.createPlugin(name); + public Plugin createPlugin(long id, String name) { + return Plugin.createPlugin(id, name); } + @Transactional public Plugin getPlugin(long id) { return Plugin.findById(id); diff --git a/src/main/java/eu/m724/mstats/api/service/ServerService.java b/src/main/java/eu/m724/mstats/api/service/ServerService.java index 1921ad2..2ae0993 100644 --- a/src/main/java/eu/m724/mstats/api/service/ServerService.java +++ b/src/main/java/eu/m724/mstats/api/service/ServerService.java @@ -1,11 +1,14 @@ package eu.m724.mstats.api.service; import eu.m724.mstats.api.resource.ServerApiResource; +import eu.m724.mstats.orm.Plugin; import eu.m724.mstats.orm.Server; import jakarta.enterprise.context.ApplicationScoped; import jakarta.transaction.Transactional; import java.time.Instant; +import java.util.HashSet; +import java.util.Set; @ApplicationScoped public class ServerService { @@ -19,17 +22,30 @@ public class ServerService { server.persistAndFlush(); } + @Transactional + public void removeServer(Server server) { + server.delete(); + } + @Transactional public void heartbeat(Server server, ServerApiResource.HeartbeatRequest heartbeatRequest) { server = Server.findById(server.id); // this is necessary for some reason + Set unknownPlugins = new HashSet<>(); for (ServerApiResource.RunningPlugin plugin : heartbeatRequest.plugins) { - server.addPlugin(plugin.id, plugin.version); + Plugin p = server.addPlugin(plugin.id, plugin.version); + if (p == null) { + unknownPlugins.add(plugin.id); + } } if (heartbeatRequest.serverVersion != null) server.serverVersion = heartbeatRequest.serverVersion; + if (!unknownPlugins.isEmpty()) { + // TODO + } + server.heartbeat = Instant.now(); server.persistAndFlush(); } diff --git a/src/main/java/eu/m724/mstats/auth/MyHttpAuthenticationMechanism.java b/src/main/java/eu/m724/mstats/auth/MyHttpAuthenticationMechanism.java index 20d026b..f4588c7 100644 --- a/src/main/java/eu/m724/mstats/auth/MyHttpAuthenticationMechanism.java +++ b/src/main/java/eu/m724/mstats/auth/MyHttpAuthenticationMechanism.java @@ -41,6 +41,7 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis } return QuarkusSecurityIdentity.builder() + .setPrincipal(new QuarkusPrincipal("Anonymous")) // this is to fix an error in logging .setAnonymous(true) .build(); }).runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); diff --git a/src/main/java/eu/m724/mstats/orm/Plugin.java b/src/main/java/eu/m724/mstats/orm/Plugin.java index 961b405..dc1c0a3 100644 --- a/src/main/java/eu/m724/mstats/orm/Plugin.java +++ b/src/main/java/eu/m724/mstats/orm/Plugin.java @@ -19,8 +19,14 @@ public class Plugin extends PanacheEntity { public List servers = new ArrayList<>(); @Transactional - public static Plugin createPlugin(String name) { + public static Plugin createPlugin(long id, String name) { + if (Plugin.find("name", name).firstResultOptional().isPresent()) throw new EntityExistsException(); + Plugin plugin = new Plugin(); + if (id != -1) { + if (Plugin.findById(id) != null) throw new EntityExistsException(); + plugin.id = id; + } plugin.name = name; plugin.persistAndFlush(); diff --git a/src/main/java/eu/m724/mstats/orm/Server.java b/src/main/java/eu/m724/mstats/orm/Server.java index 186a112..f221f19 100644 --- a/src/main/java/eu/m724/mstats/orm/Server.java +++ b/src/main/java/eu/m724/mstats/orm/Server.java @@ -19,7 +19,7 @@ public class Server extends PanacheEntity { @Column(nullable = false) public byte[] token; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @ManyToMany(fetch = FetchType.EAGER) public List plugins = new ArrayList<>(); public String serverVersion; @@ -38,7 +38,7 @@ public class Server extends PanacheEntity { return null; if (plugins.stream().anyMatch(pwv -> pwv.plugin.id == id)) - return null; + return plugin; plugins.add(new PluginWithVersion(plugin, version)); plugin.servers.add(this); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3979d2d..d7f5f47 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,5 +2,5 @@ quarkus.hibernate-orm.database.generation=update quarkus.datasource.db-kind=h2 quarkus.datasource.username=username-default -quarkus.datasource.jdbc.url=jdbc:h2:mem:default +quarkus.datasource.jdbc.url=jdbc:h2:./db:default quarkus.datasource.jdbc.max-size=13 \ No newline at end of file