Skip to content

Commit

Permalink
Merge pull request #6848 from ruibaby/feat/add-h2-alert-for-setup-page-2
Browse files Browse the repository at this point in the history
feat: add H2 database usage warning for setup page
  • Loading branch information
ruibaby authored Oct 14, 2024
2 parents b0d36b7 + eff73dc commit 4059f15
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 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,14 @@
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.ObjectProvider;
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 +81,7 @@ public class SystemSetupEndpoint {
private final PluginService pluginService;
private final ThemeService themeService;
private final Validator validator;
private final ObjectProvider<R2dbcConnectionDetails> connectionDetails;

@Bean
RouterFunction<ServerResponse> setupPageRouter() {
Expand Down Expand Up @@ -138,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 @@ -209,10 +216,25 @@ 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() {
var rcd = connectionDetails.getIfUnique();
if (rcd == null) {
// If no R2dbcConnectionDetails is available, we assume H2(mem) is used.
return true;
}
var options = rcd.getConnectionFactoryOptions();
return Optional.ofNullable(options.getValue(DRIVER))
.map(Object::toString)
.map("h2"::equalsIgnoreCase)
.orElse(false);
}

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

@Schema(requiredMode = REQUIRED, minLength = 4, maxLength = 63)
Expand Down
6 changes: 5 additions & 1 deletion application/src/main/resources/templates/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

<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">
<div class="form-item">
<label for="siteTitle" th:text="#{form.siteTitle.label}"></label>
Expand Down
4 changes: 3 additions & 1 deletion application/src/main/resources/templates/setup.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ form.username.label=用户名
form.email.label=电子邮箱
form.password.label=密码
form.confirmPassword.label=确认密码
form.submit=初始化
form.submit=初始化
form.messages.h2.title=警告:正在使用 H2 数据库
form.messages.h2.content=H2 数据库仅适用于开发环境和测试环境,不推荐在生产环境中使用,H2 非常容易因为操作不当导致数据文件损坏。如果必须要使用,请按时进行数据备份。
2 changes: 2 additions & 0 deletions application/src/main/resources/templates/setup_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ form.email.label=Email
form.password.label=Password
form.confirmPassword.label=Confirm Password
form.submit=Setup
form.messages.h2.title=Warning: Using H2 Database
form.messages.h2.content=The H2 database is only suitable for development and testing environments. It is not recommended for production environments, as H2 is very prone to data file corruption due to improper operations. If you must use it, please back up your data regularly.
4 changes: 3 additions & 1 deletion application/src/main/resources/templates/setup_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ form.username.label=Nombre de Usuario
form.email.label=Correo Electrónico
form.password.label=Contraseña
form.confirmPassword.label=Confirmar Contraseña
form.submit=Configurar
form.submit=Configurar
form.messages.h2.title=Advertencia: Usando la base de datos H2
form.messages.h2.content=La base de datos H2 solo es adecuada para entornos de desarrollo y prueba. No se recomienda su uso en entornos de producción, ya que H2 es muy susceptible a la corrupción de archivos de datos debido a un manejo inadecuado. Si debe usarla, realice copias de seguridad de los datos regularmente.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ form.username.label=使用者名稱
form.email.label=電子郵件
form.password.label=密碼
form.confirmPassword.label=確認密碼
form.submit=初始化
form.submit=初始化
form.messages.h2.title=警告:正在使用 H2 資料庫
form.messages.h2.content=H2 資料庫僅適用於開發環境和測試環境,不建議在生產環境中使用,H2 非常容易因為操作不當導致資料檔案損壞。如果必須要使用,請按時進行資料備份。

0 comments on commit 4059f15

Please sign in to comment.