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
guqing 2023-06-29 15:04:12 +08:00 committed by GitHub
parent 0d387eddf3
commit 9a0c52fb2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -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();
} }

View File

@ -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)) {

View File

@ -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());
}
} }