mirror of https://github.com/halo-dev/halo
Refactor CustomEndpoint for customizing GroupVersion (#2429)
#### What type of PR is this? /kind improvement /area core /milestone 2.0.0 #### What this PR does / why we need it: Refactor CustomEndpoint for customizing GroupVersion. #### Does this PR introduce a user-facing change? ```release-note None ```pull/2438/head
parent
a39cf2645e
commit
1714f8edb2
|
@ -65,8 +65,7 @@ public class WebFluxConfig implements WebFluxConfigurer {
|
||||||
@Bean
|
@Bean
|
||||||
RouterFunction<ServerResponse> customEndpoints(ApplicationContext context) {
|
RouterFunction<ServerResponse> customEndpoints(ApplicationContext context) {
|
||||||
var builder = new CustomEndpointsBuilder();
|
var builder = new CustomEndpointsBuilder();
|
||||||
context.getBeansOfType(CustomEndpoint.class).values()
|
context.getBeansOfType(CustomEndpoint.class).values().forEach(builder::add);
|
||||||
.forEach(customEndpoint -> builder.add(customEndpoint.endpoint()));
|
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,19 @@ package run.halo.app.core.extension.endpoint;
|
||||||
|
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
|
import run.halo.app.extension.GroupVersion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterFunction provider for custom endpoints.
|
* RouterFunction provider for custom endpoints.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
|
||||||
public interface CustomEndpoint {
|
public interface CustomEndpoint {
|
||||||
|
|
||||||
RouterFunction<ServerResponse> endpoint();
|
RouterFunction<ServerResponse> endpoint();
|
||||||
|
|
||||||
|
default GroupVersion groupVersion() {
|
||||||
|
return GroupVersion.parseAPIVersion("api.console.halo.run/v1alpha1");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,41 @@
|
||||||
package run.halo.app.core.extension.endpoint;
|
package run.halo.app.core.extension.endpoint;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
|
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
|
||||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
|
import run.halo.app.extension.GroupVersion;
|
||||||
|
|
||||||
public class CustomEndpointsBuilder {
|
public class CustomEndpointsBuilder {
|
||||||
|
|
||||||
private final List<RouterFunction<ServerResponse>> routerFunctions;
|
private final Map<GroupVersion, List<RouterFunction<ServerResponse>>> routerFunctionsMap;
|
||||||
|
|
||||||
public CustomEndpointsBuilder() {
|
public CustomEndpointsBuilder() {
|
||||||
routerFunctions = new LinkedList<>();
|
routerFunctionsMap = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomEndpointsBuilder add(RouterFunction<ServerResponse> routerFunction) {
|
public CustomEndpointsBuilder add(CustomEndpoint customEndpoint) {
|
||||||
routerFunctions.add(routerFunction);
|
routerFunctionsMap
|
||||||
|
.computeIfAbsent(customEndpoint.groupVersion(), gv -> new LinkedList<>())
|
||||||
|
.add(customEndpoint.endpoint());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RouterFunction<ServerResponse> build() {
|
public RouterFunction<ServerResponse> build() {
|
||||||
return SpringdocRouteBuilder.route()
|
SpringdocRouteBuilder routeBuilder = SpringdocRouteBuilder.route();
|
||||||
.nest(RequestPredicates.path("/apis/api.console.halo.run/v1alpha1"),
|
routerFunctionsMap.forEach((gv, routerFunctions) -> {
|
||||||
|
routeBuilder.nest(RequestPredicates.path("/apis/" + gv),
|
||||||
() -> routerFunctions.stream().reduce(RouterFunction::and).orElse(null),
|
() -> routerFunctions.stream().reduce(RouterFunction::and).orElse(null),
|
||||||
builder -> builder
|
builder -> builder.operationId("CustomEndpoints")
|
||||||
.operationId("CustomEndpoints")
|
.description("Custom Endpoint")
|
||||||
.description("Custom endpoints")
|
.tag(gv + "/CustomEndpoint")
|
||||||
.tag("api.console.halo.run/v1alpha1/CustomEndpoint"))
|
);
|
||||||
.build();
|
});
|
||||||
|
routerFunctionsMap.clear();
|
||||||
|
return routeBuilder.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue