Refactor runtime mode conversion

pull/195/head
johnniang 2019-06-10 20:02:58 +08:00
parent 186611d529
commit 5b2591b5c3
7 changed files with 59 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package run.halo.app.model.dto; package run.halo.app.model.dto;
import lombok.Data; import lombok.Data;
import run.halo.app.model.enums.Mode;
/** /**
* Theme controller. * Theme controller.
@ -17,5 +18,5 @@ public class EnvironmentDTO {
private String version; private String version;
private String mode; private Mode mode;
} }

View File

@ -0,0 +1,47 @@
package run.halo.app.model.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
/**
* Halo runtime mode.
*
* @author johnniang
* @date 19-6-10
*/
public enum Mode {
PRODUCTION,
DEVELOPMENT,
TEST;
@JsonValue
String getValue() {
return this.name().toLowerCase();
}
/**
* Get mode from value.
*
* @param value mode value
* @return runtime mode or null if the value is mismatch
*/
@Nullable
@JsonCreator
public static Mode valueFrom(@Nullable String value) {
if (StringUtils.isBlank(value) || value.equalsIgnoreCase("prod")) {
return Mode.PRODUCTION;
}
if (value.equalsIgnoreCase("dev")) {
return Mode.DEVELOPMENT;
}
if (value.equalsIgnoreCase("test")) {
return Mode.TEST;
}
return null;
}
}

View File

@ -23,7 +23,7 @@ public class HaloConst {
public final static String DEFAULT_THEME_ID = "caicai_anatole"; public final static String DEFAULT_THEME_ID = "caicai_anatole";
/** /**
* version constant * Version constant. (Available in production environment)
*/ */
public static final String HALO_VERSION; public static final String HALO_VERSION;

View File

@ -42,6 +42,7 @@ public interface CategoryService extends CrudService<Category, Integer> {
* @param name name * @param name name
* @return Category * @return Category
*/ */
@Nullable
Category getByName(@NonNull String name); Category getByName(@NonNull String name);
/** /**

View File

@ -15,6 +15,7 @@ import run.halo.app.model.dto.EnvironmentDTO;
import run.halo.app.model.dto.StatisticDTO; import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.enums.CommentStatus; import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.Mode;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.LoginParam; import run.halo.app.model.params.LoginParam;
import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.HaloConst;
@ -78,7 +79,7 @@ public class AdminServiceImpl implements AdminService {
StringCacheStore cacheStore, StringCacheStore cacheStore,
ApplicationEventPublisher eventPublisher, ApplicationEventPublisher eventPublisher,
@Value("${spring.datasource.driver-class-name}") String driverClassName, @Value("${spring.datasource.driver-class-name}") String driverClassName,
@Value("${spring.profiles.active}") String mode) { @Value("${spring.profiles.active:prod}") String mode) {
this.postService = postService; this.postService = postService;
this.sheetService = sheetService; this.sheetService = sheetService;
this.attachmentService = attachmentService; this.attachmentService = attachmentService;
@ -194,11 +195,7 @@ public class AdminServiceImpl implements AdminService {
environmentDTO.setVersion(HaloConst.HALO_VERSION); environmentDTO.setVersion(HaloConst.HALO_VERSION);
if (StringUtils.isNotEmpty(mode)) { environmentDTO.setMode(Mode.valueFrom(this.mode));
environmentDTO.setMode(StringUtils.equals("dev", mode) ? "development" : "production");
} else {
environmentDTO.setMode("test");
}
return environmentDTO; return environmentDTO;
} }
@ -214,9 +211,8 @@ public class AdminServiceImpl implements AdminService {
User user = userService.getById(userId); User user = userService.getById(userId);
// Remove all token // Remove all token
cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class).ifPresent(accessToken -> { cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class)
cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken)); .ifPresent(accessToken -> cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken)));
});
cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken)); cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken));
cacheStore.delete(SecurityUtils.buildAccessTokenKey(user)); cacheStore.delete(SecurityUtils.buildAccessTokenKey(user));
cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user)); cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user));

View File

@ -8,6 +8,7 @@ import run.halo.app.service.BackupService;
import run.halo.app.service.PostService; import run.halo.app.service.PostService;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
/** /**
* Backup service implementation. * Backup service implementation.
@ -28,7 +29,7 @@ public class BackupServiceImpl implements BackupService {
public BasePostDetailDTO importMarkdowns(MultipartFile file) throws IOException { public BasePostDetailDTO importMarkdowns(MultipartFile file) throws IOException {
// Read markdown content. // Read markdown content.
String markdown = IoUtil.read(file.getInputStream(), "UTF-8"); String markdown = IoUtil.read(file.getInputStream(), StandardCharsets.UTF_8);
// TODO sheet import // TODO sheet import

View File

@ -340,6 +340,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
category = categoryService.create(category); category = categoryService.create(category);
} }
categoryIds.add(category.getId()); categoryIds.add(category.getId());
break;
default: default:
break; break;
} }