Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature/3089
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java
  • Loading branch information
guqing committed Jan 11, 2023
2 parents b35bf2c + 2a70d59 commit d4979d9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
21 changes: 15 additions & 6 deletions src/main/java/run/halo/app/config/WebFluxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -45,12 +45,18 @@ public class WebFluxConfig implements WebFluxConfigurer {

private final HaloProperties haloProp;


private final WebProperties.Resources resourceProperties;

private final ApplicationContext applicationContext;

public WebFluxConfig(ObjectMapper objectMapper, HaloProperties haloProp,
public WebFluxConfig(ObjectMapper objectMapper,
HaloProperties haloProp,
WebProperties webProperties,
ApplicationContext applicationContext) {
this.objectMapper = objectMapper;
this.haloProp = haloProp;
this.resourceProperties = webProperties.getResources();
this.applicationContext = applicationContext;
}

Expand Down Expand Up @@ -118,19 +124,22 @@ private Mono<ServerResponse> redirectConsole(ServerRequest request) {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
var attachmentsRoot = haloProp.getWorkDir().resolve("attachments");
var cacheControl = CacheControl.maxAge(Duration.ofDays(365 / 2));
final var cacheControl = resourceProperties.getCache()
.getCachecontrol()
.toHttpCacheControl();
final var useLastModified = resourceProperties.getCache().isUseLastModified();

// Mandatory resource mapping
var uploadRegistration = registry.addResourceHandler("/upload/**")
.addResourceLocations(FILE_URL_PREFIX + attachmentsRoot.resolve("upload") + "/")
.setUseLastModified(true)
.setUseLastModified(useLastModified)
.setCacheControl(cacheControl);

// For console project
registry.addResourceHandler("/console/**")
.addResourceLocations(haloProp.getConsole().getLocation())
.setCacheControl(cacheControl)
.setUseLastModified(true)
.setUseLastModified(useLastModified)
.resourceChain(true)
.addResolver(new EncodedResourceResolver())
.addResolver(new PathResourceResolver());
Expand All @@ -149,7 +158,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
checkDirectoryTraversal(attachmentsRoot, path);
registration.addResourceLocations(FILE_URL_PREFIX + path + "/")
.setCacheControl(cacheControl)
.setUseLastModified(true);
.setUseLastModified(useLastModified);
});
});

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -25,10 +24,10 @@
import org.pf4j.PluginStatusProvider;
import org.pf4j.RuntimeMode;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
Expand Down Expand Up @@ -187,7 +186,9 @@ String getSystemVersion() {
}

@Bean
public RouterFunction<ServerResponse> pluginJsBundleRoute(HaloPluginManager haloPluginManager) {
public RouterFunction<ServerResponse> pluginJsBundleRoute(HaloPluginManager haloPluginManager,
WebProperties webProperties) {
var cacheProperties = webProperties.getResources().getCache();
return RouterFunctions.route()
.GET("/plugins/{name}/assets/console/{*resource}", request -> {
String pluginName = request.pathVariable("name");
Expand All @@ -197,13 +198,17 @@ public RouterFunction<ServerResponse> pluginJsBundleRoute(HaloPluginManager halo
if (jsBundle == null || !jsBundle.exists()) {
return ServerResponse.notFound().build();
}
var useLastModified = cacheProperties.isUseLastModified();
var bodyBuilder = ServerResponse.ok()
.cacheControl(cacheProperties.getCachecontrol().toHttpCacheControl());
try {
var lastModified = Instant.ofEpochMilli(jsBundle.lastModified());
return request.checkNotModified(lastModified)
.switchIfEmpty(Mono.defer(() -> ServerResponse.ok()
.cacheControl(CacheControl.maxAge(Duration.ofDays(365 / 2)))
.lastModified(lastModified)
if (useLastModified) {
var lastModified = Instant.ofEpochMilli(jsBundle.lastModified());
return request.checkNotModified(lastModified)
.switchIfEmpty(Mono.defer(() -> bodyBuilder.lastModified(lastModified)
.body(BodyInserters.fromResource(jsBundle))));
}
return bodyBuilder.body(BodyInserters.fromResource(jsBundle));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/run/halo/app/theme/ThemeConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

import java.io.IOException;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
Expand All @@ -34,20 +33,24 @@ public ThemeConfiguration(HaloProperties haloProperties) {
}

@Bean
public RouterFunction<ServerResponse> themeAssets() {
public RouterFunction<ServerResponse> themeAssets(WebProperties webProperties) {
var cacheProperties = webProperties.getResources().getCache();
return route(
GET("/themes/{themeName}/assets/{*resource}").and(accept(MediaType.TEXT_PLAIN)),
request -> {
var themeName = request.pathVariable("themeName");
var resource = request.pathVariable("resource");
var fsRes = new FileSystemResource(getThemeAssetsPath(themeName, resource));
var bodyBuilder = ServerResponse.ok()
.cacheControl(cacheProperties.getCachecontrol().toHttpCacheControl());
try {
var lastModified = Instant.ofEpochMilli(fsRes.lastModified());
return request.checkNotModified(lastModified)
.switchIfEmpty(Mono.defer(() -> ServerResponse.ok()
.cacheControl(CacheControl.maxAge(Duration.ofDays(356 / 2)))
.lastModified(lastModified)
.body(BodyInserters.fromResource(fsRes))));
if (cacheProperties.isUseLastModified()) {
var lastModified = Instant.ofEpochMilli(fsRes.lastModified());
return request.checkNotModified(lastModified)
.switchIfEmpty(Mono.defer(() -> bodyBuilder.lastModified(lastModified)
.body(BodyInserters.fromResource(fsRes))));
}
return bodyBuilder.body(BodyInserters.fromResource(fsRes));
} catch (IOException e) {
return Mono.error(e);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ spring:
enabled: always
thymeleaf:
cache: false
web:
resources:
cache:
cachecontrol:
no-cache: true
use-last-modified: false

halo:
console:
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ spring:
max-in-memory-size: 10MB
messages:
basename: config.i18n.messages
web:
resources:
cache:
cachecontrol:
max-age: 365d

halo:
external-url: "http://${server.address:localhost}:${server.port}"
Expand Down

0 comments on commit d4979d9

Please sign in to comment.