Skip to content

Commit

Permalink
perf: cannot be accessed for a long time during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Feb 14, 2023
1 parent d527204 commit 9f3f1dc
Show file tree
Hide file tree
Showing 82 changed files with 721 additions and 3,917 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,46 @@
import static org.springframework.web.util.UriUtils.encode;

import java.nio.charset.StandardCharsets;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import run.halo.app.core.extension.content.Category;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.core.extension.content.Constant;
import run.halo.app.extension.ExtensionUtil;
import run.halo.app.infra.ExternalUrlSupplier;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.router.PermalinkIndexAddCommand;
import run.halo.app.theme.router.PermalinkIndexDeleteCommand;
import run.halo.app.theme.router.PermalinkIndexUpdateCommand;
import run.halo.app.theme.router.PermalinkPatternProvider;
import run.halo.app.theme.router.PermalinkWatch;

/**
* @author guqing
* @since 2.0.0
*/
@Component
public class CategoryPermalinkPolicy
implements PermalinkPolicy<Category>, PermalinkWatch<Category> {
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Category.class);
private final ApplicationContext applicationContext;
private final PermalinkPatternProvider permalinkPatternProvider;
@RequiredArgsConstructor
public class CategoryPermalinkPolicy implements PermalinkPolicy<Category> {
public static final String DEFAULT_PERMALINK_PREFIX =
SystemSetting.ThemeRouteRules.empty().getCategories();

private final ExternalUrlSupplier externalUrlSupplier;

public CategoryPermalinkPolicy(ApplicationContext applicationContext,
PermalinkPatternProvider permalinkPatternProvider,
ExternalUrlSupplier externalUrlSupplier) {
this.applicationContext = applicationContext;
this.permalinkPatternProvider = permalinkPatternProvider;
this.externalUrlSupplier = externalUrlSupplier;
}
private final SystemConfigurableEnvironmentFetcher environmentFetcher;

@Override
public String permalink(Category category) {
Map<String, String> annotations = ExtensionUtil.nullSafeAnnotations(category);
String permalinkPrefix =
annotations.getOrDefault(Constant.PERMALINK_PATTERN_ANNO, DEFAULT_PERMALINK_PREFIX);
String slug = encode(category.getSpec().getSlug(), StandardCharsets.UTF_8);
String path = PathUtils.combinePath(pattern(), slug);
String path = PathUtils.combinePath(permalinkPrefix, slug);
return externalUrlSupplier.get()
.resolve(path)
.normalize().toString();
}

@Override
public String templateName() {
return DefaultTemplateEnum.CATEGORY.getValue();
}

@Override
public String pattern() {
return permalinkPatternProvider.getPattern(DefaultTemplateEnum.CATEGORY);
}

@Override
public void onPermalinkAdd(Category category) {
applicationContext.publishEvent(new PermalinkIndexAddCommand(this, getLocator(category),
category.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkUpdate(Category category) {
applicationContext.publishEvent(new PermalinkIndexUpdateCommand(this, getLocator(category),
category.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkDelete(Category category) {
applicationContext.publishEvent(
new PermalinkIndexDeleteCommand(this, getLocator(category)));
}

private ExtensionLocator getLocator(Category category) {
return new ExtensionLocator(gvk, category.getMetadata().getName(),
category.getSpec().getSlug());
return environmentFetcher.fetchRouteRules()
.map(SystemSetting.ThemeRouteRules::getCategories)
.blockOptional()
.orElse(DEFAULT_PERMALINK_PREFIX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public interface PermalinkPolicy<T extends AbstractExtension> {
new PropertyPlaceholderHelper("{", "}");

String permalink(T extension);

String templateName();

String pattern();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,45 @@
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.Map;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import run.halo.app.core.extension.content.Constant;
import run.halo.app.core.extension.content.Post;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ExtensionUtil;
import run.halo.app.infra.ExternalUrlSupplier;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.router.PermalinkIndexAddCommand;
import run.halo.app.theme.router.PermalinkIndexDeleteCommand;
import run.halo.app.theme.router.PermalinkIndexUpdateCommand;
import run.halo.app.theme.router.PermalinkPatternProvider;
import run.halo.app.theme.router.PermalinkWatch;

/**
* @author guqing
* @since 2.0.0
*/
@Component
public class PostPermalinkPolicy implements PermalinkPolicy<Post>, PermalinkWatch<Post> {
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Post.class);
@RequiredArgsConstructor
public class PostPermalinkPolicy implements PermalinkPolicy<Post> {
public static final String DEFAULT_PERMALINK_PATTERN =
SystemSetting.ThemeRouteRules.empty().getPost();
private static final NumberFormat NUMBER_FORMAT = new DecimalFormat("00");

private final PermalinkPatternProvider permalinkPatternProvider;
private final ApplicationContext applicationContext;
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
private final ExternalUrlSupplier externalUrlSupplier;

public PostPermalinkPolicy(PermalinkPatternProvider permalinkPatternProvider,
ApplicationContext applicationContext, ExternalUrlSupplier externalUrlSupplier) {
this.permalinkPatternProvider = permalinkPatternProvider;
this.applicationContext = applicationContext;
this.externalUrlSupplier = externalUrlSupplier;
}

@Override
public String permalink(Post post) {
return createPermalink(post, pattern());
}

@Override
public String templateName() {
return DefaultTemplateEnum.POST.getValue();
Map<String, String> annotations = ExtensionUtil.nullSafeAnnotations(post);
String permalinkPattern =
annotations.getOrDefault(Constant.PERMALINK_PATTERN_ANNO, DEFAULT_PERMALINK_PATTERN);
return createPermalink(post, permalinkPattern);
}

@Override
public String pattern() {
return permalinkPatternProvider.getPattern(DefaultTemplateEnum.POST);
}

@Override
public void onPermalinkAdd(Post post) {
if (!post.isPublished() || Objects.equals(true, post.getSpec().getDeleted())) {
return;
}
// publish when post is published and not deleted
applicationContext.publishEvent(new PermalinkIndexAddCommand(this, getLocator(post),
post.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkUpdate(Post post) {
applicationContext.publishEvent(new PermalinkIndexUpdateCommand(this, getLocator(post),
post.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkDelete(Post post) {
applicationContext.publishEvent(new PermalinkIndexDeleteCommand(this, getLocator(post)));
}

private ExtensionLocator getLocator(Post post) {
return new ExtensionLocator(gvk, post.getMetadata().getName(), post.getSpec().getSlug());
return environmentFetcher.fetchRouteRules()
.map(SystemSetting.ThemeRouteRules::getPost)
.blockOptional()
.orElse(DEFAULT_PERMALINK_PATTERN);
}

private String createPermalink(Post post, String pattern) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,47 @@
package run.halo.app.content.permalinks;

import java.nio.charset.StandardCharsets;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriUtils;
import run.halo.app.core.extension.content.Constant;
import run.halo.app.core.extension.content.Tag;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ExtensionUtil;
import run.halo.app.infra.ExternalUrlSupplier;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.router.PermalinkIndexAddCommand;
import run.halo.app.theme.router.PermalinkIndexDeleteCommand;
import run.halo.app.theme.router.PermalinkIndexUpdateCommand;
import run.halo.app.theme.router.PermalinkPatternProvider;
import run.halo.app.theme.router.PermalinkWatch;

/**
* @author guqing
* @since 2.0.0
*/
@Component
public class TagPermalinkPolicy implements PermalinkPolicy<Tag>, PermalinkWatch<Tag> {
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Tag.class);
private final PermalinkPatternProvider permalinkPatternProvider;
private final ApplicationContext applicationContext;

@RequiredArgsConstructor
public class TagPermalinkPolicy implements PermalinkPolicy<Tag> {
public static final String DEFAULT_PERMALINK_PREFIX =
SystemSetting.ThemeRouteRules.empty().getTags();
private final ExternalUrlSupplier externalUrlSupplier;

public TagPermalinkPolicy(PermalinkPatternProvider permalinkPatternProvider,
ApplicationContext applicationContext, ExternalUrlSupplier externalUrlSupplier) {
this.permalinkPatternProvider = permalinkPatternProvider;
this.applicationContext = applicationContext;
this.externalUrlSupplier = externalUrlSupplier;
}
private final SystemConfigurableEnvironmentFetcher environmentFetcher;

@Override
public String permalink(Tag tag) {
Map<String, String> annotations = ExtensionUtil.nullSafeAnnotations(tag);
String permalinkPrefix =
annotations.getOrDefault(Constant.PERMALINK_PATTERN_ANNO, DEFAULT_PERMALINK_PREFIX);

String slug = UriUtils.encode(tag.getSpec().getSlug(), StandardCharsets.UTF_8);
String path = PathUtils.combinePath(pattern(), slug);
String path = PathUtils.combinePath(permalinkPrefix, slug);
return externalUrlSupplier.get()
.resolve(path)
.normalize().toString();
}

@Override
public String templateName() {
return DefaultTemplateEnum.TAG.getValue();
}

@Override
public String pattern() {
return permalinkPatternProvider.getPattern(DefaultTemplateEnum.TAG);
}

@Override
public void onPermalinkAdd(Tag tag) {
applicationContext.publishEvent(new PermalinkIndexAddCommand(this, getLocator(tag),
tag.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkUpdate(Tag tag) {
applicationContext.publishEvent(new PermalinkIndexUpdateCommand(this, getLocator(tag),
tag.getStatusOrDefault().getPermalink()));
}

@Override
public void onPermalinkDelete(Tag tag) {
applicationContext.publishEvent(new PermalinkIndexDeleteCommand(this, getLocator(tag)));
}

private ExtensionLocator getLocator(Tag tag) {
return new ExtensionLocator(gvk, tag.getMetadata().getName(), tag.getSpec().getSlug());
return environmentFetcher.fetchRouteRules()
.map(SystemSetting.ThemeRouteRules::getTags)
.blockOptional()
.orElse(DEFAULT_PERMALINK_PREFIX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public enum Constant {
public static final String VERSION = "v1alpha1";

public static final String LAST_READ_TIME_ANNO = "content.halo.run/last-read-time";
public static final String PERMALINK_PATTERN_ANNO = "content.halo.run/permalink-pattern";
}
Loading

0 comments on commit 9f3f1dc

Please sign in to comment.