etags attempt #2

This commit is contained in:
Lee 2024-04-19 21:00:08 +01:00
parent 46d4a53b11
commit 1cfcce4806
11 changed files with 31 additions and 21 deletions

View File

@ -12,8 +12,6 @@ import java.net.http.HttpClient;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Log4j2 @Log4j2
@SpringBootApplication @SpringBootApplication

View File

@ -6,9 +6,11 @@ import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -28,6 +30,14 @@ public class Config {
INSTANCE = this; INSTANCE = this;
} }
@Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setName("etagFilter");
return filterRegistrationBean;
}
@Bean @Bean
public WebMvcConfigurer configureCors() { public WebMvcConfigurer configureCors() {
return new WebMvcConfigurer() { return new WebMvcConfigurer() {

View File

@ -29,7 +29,6 @@ public class MojangController {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES).cachePublic()) .cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES).cachePublic())
.eTag(String.valueOf(status.hashCode()))
.body(status); .body(status);
} }
} }

View File

@ -35,7 +35,6 @@ public class PlayerController {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
.eTag(String.valueOf(player.hashCode()))
.body(player); .body(player);
} }
@ -47,7 +46,6 @@ public class PlayerController {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(6, TimeUnit.HOURS).cachePublic()) .cacheControl(CacheControl.maxAge(6, TimeUnit.HOURS).cachePublic())
.eTag(String.valueOf(player.hashCode()))
.body(player); .body(player);
} }
@ -67,7 +65,6 @@ public class PlayerController {
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
.contentType(MediaType.IMAGE_PNG) .contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername())) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername()))
.eTag(String.valueOf(player.hashCode()))
.body(playerService.getSkinPart(player, part, overlays, size).getBytes()); .body(playerService.getSkinPart(player, part, overlays, size).getBytes());
} }
} }

View File

@ -12,7 +12,6 @@ import xyz.mcutils.backend.model.cache.CachedMinecraftServer;
import xyz.mcutils.backend.service.MojangService; import xyz.mcutils.backend.service.MojangService;
import xyz.mcutils.backend.service.ServerService; import xyz.mcutils.backend.service.ServerService;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -39,7 +38,6 @@ public class ServerController {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES).cachePublic()) .cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES).cachePublic())
.eTag(String.valueOf(server.hashCode()))
.body(server); .body(server);
} }
@ -55,7 +53,6 @@ public class ServerController {
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
.contentType(MediaType.IMAGE_PNG) .contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname))
.eTag(String.valueOf(Arrays.hashCode(favicon)))
.body(favicon); .body(favicon);
} }
@ -65,7 +62,6 @@ public class ServerController {
@Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) { @Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
.eTag(String.valueOf(hostname.hashCode()))
.body(Map.of( .body(Map.of(
"blocked", mojangService.isServerBlocked(hostname) "blocked", mojangService.isServerBlocked(hostname)
)); ));

View File

@ -1,8 +1,10 @@
package xyz.mcutils.backend.model.cache; package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import lombok.EqualsAndHashCode;
import lombok.*; import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.common.CachedResponse; import xyz.mcutils.backend.common.CachedResponse;

View File

@ -2,7 +2,10 @@ package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.*; import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.common.CachedResponse; import xyz.mcutils.backend.common.CachedResponse;

View File

@ -2,7 +2,9 @@ package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.*; import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.common.CachedResponse; import xyz.mcutils.backend.common.CachedResponse;

View File

@ -1,7 +1,9 @@
package xyz.mcutils.backend.model.cache; package xyz.mcutils.backend.model.cache;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*; import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.common.CachedResponse; import xyz.mcutils.backend.common.CachedResponse;

View File

@ -1,8 +1,9 @@
package xyz.mcutils.backend.model.mojang; package xyz.mcutils.backend.model.mojang;
import lombok.*; import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.Map; import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Setter @EqualsAndHashCode @Getter @Setter @EqualsAndHashCode

View File

@ -11,7 +11,9 @@ import lombok.extern.log4j.Log4j2;
import net.jodah.expiringmap.ExpirationPolicy; import net.jodah.expiringmap.ExpirationPolicy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import xyz.mcutils.backend.common.*; import xyz.mcutils.backend.common.EnvironmentUtils;
import xyz.mcutils.backend.common.ExpiringSet;
import xyz.mcutils.backend.common.WebRequest;
import xyz.mcutils.backend.model.cache.CachedEndpointStatus; import xyz.mcutils.backend.model.cache.CachedEndpointStatus;
import xyz.mcutils.backend.model.mojang.EndpointStatus; import xyz.mcutils.backend.model.mojang.EndpointStatus;
import xyz.mcutils.backend.model.token.MojangProfileToken; import xyz.mcutils.backend.model.token.MojangProfileToken;
@ -24,8 +26,6 @@ import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.Timer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Service @Log4j2 @Getter @Service @Log4j2 @Getter