-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThrottlingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/it/chalmers/gamma/app/throttling/ThrottlingManagementFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KeyValue> getAll() { | ||
super.accessGuard.require(isAdmin()); | ||
|
||
Set<String> keys = redisTemplate.keys("throttle:*"); | ||
List<KeyValue> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
app/src/main/resources/templates/throttling/throttling.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<header th:replace="~{common/header}"></header> | ||
<main> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Key</th> | ||
<th>Value</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr th:each="t : ${throttling}"> | ||
<td th:text="${t.key()}"></td> | ||
<td th:text="${t.value()}"></td> | ||
<td class="table-align-right"> | ||
<form th:data-hx-post="|/throttling/${t.key()}|" data-hx-target="closest tr" data-hx-swap="delete"> | ||
<div th:replace="~{common/form-csrf}"></div> | ||
<input type="hidden" name="_method" value="delete" /> | ||
<button class="outline contrast" data-loading-disable>Delete</button> | ||
</form> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</main> |