/
SinglePageEndpoint.java
239 lines (227 loc) · 11.1 KB
/
SinglePageEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package run.halo.app.core.extension.endpoint;
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder;
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.fn.builders.schema.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.http.MediaType;
import org.springframework.retry.RetryException;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.thymeleaf.util.StringUtils;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import run.halo.app.content.Content;
import run.halo.app.content.ContentWrapper;
import run.halo.app.content.ListedSinglePage;
import run.halo.app.content.SinglePageQuery;
import run.halo.app.content.SinglePageRequest;
import run.halo.app.content.SinglePageService;
import run.halo.app.core.extension.content.Post;
import run.halo.app.core.extension.content.SinglePage;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.router.QueryParamBuildUtil;
/**
* Endpoint for managing {@link SinglePage}.
*
* @author guqing
* @since 2.0.0
*/
@Slf4j
@Component
@AllArgsConstructor
public class SinglePageEndpoint implements CustomEndpoint {
private final SinglePageService singlePageService;
private final ReactiveExtensionClient client;
@Override
public RouterFunction<ServerResponse> endpoint() {
final var tag = "api.console.halo.run/v1alpha1/SinglePage";
return SpringdocRouteBuilder.route()
.GET("singlepages", this::listSinglePage, builder -> {
builder.operationId("ListSinglePages")
.description("List single pages.")
.tag(tag)
.response(responseBuilder()
.implementation(ListResult.generateGenericClass(ListedSinglePage.class))
);
QueryParamBuildUtil.buildParametersFromType(builder, SinglePageQuery.class);
}
)
.GET("singlepages/{name}/head-content", this::fetchHeadContent,
builder -> builder.operationId("fetchSinglePageHeadContent")
.description("Fetch head content of single page.")
.tag(tag)
.parameter(parameterBuilder().name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class)
)
.response(responseBuilder()
.implementation(ContentWrapper.class))
)
.GET("singlepages/{name}/release-content", this::fetchReleaseContent,
builder -> builder.operationId("fetchSinglePageReleaseContent")
.description("Fetch release content of single page.")
.tag(tag)
.parameter(parameterBuilder().name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class)
)
.response(responseBuilder()
.implementation(ContentWrapper.class))
)
.POST("singlepages", this::draftSinglePage,
builder -> builder.operationId("DraftSinglePage")
.description("Draft a single page.")
.tag(tag)
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(Builder.schemaBuilder()
.implementation(SinglePageRequest.class))
))
.response(responseBuilder()
.implementation(SinglePage.class))
)
.PUT("singlepages/{name}", this::updateSinglePage,
builder -> builder.operationId("UpdateDraftSinglePage")
.description("Update a single page.")
.tag(tag)
.parameter(parameterBuilder().name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class))
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(Builder.schemaBuilder()
.implementation(SinglePageRequest.class))
))
.response(responseBuilder()
.implementation(SinglePage.class))
)
.PUT("singlepages/{name}/content", this::updateContent,
builder -> builder.operationId("UpdateSinglePageContent")
.description("Update a single page's content.")
.tag(tag)
.parameter(parameterBuilder().name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class))
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(Builder.schemaBuilder()
.implementation(Content.class))
))
.response(responseBuilder()
.implementation(Post.class))
)
.PUT("singlepages/{name}/publish", this::publishSinglePage,
builder -> builder.operationId("PublishSinglePage")
.description("Publish a single page.")
.tag(tag)
.parameter(parameterBuilder().name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class))
.response(responseBuilder()
.implementation(SinglePage.class))
)
.build();
}
private Mono<ServerResponse> fetchReleaseContent(ServerRequest request) {
final var name = request.pathVariable("name");
return singlePageService.getReleaseContent(name)
.flatMap(content -> ServerResponse.ok().bodyValue(content));
}
private Mono<ServerResponse> fetchHeadContent(ServerRequest request) {
String name = request.pathVariable("name");
return singlePageService.getHeadContent(name)
.flatMap(content -> ServerResponse.ok().bodyValue(content));
}
Mono<ServerResponse> draftSinglePage(ServerRequest request) {
return request.bodyToMono(SinglePageRequest.class)
.flatMap(singlePageService::draft)
.flatMap(singlePage -> ServerResponse.ok().bodyValue(singlePage));
}
Mono<ServerResponse> updateContent(ServerRequest request) {
String pageName = request.pathVariable("name");
return request.bodyToMono(Content.class)
.flatMap(content -> Mono.defer(() -> client.fetch(SinglePage.class, pageName)
.flatMap(page -> {
SinglePageRequest pageRequest = new SinglePageRequest(page, content);
return singlePageService.update(pageRequest);
}))
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException))
)
.flatMap(post -> ServerResponse.ok().bodyValue(post));
}
Mono<ServerResponse> updateSinglePage(ServerRequest request) {
return request.bodyToMono(SinglePageRequest.class)
.flatMap(singlePageService::update)
.flatMap(page -> ServerResponse.ok().bodyValue(page));
}
Mono<ServerResponse> publishSinglePage(ServerRequest request) {
String name = request.pathVariable("name");
boolean asyncPublish = request.queryParam("async")
.map(Boolean::parseBoolean)
.orElse(false);
return Mono.defer(() -> client.get(SinglePage.class, name)
.flatMap(singlePage -> {
SinglePage.SinglePageSpec spec = singlePage.getSpec();
spec.setPublish(true);
if (spec.getHeadSnapshot() == null) {
spec.setHeadSnapshot(spec.getBaseSnapshot());
}
spec.setReleaseSnapshot(spec.getHeadSnapshot());
return client.update(singlePage);
})
)
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(t -> t instanceof OptimisticLockingFailureException))
.flatMap(post -> {
if (asyncPublish) {
return Mono.just(post);
}
return client.fetch(SinglePage.class, name)
.map(latest -> {
String latestReleasedSnapshotName =
MetadataUtil.nullSafeAnnotations(latest)
.get(Post.LAST_RELEASED_SNAPSHOT_ANNO);
if (StringUtils.equals(latestReleasedSnapshotName,
latest.getSpec().getReleaseSnapshot())) {
return latest;
}
throw new RetryException("SinglePage publishing status is not as expected");
})
.retryWhen(Retry.fixedDelay(10, Duration.ofMillis(100))
.filter(t -> t instanceof RetryException))
.doOnError(IllegalStateException.class, err -> {
log.error("Failed to publish single page [{}]", name, err);
throw new IllegalStateException("Publishing wait timeout.");
});
})
.flatMap(page -> ServerResponse.ok().bodyValue(page));
}
Mono<ServerResponse> listSinglePage(ServerRequest request) {
var listRequest = new SinglePageQuery(request);
return singlePageService.list(listRequest)
.flatMap(listedPages -> ServerResponse.ok().bodyValue(listedPages));
}
}