Skip to content

Commit

Permalink
Add events controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Mar 2, 2024
1 parent 0c4939b commit 13c30a4
Show file tree
Hide file tree
Showing 11 changed files with 754 additions and 14 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ license {

repositories {
mavenCentral()
maven { url 'https://nexus.lucko.me/repository/maven-snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.36'
compileOnly 'net.luckperms:api:5.5-20231022.200451-2'
compileOnly 'net.luckperms:api:5.5-20240218.224354-2'
implementation 'io.javalin:javalin:4.6.4'
implementation 'io.javalin:javalin-openapi:4.6.4'
}
Expand Down
43 changes: 31 additions & 12 deletions src/main/java/me/lucko/luckperms/extension/rest/RestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.javalin.plugin.json.JavalinJackson;
import io.javalin.plugin.openapi.utils.OpenApiVersionUtil;
import me.lucko.luckperms.extension.rest.controller.ActionController;
import me.lucko.luckperms.extension.rest.controller.EventController;
import me.lucko.luckperms.extension.rest.controller.GroupController;
import me.lucko.luckperms.extension.rest.controller.PermissionHolderController;
import me.lucko.luckperms.extension.rest.controller.TrackController;
Expand All @@ -58,6 +59,7 @@
import static io.javalin.apibuilder.ApiBuilder.path;
import static io.javalin.apibuilder.ApiBuilder.post;
import static io.javalin.apibuilder.ApiBuilder.put;
import static io.javalin.apibuilder.ApiBuilder.sse;

/**
* An HTTP server that implements a REST API for LuckPerms.
Expand All @@ -67,6 +69,7 @@ public class RestServer implements AutoCloseable {

private final ObjectMapper objectMapper;
private final Javalin app;
private final AutoCloseable routesClosable;

public RestServer(LuckPerms luckPerms, int port) {
LOGGER.info("[REST] Starting server...");
Expand All @@ -78,13 +81,18 @@ public RestServer(LuckPerms luckPerms, int port) {

this.setupLogging(this.app);
this.setupErrorHandlers(this.app);
this.setupRoutes(this.app, luckPerms);
this.routesClosable = this.setupRoutes(this.app, luckPerms);

LOGGER.info("[REST] Startup complete! Listening on http://localhost:" + port);
}

@Override
public void close() {
try {
this.routesClosable.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
this.app.close();
}

Expand Down Expand Up @@ -114,7 +122,7 @@ private void setupErrorHandlers(Javalin app) {
});
}

private void setupRoutes(Javalin app, LuckPerms luckPerms) {
private AutoCloseable setupRoutes(Javalin app, LuckPerms luckPerms) {
app.get("/", ctx -> ctx.redirect("/docs/swagger-ui"));

app.get("health", ctx -> {
Expand All @@ -128,22 +136,20 @@ private void setupRoutes(Javalin app, LuckPerms luckPerms) {
GroupController groupController = new GroupController(luckPerms.getGroupManager(), messagingService, this.objectMapper);
TrackController trackController = new TrackController(luckPerms.getTrackManager(), luckPerms.getGroupManager(), messagingService, this.objectMapper);
ActionController actionController = new ActionController(luckPerms.getActionLogger());
EventController eventController = new EventController(luckPerms.getEventBus());

app.routes(() -> {
path("user", () -> {
get("lookup", userController::lookup);
setupControllerRoutes(userController);
});
path("group", () -> {
setupControllerRoutes(groupController);
});
path("track", () -> {
setupControllerRoutes(trackController);
});
path("action", () -> {
setupControllerRoutes(actionController);
});
path("group", () -> setupControllerRoutes(groupController));
path("track", () -> setupControllerRoutes(trackController));
path("action", () -> setupControllerRoutes(actionController));
path("event", () -> setupControllerRoutes(eventController));
});

return eventController;
}

private void setupControllerRoutes(PermissionHolderController controller) {
Expand Down Expand Up @@ -192,6 +198,14 @@ private void setupControllerRoutes(ActionController controller) {
post(controller::submit);
}

private void setupControllerRoutes(EventController controller) {
sse("log-broadcast", controller::logBroadcast);
sse("post-network-sync", controller::postNetworkSync);
sse("post-sync", controller::postSync);
sse("pre-network-sync", controller::preNetworkSync);
sse("pre-sync", controller::preSync);
}

private void setupAuth(JavalinConfig config) {
if (RestConfig.getBoolean("auth", false)) {
Set<String> keys = ImmutableSet.copyOf(
Expand Down Expand Up @@ -237,7 +251,12 @@ private void setupAuth(JavalinConfig config) {
}

private void setupLogging(Javalin app) {
app.before(ctx -> ctx.attribute("startTime", System.currentTimeMillis()));
app.before(ctx -> {
ctx.attribute("startTime", System.currentTimeMillis());
if (ctx.path().startsWith("/event/")) {
LOGGER.info("[REST] %s %s - %d".formatted(ctx.method(), ctx.path(), ctx.status()));
}
});
app.after(ctx -> {
//noinspection ConstantConditions
long startTime = ctx.attribute("startTime");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.actionlog.Action;

import java.io.IOException;
import java.util.UUID;

public class ActionSerializer extends JsonSerializer<Action> {

@Override
public void serialize(Action value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}


record Model(long timestamp, SourceModel source, TargetModel target, String description) {
static Model from(Action action) {
return new Model(
action.getTimestamp().getEpochSecond(),
SourceModel.from(action.getSource()),
TargetModel.from(action.getTarget()),
action.getDescription()
);
}
}

record SourceModel(UUID uniqueId, String name) {
static SourceModel from(Action.Source source) {
return new SourceModel(source.getUniqueId(), source.getName());
}
}

record TargetModel(UUID uniqueId, String name, Action.Target.Type type) {
static TargetModel from(Action.Target action) {
return new TargetModel(action.getUniqueId().orElse(null), action.getName(), action.getType());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind.event;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.event.log.LogBroadcastEvent;

import java.io.IOException;

public class LogBroadcastEventSerializer extends JsonSerializer<LogBroadcastEvent> {

@Override
public void serialize(LogBroadcastEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}

record Model(Action entry, LogBroadcastEvent.Origin origin) {
static Model from(LogBroadcastEvent event) {
return new Model(
event.getEntry(),
event.getOrigin()
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind.event;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.event.log.LogBroadcastEvent;
import net.luckperms.api.event.sync.PostNetworkSyncEvent;
import net.luckperms.api.event.sync.SyncType;

import java.io.IOException;
import java.util.UUID;

public class PostNetworkSyncEventSerializer extends JsonSerializer<PostNetworkSyncEvent> {

@Override
public void serialize(PostNetworkSyncEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}

record Model(UUID syncId, SyncType type, boolean didSyncOccur, UUID specificUserUniqueId) {
static Model from(PostNetworkSyncEvent event) {
return new Model(
event.getSyncId(),
event.getType(),
event.didSyncOccur(),
event.getSpecificUserUniqueId()
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.luckperms.extension.rest.bind.event;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.event.log.LogBroadcastEvent;
import net.luckperms.api.event.sync.PostSyncEvent;

import java.io.IOException;

public class PostSyncEventSerializer extends JsonSerializer<PostSyncEvent> {

@Override
public void serialize(PostSyncEvent value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writePOJO(Model.from(value));
}

record Model() {
static Model from(PostSyncEvent event) {
return new Model();
}
}

}
Loading

0 comments on commit 13c30a4

Please sign in to comment.