mirror of https://github.com/halo-dev/halo
feat: support obtaining the raw external URL configuration (#4150)
#### What type of PR is this? /kind feature /area core /milestone 2.7.x #### What this PR does / why we need it: 支持通过 ExternalUrlSupplier 获取 externalUrl 配置 #### Which issue(s) this PR fixes: Fixes #4149 #### Does this PR introduce a user-facing change? ```release-note 支持通过 ExternalUrlSupplier 获取 externalUrl 配置 ```pull/4153/head
parent
0d387eddf3
commit
9a0c52fb2a
|
@ -3,6 +3,7 @@ package run.halo.app.infra;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import org.springframework.http.HttpRequest;
|
import org.springframework.http.HttpRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,4 +31,11 @@ public interface ExternalUrlSupplier extends Supplier<URI> {
|
||||||
*/
|
*/
|
||||||
URL getURL(HttpRequest request);
|
URL getURL(HttpRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets user-configured external URL from <code>HaloProperties#getExternalUrl()</code>.
|
||||||
|
*
|
||||||
|
* @return user-configured external URL or null if it is not provided.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
URL getRaw();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
|
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
|
||||||
import org.springframework.http.HttpRequest;
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import run.halo.app.infra.properties.HaloProperties;
|
import run.halo.app.infra.properties.HaloProperties;
|
||||||
|
@ -54,6 +55,12 @@ public class HaloPropertiesExternalUrlSupplier implements ExternalUrlSupplier {
|
||||||
return externalUrl;
|
return externalUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public URL getRaw() {
|
||||||
|
return haloProperties.getExternalUrl();
|
||||||
|
}
|
||||||
|
|
||||||
private String getBasePath() {
|
private String getBasePath() {
|
||||||
var basePath = webFluxProperties.getBasePath();
|
var basePath = webFluxProperties.getBasePath();
|
||||||
if (!StringUtils.hasText(basePath)) {
|
if (!StringUtils.hasText(basePath)) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package run.halo.app.infra;
|
package run.halo.app.infra;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.mockito.Mockito.lenient;
|
import static org.mockito.Mockito.lenient;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
@ -105,4 +106,14 @@ class HaloPropertiesExternalUrlSupplierTest {
|
||||||
assertEquals(new URL("https://localhost/blog"), url);
|
assertEquals(new URL("https://localhost/blog"), url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRaw() throws MalformedURLException {
|
||||||
|
var fakeUri = URI.create("http://localhost/fake");
|
||||||
|
when(haloProperties.getExternalUrl()).thenReturn(fakeUri.toURL());
|
||||||
|
assertEquals(fakeUri.toURL(), externalUrl.getRaw());
|
||||||
|
|
||||||
|
when(haloProperties.getExternalUrl()).thenReturn(null);
|
||||||
|
assertNull(externalUrl.getRaw());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue