Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cannot be accessed for a long time during startup #3300

Merged
merged 16 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
26 changes: 14 additions & 12 deletions src/main/java/run/halo/app/core/extension/content/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import java.time.Instant;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -48,8 +49,9 @@ public class Post extends AbstractExtension {
public static final String ARCHIVE_YEAR_LABEL = "content.halo.run/archive-year";

public static final String ARCHIVE_MONTH_LABEL = "content.halo.run/archive-month";
public static final String ARCHIVE_DAY_LABEL = "content.halo.run/archive-day";

@Schema(required = true)
@Schema(requiredMode = RequiredMode.REQUIRED)
private PostSpec spec;

@Schema
Expand Down Expand Up @@ -81,10 +83,10 @@ public static boolean isPublished(MetadataOperator metadata) {

@Data
public static class PostSpec {
@Schema(required = true, minLength = 1)
@Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
private String title;

@Schema(required = true, minLength = 1)
@Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
private String slug;

/**
Expand All @@ -102,27 +104,27 @@ public static class PostSpec {

private String cover;

@Schema(required = true, defaultValue = "false")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean deleted;

@Schema(required = true, defaultValue = "false")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean publish;

private Instant publishTime;

@Schema(required = true, defaultValue = "false")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean pinned;

@Schema(required = true, defaultValue = "true")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "true")
private Boolean allowComment;

@Schema(required = true, defaultValue = "PUBLIC")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "PUBLIC")
private VisibleEnum visible;

@Schema(required = true, defaultValue = "0")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "0")
private Integer priority;

@Schema(required = true)
@Schema(requiredMode = RequiredMode.REQUIRED)
private Excerpt excerpt;

private List<String> categories;
Expand All @@ -134,7 +136,7 @@ public static class PostSpec {

@Data
public static class PostStatus {
@Schema(required = true)
@Schema(requiredMode = RequiredMode.REQUIRED)
private String phase;

@Schema
Expand Down Expand Up @@ -164,7 +166,7 @@ public ConditionList getConditionsOrDefault() {
@Data
public static class Excerpt {

@Schema(required = true, defaultValue = "true")
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "true")
private Boolean autoGenerate;

private String raw;
Expand Down
Loading