Skip to content

Commit

Permalink
Merge branch 'main' into fix/editor-freezing
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Nov 1, 2023
2 parents 2dabb0f + b9c0a1f commit 3444329
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ application-local.properties
!application/src/test/resources/themes/*.zip
!application/src/main/resources/themes/*.zip
application/src/main/resources/console/
application/src/main/resources/uc/
application/src/main/resources/presets/
39 changes: 32 additions & 7 deletions application/src/main/java/run/halo/app/config/WebFluxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.resource.EncodedResourceResolver;
import org.springframework.web.reactive.resource.PathResourceResolver;
import org.springframework.web.reactive.result.view.ViewResolutionResultHandler;
import org.springframework.web.reactive.result.view.ViewResolver;
import reactor.core.publisher.Mono;
import run.halo.app.console.ConsoleProxyFilter;
import run.halo.app.console.ProxyFilter;
import run.halo.app.console.WebSocketRequestPredicate;
import run.halo.app.core.extension.endpoint.CustomEndpoint;
import run.halo.app.core.extension.endpoint.CustomEndpointsBuilder;
Expand Down Expand Up @@ -104,11 +103,21 @@ RouterFunction<ServerResponse> consoleIndexRedirection() {
.and(path("/console/**").and(path("/console/assets/**").negate()))
.and(accept(MediaType.TEXT_HTML))
.and(new WebSocketRequestPredicate().negate());
return route(consolePredicate, this::serveConsoleIndex);
return route(consolePredicate,
request -> this.serveIndex(haloProp.getConsole().getLocation() + "index.html"));
}

private Mono<ServerResponse> serveConsoleIndex(ServerRequest request) {
var indexLocation = haloProp.getConsole().getLocation() + "index.html";
@Bean
RouterFunction<ServerResponse> ucIndexRedirect() {
var consolePredicate = method(HttpMethod.GET)
.and(path("/uc/**").and(path("/uc/assets/**").negate()))
.and(accept(MediaType.TEXT_HTML))
.and(new WebSocketRequestPredicate().negate());
return route(consolePredicate,
request -> this.serveIndex(haloProp.getUc().getLocation() + "index.html"));
}

private Mono<ServerResponse> serveIndex(String indexLocation) {
var indexResource = applicationContext.getResource(indexLocation);
try {
return ServerResponse.ok()
Expand Down Expand Up @@ -142,6 +151,15 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
.addResolver(new EncodedResourceResolver())
.addResolver(new PathResourceResolver());

// For uc assets
registry.addResourceHandler("/uc/assets/**")
.addResourceLocations(haloProp.getUc().getLocation() + "assets/")
.setCacheControl(cacheControl)
.setUseLastModified(useLastModified)
.resourceChain(true)
.addResolver(new EncodedResourceResolver())
.addResolver(new PathResourceResolver());

// Additional resource mappings
var staticResources = haloProp.getAttachment().getResourceMappings();
staticResources.forEach(staticResource -> {
Expand Down Expand Up @@ -172,7 +190,14 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {

@ConditionalOnProperty(name = "halo.console.proxy.enabled", havingValue = "true")
@Bean
ConsoleProxyFilter consoleProxyFilter() {
return new ConsoleProxyFilter(haloProp);
ProxyFilter consoleProxyFilter() {
return new ProxyFilter("/console/**", haloProp.getConsole().getProxy());
}


@ConditionalOnProperty(name = "halo.uc.proxy.enabled", havingValue = "true")
@Bean
ProxyFilter ucProxyFilter() {
return new ProxyFilter("/uc/**", haloProp.getUc().getProxy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import run.halo.app.infra.properties.ConsoleProperties.ProxyProperties;
import run.halo.app.infra.properties.HaloProperties;
import run.halo.app.infra.properties.ProxyProperties;

@Slf4j
public class ConsoleProxyFilter implements WebFilter {
public class ProxyFilter implements WebFilter {

private final ProxyProperties proxyProperties;

private final ServerWebExchangeMatcher consoleMatcher;

private final WebClient webClient;

public ConsoleProxyFilter(HaloProperties haloProperties) {
this.proxyProperties = haloProperties.getConsole().getProxy();
var consoleMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/console/**");
public ProxyFilter(String pattern, ProxyProperties proxyProperties) {
this.proxyProperties = proxyProperties;
var consoleMatcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, pattern);
consoleMatcher = new AndServerWebExchangeMatcher(consoleMatcher,
new NegatedServerWebExchangeMatcher(new WebSocketServerWebExchangeMatcher()));
this.consoleMatcher = consoleMatcher;
this.webClient = WebClient.create(proxyProperties.getEndpoint().toString());
log.info("Initialized ConsoleProxyFilter to proxy console");
log.debug("Initialized ProxyFilter to proxy {} to endpoint {}", pattern,
proxyProperties.getEndpoint());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Mono<Boolean> contains(Collection<String> source, Collection<String> cand
if (source.contains(SuperAdminInitializer.SUPER_ROLE_NAME)) {
return Mono.just(true);
}
return listDependencies(new HashSet<>(source), shouldFilterHidden(true))
return listDependencies(new HashSet<>(source), shouldFilterHidden(false))
.map(role -> role.getMetadata().getName())
.collect(Collectors.toSet())
.map(roleNames -> roleNames.containsAll(candidates));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package run.halo.app.infra.properties;

import jakarta.validation.Valid;
import java.net.URI;
import lombok.Data;

@Data
Expand All @@ -12,17 +11,4 @@ public class ConsoleProperties {
@Valid
private ProxyProperties proxy = new ProxyProperties();

@Data
public static class ProxyProperties {

/**
* Console endpoint in development environment to be proxied. e.g.: http://localhost:8090/
*/
private URI endpoint;

/**
* Indicates if the proxy behaviour is enabled. Default is false
*/
private boolean enabled = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class HaloProperties implements Validator {
@Valid
private final ConsoleProperties console = new ConsoleProperties();

@Valid
private final UcProperties uc = new UcProperties();

@Valid
private final ThemeProperties theme = new ThemeProperties();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package run.halo.app.infra.properties;

import java.net.URI;
import lombok.Data;

@Data
public class ProxyProperties {

/**
* Console endpoint in development environment to be proxied. e.g.: http://localhost:8090/
*/
private URI endpoint;

/**
* Indicates if the proxy behaviour is enabled. Default is false
*/
private boolean enabled = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package run.halo.app.infra.properties;

import jakarta.validation.Valid;
import lombok.Data;

@Data
public class UcProperties {

private String location = "classpath:/uc/";

@Valid
private ProxyProperties proxy = new ProxyProperties();

}
4 changes: 4 additions & 0 deletions application/src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ halo:
proxy:
endpoint: http://localhost:3000/
enabled: true
uc:
proxy:
endpoint: http://localhost:4000/
enabled: true
plugin:
runtime-mode: development # development, deployment
work-dir: ${user.home}/halo2-dev
Expand Down

0 comments on commit 3444329

Please sign in to comment.