Skip to content

Commit

Permalink
Set usingH2database variable into setup tempalte model
Browse files Browse the repository at this point in the history
Signed-off-by: JohnNiang <[email protected]>
  • Loading branch information
JohnNiang committed Oct 13, 2024
1 parent 9e15930 commit f7d27fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package run.halo.app.security.preauth;

import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
Expand All @@ -18,11 +19,13 @@
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.fn.builders.content.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.OptimisticLockingFailureException;
Expand Down Expand Up @@ -77,6 +80,7 @@ public class SystemSetupEndpoint {
private final PluginService pluginService;
private final ThemeService themeService;
private final Validator validator;
private final R2dbcConnectionDetails connectionDetails;

@Bean
RouterFunction<ServerResponse> setupPageRouter() {
Expand Down Expand Up @@ -139,8 +143,10 @@ private static Mono<ServerResponse> handleSetupSuccessfully(ServerRequest reques
private Mono<ServerResponse> handleValidationErrors(BindingResult bindingResult,
ServerRequest request) {
if (isHtmlRequest(request)) {
var model = bindingResult.getModel();
model.put("usingH2database", usingH2database());
return ServerResponse.status(HttpStatus.BAD_REQUEST)
.render(SETUP_TEMPLATE, bindingResult.getModel());
.render(SETUP_TEMPLATE, model);
}
return Mono.error(new RequestBodyValidationException(bindingResult));
}
Expand Down Expand Up @@ -210,10 +216,19 @@ private Mono<ServerResponse> setupPage(ServerRequest request) {
}
var body = new SetupRequest(new LinkedMultiValueMap<>());
var bindingResult = new BeanPropertyBindingResult(body, "form");
return ServerResponse.ok().render(SETUP_TEMPLATE, bindingResult.getModel());
var model = bindingResult.getModel();
model.put("usingH2database", usingH2database());
return ServerResponse.ok().render(SETUP_TEMPLATE, model);
});
}

private boolean usingH2database() {
return Optional.ofNullable(connectionDetails.getConnectionFactoryOptions().getValue(DRIVER))
// See io.r2dbc.h2.H2ConnectionFactoryProvider.H2_DRIVER for more
.map("h2"::equals)
.orElse(false);
}

record SetupRequest(MultiValueMap<String, String> formData) {

@Schema(requiredMode = REQUIRED, minLength = 4, maxLength = 63)
Expand Down
13 changes: 5 additions & 8 deletions application/src/main/resources/templates/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

<div class="halo-form-wrapper">
<h1 class="form-title" th:text="#{title}"></h1>

<div class="alert alert-error" role="alert" th:if="${usingH2database}">
<strong th:text="#{form.messages.h2.title}"></strong>
<br />
<span th:text="#{form.messages.h2.content}"> </span>
</div>
<form th:object="${form}" th:action="@{/system/setup}" class="halo-form" method="post">
<!-- TODO: 添加判断 -->
<div class="alert alert-error" role="alert">
<strong th:text="#{form.messages.h2.title}"></strong>
<br />
<span th:text="#{form.messages.h2.content}"> </span>
</div>

<div class="form-item">
<label for="siteTitle" th:text="#{form.siteTitle.label}"></label>
<div class="form-input">
Expand Down

0 comments on commit f7d27fd

Please sign in to comment.