From b513bb5a06fb08e71267d8da6270f39e478ffd2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Theodor=20Angerg=C3=A5rd?= Date: Sun, 18 Aug 2024 11:27:16 +0200 Subject: [PATCH] Update --- .../primary/web/ThrottlingController.java | 44 +++++++++++++++++ .../ThrottlingManagementFacade.java | 48 +++++++++++++++++++ .../resources/templates/common/header.html | 3 ++ .../templates/throttling/throttling.html | 25 ++++++++++ 4 files changed, 120 insertions(+) create mode 100644 app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThrottlingController.java create mode 100644 app/src/main/java/it/chalmers/gamma/app/throttling/ThrottlingManagementFacade.java create mode 100644 app/src/main/resources/templates/throttling/throttling.html diff --git a/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThrottlingController.java b/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThrottlingController.java new file mode 100644 index 000000000..b5d2b7a30 --- /dev/null +++ b/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThrottlingController.java @@ -0,0 +1,44 @@ +package it.chalmers.gamma.adapter.primary.web; + +import it.chalmers.gamma.app.throttling.ThrottlingManagementFacade; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class ThrottlingController { + + private final ThrottlingManagementFacade throttlingManagementFacade; + + public ThrottlingController(ThrottlingManagementFacade throttlingManagementFacade) { + this.throttlingManagementFacade = throttlingManagementFacade; + } + + @GetMapping("/throttling") + public ModelAndView getThrottling( + @RequestHeader(value = "HX-Request", required = false) boolean htmxRequest) { + ModelAndView mv = new ModelAndView(); + if (htmxRequest) { + mv.setViewName("throttling/throttling"); + } else { + mv.setViewName("index"); + mv.addObject("page", "throttling/throttling"); + } + + mv.addObject("throttling", throttlingManagementFacade.getAll()); + + return mv; + } + + @DeleteMapping("/throttling/{key}") + public ModelAndView deleteActivationCode( + @RequestHeader(value = "HX-Request", required = false) boolean htmxRequest, + @PathVariable("key") String key) { + this.throttlingManagementFacade.deleteKey(key); + + return new ModelAndView("common/empty"); + } +} diff --git a/app/src/main/java/it/chalmers/gamma/app/throttling/ThrottlingManagementFacade.java b/app/src/main/java/it/chalmers/gamma/app/throttling/ThrottlingManagementFacade.java new file mode 100644 index 000000000..9e37f22ef --- /dev/null +++ b/app/src/main/java/it/chalmers/gamma/app/throttling/ThrottlingManagementFacade.java @@ -0,0 +1,48 @@ +package it.chalmers.gamma.app.throttling; + +import static it.chalmers.gamma.app.authentication.AccessGuard.isAdmin; + +import it.chalmers.gamma.app.Facade; +import it.chalmers.gamma.app.authentication.AccessGuard; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +@Service +public class ThrottlingManagementFacade extends Facade { + + private final StringRedisTemplate redisTemplate; + + public ThrottlingManagementFacade(AccessGuard accessGuard, StringRedisTemplate redisTemplate) { + super(accessGuard); + this.redisTemplate = redisTemplate; + } + + public record KeyValue(String key, String value) {} + + public List getAll() { + super.accessGuard.require(isAdmin()); + + Set keys = redisTemplate.keys("throttle:*"); + List result = new ArrayList<>(); + + if (keys != null) { + for (String key : keys) { + String value = redisTemplate.opsForValue().get(key); + String trimmedKey = key.substring("throttle:".length()); + result.add(new KeyValue(trimmedKey, value)); + } + } + + return result; + } + + public void deleteKey(String key) { + super.accessGuard.require(isAdmin()); + + String prefixedKey = "throttle:" + key; + redisTemplate.delete(prefixedKey); + } +} diff --git a/app/src/main/resources/templates/common/header.html b/app/src/main/resources/templates/common/header.html index adfa55c14..eaa0e4f2b 100644 --- a/app/src/main/resources/templates/common/header.html +++ b/app/src/main/resources/templates/common/header.html @@ -51,6 +51,9 @@
  • Allow lists
  • +
  • + Throttling +
  • diff --git a/app/src/main/resources/templates/throttling/throttling.html b/app/src/main/resources/templates/throttling/throttling.html new file mode 100644 index 000000000..9bb345b70 --- /dev/null +++ b/app/src/main/resources/templates/throttling/throttling.html @@ -0,0 +1,25 @@ +
    +
    + + + + + + + + + + + + + + + +
    KeyValue
    +
    +
    + + +
    +
    +
    \ No newline at end of file