|
19 | 19 |
|
20 | 20 | import static dev.zenith.web.WebApiPlugin.LOG; |
21 | 21 | import static dev.zenith.web.WebApiPlugin.PLUGIN_CONFIG; |
| 22 | +import static io.javalin.apibuilder.ApiBuilder.beforeMatched; |
| 23 | +import static io.javalin.apibuilder.ApiBuilder.post; |
22 | 24 |
|
23 | 25 | public class WebServer { |
24 | 26 | private Javalin server; |
@@ -58,55 +60,57 @@ private Javalin createServer() { |
58 | 60 | var objectMapper = JavalinJackson.defaultMapper() |
59 | 61 | .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
60 | 62 | 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 | + } |
74 | 78 | } |
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 | + }); |
110 | 114 | }); |
111 | 115 | } |
112 | 116 | } |
0 commit comments