This commit is contained in:
Minecon724 2024-12-07 20:10:23 +01:00
parent 9b528042b3
commit 0618794491
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
9 changed files with 56 additions and 12 deletions

View file

@ -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");
}
}
}

View file

@ -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<Version> pluginVersions;
}
class Version {
static class Version {
public Version(String version, int servers) {
this.version = version;
this.servers = servers;

View file

@ -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")

View file

@ -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);

View file

@ -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<Long> 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();
}

View file

@ -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());

View file

@ -19,8 +19,14 @@ public class Plugin extends PanacheEntity {
public List<Server> 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();

View file

@ -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<PluginWithVersion> 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);

View file

@ -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