Skip to content

Commit

Permalink
refactor: append all post feed to source list
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Dec 5, 2024
1 parent 534e225 commit 197a690
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ jobs:
contents: write
with:
app-id: app-KhIVw
skip-node-setup: true
node-version: "20"
pnpm-version: "9"
ui-path: "ui"
artifacts-path: 'app/build/libs'
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ jobs:
ci:
uses: halo-sigs/reusable-workflows/.github/workflows/plugin-ci.yaml@v1
with:
skip-node-setup: true
node-version: "20"
pnpm-version: "9"
ui-path: "ui"
artifacts-path: 'app/build/libs'
85 changes: 49 additions & 36 deletions app/src/main/java/run/halo/feed/FeedPluginEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
import run.halo.app.plugin.extensionpoint.ExtensionGetter;
import run.halo.feed.provider.PostRssProvider;

import java.util.ArrayList;

import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;

@Component
@AllArgsConstructor
public class FeedPluginEndpoint {
static final RequestPredicate ACCEPT_PREDICATE = accept(
MediaType.TEXT_XML,
MediaType.APPLICATION_RSS_XML
MediaType.TEXT_XML,
MediaType.APPLICATION_RSS_XML
);

private final PostRssProvider postRssProvider;
Expand All @@ -35,35 +37,46 @@ public class FeedPluginEndpoint {
@Bean
RouterFunction<ServerResponse> rssRouterFunction() {
return RouterFunctions.route()
.GET(path("/feed.xml").or(path("/rss.xml")).and(ACCEPT_PREDICATE),
request -> rssCacheManager.get("/rss.xml", postRssProvider.handler(request))
.flatMap(this::buildResponse)
)
.build();
.GET(path("/feed.xml").or(path("/rss.xml")).and(ACCEPT_PREDICATE),
request -> rssCacheManager.get("/rss.xml", postRssProvider.handler(request))
.flatMap(this::buildResponse)
)
.build();
}

@Bean
RouterFunction<ServerResponse> rssSourcesListerRouter() {
return RouterFunctions.route()
.GET("/apis/api.feed.halo.run/v1alpha1/rss-sources", this::listRssSources)
.build();
.GET("/apis/api.feed.halo.run/v1alpha1/rss-sources", this::listRssSources)
.build();
}

private Mono<ServerResponse> listRssSources(ServerRequest request) {
var externalUrl = externalUrlSupplier.getURL(request.exchange().getRequest()).toString();
return extensionGetter.getEnabledExtensions(RssRouteItem.class)
.concatMap(item -> {
var rssSource = RssSource.builder()
.displayName(item.displayName())
.description(item.description())
.example(item.example());
return item.pathPattern()
.map(pattern -> buildPathPattern(pattern, item.namespace()))
.doOnNext(path -> rssSource.pattern(externalUrl + path))
.then(Mono.fromSupplier(rssSource::build));
})
.collectList()
.flatMap(ServerResponse.ok()::bodyValue);
.concatMap(item -> {
var rssSource = RssSource.builder()
.displayName(item.displayName())
.description(item.description())
.example(item.example());
return item.pathPattern()
.map(pattern -> buildPathPattern(pattern, item.namespace()))
.doOnNext(path -> rssSource.pattern(externalUrl + path))
.then(Mono.fromSupplier(rssSource::build));
})
.collectList()
.flatMap(result -> {
var allPosts = RssSource.builder()
.pattern(externalUrl + "/rss.xml")
.displayName("订阅所有文章")
.description("会根据设置的文章数量返回最新的文章")
.example("https://example.com/rss.xml")
.build();
var sources = new ArrayList<>();
sources.add(allPosts);
sources.addAll(result);
return ServerResponse.ok().bodyValue(sources);
});
}

@Builder
Expand All @@ -78,20 +91,20 @@ RouterFunction<ServerResponse> additionalRssRouter() {
@NonNull
public Mono<HandlerFunction<ServerResponse>> route(@NonNull ServerRequest request) {
return pathMatcher.matches(request.exchange())
.filter(ServerWebExchangeMatcher.MatchResult::isMatch)
.flatMap(matched -> handleRequest(request));
.filter(ServerWebExchangeMatcher.MatchResult::isMatch)
.flatMap(matched -> handleRequest(request));
}

private Mono<HandlerFunction<ServerResponse>> handleRequest(ServerRequest request) {
return extensionGetter.getEnabledExtensions(RssRouteItem.class)
.concatMap(routeItem -> buildRequestPredicate(routeItem)
.map(requestPredicate -> new RouteItem(requestPredicate,
buildHandleFunction(routeItem))
)
.concatMap(routeItem -> buildRequestPredicate(routeItem)
.map(requestPredicate -> new RouteItem(requestPredicate,
buildHandleFunction(routeItem))
)
.filter(route -> route.requestPredicate.test(request))
.next()
.map(RouteItem::handler);
)
.filter(route -> route.requestPredicate.test(request))
.next()
.map(RouteItem::handler);
}

record RouteItem(RequestPredicate requestPredicate,
Expand All @@ -100,15 +113,15 @@ record RouteItem(RequestPredicate requestPredicate,

private HandlerFunction<ServerResponse> buildHandleFunction(RssRouteItem routeItem) {
return request -> rssCacheManager.get(request.path(), routeItem.handler(request))
.flatMap(item -> buildResponse(item));
.flatMap(item -> buildResponse(item));
}

private Mono<RequestPredicate> buildRequestPredicate(RssRouteItem routeItem) {
return routeItem.pathPattern()
.map(pathPattern -> path(
buildPathPattern(pathPattern, routeItem.namespace()))
.and(ACCEPT_PREDICATE)
);
.map(pathPattern -> path(
buildPathPattern(pathPattern, routeItem.namespace()))
.and(ACCEPT_PREDICATE)
);
}
};
}
Expand All @@ -133,6 +146,6 @@ private String buildPathPattern(String pathPattern, String namespace) {

private Mono<ServerResponse> buildResponse(String xml) {
return ServerResponse.ok().contentType(MediaType.TEXT_XML)
.bodyValue(xml);
.bodyValue(xml);
}
}

0 comments on commit 197a690

Please sign in to comment.