Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Aug 18, 2024
1 parent 0431ea2 commit b513bb5
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
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");
}
}
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);
}
}
3 changes: 3 additions & 0 deletions app/src/main/resources/templates/common/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<li>
<a th:href="@{/allow-list}">Allow lists</a>
</li>
<li>
<a th:href="@{/throttling}">Throttling</a>
</li>
</th:block>
</ul>
</aside>
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/resources/templates/throttling/throttling.html
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>

0 comments on commit b513bb5

Please sign in to comment.