File tree Expand file tree Collapse file tree
src/main/java/dev/zenith/web Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,4 +6,6 @@ public class WebAPIConfig {
66 public boolean enabled = true ;
77 public int port = 8080 ;
88 public String authToken = UUID .randomUUID ().toString ();
9+ public boolean rateLimiter = true ;
10+ public int rateLimitRequestsPerMinute = 30 ;
911}
Original file line number Diff line number Diff line change 88import dev .zenith .web .model .CommandRequest ;
99import dev .zenith .web .model .CommandResponse ;
1010import io .javalin .Javalin ;
11+ import io .javalin .http .util .NaiveRateLimit ;
1112import org .eclipse .jetty .util .thread .ExecutorThreadPool ;
1213
1314import java .util .List ;
15+ import java .util .concurrent .TimeUnit ;
1416
1517import static dev .zenith .web .WebApiPlugin .LOG ;
1618import static dev .zenith .web .WebApiPlugin .PLUGIN_CONFIG ;
@@ -66,6 +68,9 @@ private Javalin createServer() {
6668 LOG .warn ("Denied request from {}: {}" , ctx .ip (), reason );
6769 })
6870 .post ("/command" , ctx -> {
71+ if (PLUGIN_CONFIG .rateLimiter ) {
72+ NaiveRateLimit .requestPerTimeUnit (ctx , PLUGIN_CONFIG .rateLimitRequestsPerMinute , TimeUnit .MINUTES );
73+ }
6974 var req = ctx .bodyAsClass (CommandRequest .class );
7075 var command = req .command ();
7176 var context = CommandContext .create (command , WebAPICommandSource .INSTANCE );
You can’t perform that action at this time.
0 commit comments