Skip to content

Commit 9c39c58

Browse files
committed
update to javalin 7
1 parent 5a213b6 commit 9c39c58

3 files changed

Lines changed: 54 additions & 50 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repositories {
2626

2727
dependencies {
2828
zenithProxy("com.zenith:ZenithProxy:$mc-SNAPSHOT")
29-
shade("io.javalin:javalin:6.7.0")
29+
shade("io.javalin:javalin:7.0.0")
3030

3131
// todo: remove when javalin updates to jackson 3
3232
shade("com.fasterxml.jackson.core:jackson-core:2.20.1")

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
plugin_version=1.0.5
1+
plugin_version=1.0.6
22
plugin_name=ZenithProxyWebAPI
33
mc=1.21.4
44
maven_group=dev.zenith.web

src/main/java/dev/zenith/web/api/WebServer.java

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import static dev.zenith.web.WebApiPlugin.LOG;
2121
import static dev.zenith.web.WebApiPlugin.PLUGIN_CONFIG;
22+
import static io.javalin.apibuilder.ApiBuilder.beforeMatched;
23+
import static io.javalin.apibuilder.ApiBuilder.post;
2224

2325
public class WebServer {
2426
private Javalin server;
@@ -58,55 +60,57 @@ private Javalin createServer() {
5860
var objectMapper = JavalinJackson.defaultMapper()
5961
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
6062
config.jsonMapper(new JavalinJackson(objectMapper, false));
61-
})
62-
.beforeMatched(ctx -> {
63-
if (PLUGIN_CONFIG.rateLimiter) {
64-
String ip = ctx.ip();
65-
synchronized (this) {
66-
int reqCount = rateLimitCache.get(ip, () -> 0);
67-
rateLimitCache.put(ip, reqCount + 1);
68-
if (reqCount >= PLUGIN_CONFIG.rateLimitRequestsPerMinute) {
69-
ctx.status(429);
70-
ctx.json(new AuthErrorResponse("Rate limit exceeded"));
71-
ctx.skipRemainingHandlers();
72-
LOG.warn("Rate limit exceeded for IP: {}", ip);
73-
return;
63+
config.routes.apiBuilder(() -> {
64+
beforeMatched(ctx -> {
65+
if (PLUGIN_CONFIG.rateLimiter) {
66+
String ip = ctx.ip();
67+
synchronized (this) {
68+
int reqCount = rateLimitCache.get(ip, () -> 0);
69+
rateLimitCache.put(ip, reqCount + 1);
70+
if (reqCount >= PLUGIN_CONFIG.rateLimitRequestsPerMinute) {
71+
ctx.status(429);
72+
ctx.json(new AuthErrorResponse("Rate limit exceeded"));
73+
ctx.skipRemainingHandlers();
74+
LOG.warn("Rate limit exceeded for IP: {}", ip);
75+
return;
76+
}
77+
}
7478
}
75-
}
76-
}
77-
var authHeaderValue = ctx.header("Authorization");
78-
if (authHeaderValue != null) {
79-
var expectedHeaderValue = PLUGIN_CONFIG.authToken;
80-
if (authHeaderValue.equals(expectedHeaderValue)) {
81-
// ok
82-
return;
83-
}
84-
}
85-
String reason = authHeaderValue == null
86-
? "Authorization header missing"
87-
: "Invalid auth token";
88-
ctx.json(new AuthErrorResponse(reason));
89-
ctx.status(401);
90-
ctx.skipRemainingHandlers();
91-
LOG.warn("Denied request from {}: {}", ctx.ip(), reason);
92-
})
93-
.post("/command", ctx -> {
94-
var req = ctx.bodyAsClass(CommandRequest.class);
95-
var command = req.command();
96-
var context = CommandContext.create(command, WebAPICommandSource.INSTANCE);
97-
LOG.info("{} executed command: {}", ctx.ip(), command);
98-
Globals.COMMAND.execute(context);
99-
context.getSource().logEmbed(context, context.getEmbed());
100-
String embedResponse = null;
101-
String embedResponseComponent = null;
102-
List<String> multiLineResponse = context.getMultiLineOutput();
103-
if (context.getEmbed().isTitlePresent()) {
104-
var embedComponent = EmbedSerializer.serialize(context.getEmbed());
105-
embedResponse = ComponentSerializer.serializePlain(embedComponent);
106-
embedResponseComponent = ComponentSerializer.serializeJson(embedComponent);
107-
}
108-
ctx.json(new CommandResponse(embedResponse, embedResponseComponent, multiLineResponse));
109-
ctx.status(200);
79+
var authHeaderValue = ctx.header("Authorization");
80+
if (authHeaderValue != null) {
81+
var expectedHeaderValue = PLUGIN_CONFIG.authToken;
82+
if (authHeaderValue.equals(expectedHeaderValue)) {
83+
// ok
84+
return;
85+
}
86+
}
87+
String reason = authHeaderValue == null
88+
? "Authorization header missing"
89+
: "Invalid auth token";
90+
ctx.json(new AuthErrorResponse(reason));
91+
ctx.status(401);
92+
ctx.skipRemainingHandlers();
93+
LOG.warn("Denied request from {}: {}", ctx.ip(), reason);
94+
});
95+
post("/command", ctx -> {
96+
var req = ctx.bodyAsClass(CommandRequest.class);
97+
var command = req.command();
98+
var context = CommandContext.create(command, WebAPICommandSource.INSTANCE);
99+
LOG.info("{} executed command: {}", ctx.ip(), command);
100+
Globals.COMMAND.execute(context);
101+
context.getSource().logEmbed(context, context.getEmbed());
102+
String embedResponse = null;
103+
String embedResponseComponent = null;
104+
List<String> multiLineResponse = context.getMultiLineOutput();
105+
if (context.getEmbed().isTitlePresent()) {
106+
var embedComponent = EmbedSerializer.serialize(context.getEmbed());
107+
embedResponse = ComponentSerializer.serializePlain(embedComponent);
108+
embedResponseComponent = ComponentSerializer.serializeJson(embedComponent);
109+
}
110+
ctx.json(new CommandResponse(embedResponse, embedResponseComponent, multiLineResponse));
111+
ctx.status(200);
112+
});
113+
});
110114
});
111115
}
112116
}

0 commit comments

Comments
 (0)