-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add system info getter for plugin
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
api/src/main/java/run/halo/app/infra/SystemInfoGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package run.halo.app.infra; | ||
|
||
import com.github.zafarkhaja.semver.Version; | ||
import java.net.URL; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
import java.util.function.Supplier; | ||
import lombok.Data; | ||
import lombok.experimental.Accessors; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface SystemInfoGetter extends Supplier<Mono<SystemInfoGetter.SystemInfo>> { | ||
|
||
@Data | ||
@Accessors(chain = true) | ||
class SystemInfo { | ||
private String title; | ||
|
||
private String subtitle; | ||
|
||
private String logo; | ||
|
||
private String favicon; | ||
|
||
private URL url; | ||
|
||
private Version version; | ||
|
||
private SeoProp seo; | ||
|
||
private Locale locale; | ||
|
||
private TimeZone timeZone; | ||
|
||
private String activatedThemeName; | ||
} | ||
|
||
@Data | ||
@Accessors(chain = true) | ||
class SeoProp { | ||
private boolean blockSpiders; | ||
private String keywords; | ||
private String description; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
application/src/main/java/run/halo/app/infra/SystemInfoGetterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package run.halo.app.infra; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.boot.autoconfigure.web.ServerProperties; | ||
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SystemInfoGetterImpl implements SystemInfoGetter { | ||
private final SystemConfigurableEnvironmentFetcher environmentFetcher; | ||
private final SystemVersionSupplier systemVersionSupplier; | ||
private final ExternalUrlSupplier externalUrlSupplier; | ||
private final ServerProperties serverProperties; | ||
private final WebFluxProperties webFluxProperties; | ||
|
||
@Override | ||
public Mono<SystemInfo> get() { | ||
var systemInfo = new SystemInfo() | ||
.setVersion(systemVersionSupplier.get()) | ||
.setUrl(getExternalUrl()) | ||
// TODO populate locale and timezone from system settings in the future | ||
.setLocale(Locale.getDefault()) | ||
.setTimeZone(TimeZone.getDefault()); | ||
|
||
var basicMono = | ||
environmentFetcher.fetch(SystemSetting.Basic.GROUP, SystemSetting.Basic.class) | ||
.doOnNext(basic -> systemInfo.setTitle(basic.getTitle()) | ||
.setSubtitle(basic.getSubtitle()) | ||
.setLogo(basic.getLogo()) | ||
.setFavicon(basic.getFavicon()) | ||
); | ||
|
||
var seoMono = environmentFetcher.fetch(SystemSetting.Seo.GROUP, SystemSetting.Seo.class) | ||
.doOnNext(seo -> systemInfo.setSeo(new SeoProp() | ||
.setBlockSpiders(BooleanUtils.isTrue(seo.blockSpiders)) | ||
.setKeywords(seo.getKeywords()) | ||
.setDescription(seo.getDescription()) | ||
)); | ||
|
||
var themeMono = | ||
environmentFetcher.fetch(SystemSetting.Theme.GROUP, SystemSetting.Theme.class) | ||
.doOnNext(theme -> systemInfo.setActivatedThemeName(theme.getActive())); | ||
return Mono.when(basicMono, seoMono, themeMono) | ||
.thenReturn(systemInfo); | ||
} | ||
|
||
private URL getExternalUrl() { | ||
var url = externalUrlSupplier.getRaw(); | ||
if (url != null) { | ||
return url; | ||
} | ||
var port = serverProperties.getPort(); | ||
var basePath = StringUtils.defaultIfBlank(webFluxProperties.getBasePath(), "/"); | ||
try { | ||
var uriStr = "http://localhost:" + port + basePath; | ||
return URI.create(StringUtils.removeEnd(uriStr, "/")).toURL(); | ||
} catch (MalformedURLException e) { | ||
// Should not happen | ||
throw new RuntimeException("Cannot create URL from server properties.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters