Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
🎉 init repository
Browse files Browse the repository at this point in the history
  • Loading branch information
fallen-angle committed Apr 28, 2021
1 parent f4af643 commit e607d2a
Show file tree
Hide file tree
Showing 72 changed files with 74 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.cn.bookmarktomb.filter.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import cn.hutool.core.map.MapBuilder;
import com.cn.bookmarktomb.config.AdminConfig;
import com.cn.bookmarktomb.excepotion.AdminSetBeforeException;
import com.cn.bookmarktomb.excepotion.SystemInitBeforeException;
import com.cn.bookmarktomb.model.bean.ProjectProperties;
import com.cn.bookmarktomb.model.cache.ConfigCache;
import com.cn.bookmarktomb.model.convert.SystemInfoConvert;
Expand Down Expand Up @@ -53,7 +55,7 @@ public ResponseEntity<Object> getSystemInfo() {
public ResponseEntity<Object> initSystem(@Valid @RequestBody InitVO initVO) {
boolean initFlag = (boolean)ConfigCache.get(ConfigCache.INIT_FLAG);
if (initFlag) {
return ResponseEntity.ok("The system has been initialized before!");
throw new SystemInitBeforeException();
}
Map<String, Object> initMap = MapBuilder.<String, Object>create()
.put(ConfigCache.SERVER_PORT, initVO.getServerPort())
Expand All @@ -71,7 +73,7 @@ public ResponseEntity<Object> initSystem(@Valid @RequestBody InitVO initVO) {
@ApiOperation("Init admin account")
public ResponseEntity<Object> createAdmin(@Valid @RequestBody AdminCreateUserVO createUserVO) {
if ((boolean) ConfigCache.get(ConfigCache.ADMIN_FLAG)) {
return ResponseEntity.ok("The admin has been set before!");
throw new AdminSetBeforeException();
}
RegisterDTO registerDTO = userInfoConverter.adminCreateVO2DTO(createUserVO);
registerDTO.setIsAdmin(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cn.bookmarktomb.excepotion;

import com.cn.bookmarktomb.model.constant.ErrorCodeConstant;

public class AdminSetBeforeException extends RuntimeException {

private Integer code = ErrorCodeConstant.ADMIN_SET_BEFORE_CODE;

public AdminSetBeforeException() {
super(ErrorCodeConstant.ADMIN_SET_BEFORE_MSG);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.cn.bookmarktomb.excepotion;

import com.cn.bookmarktomb.model.entity.Email;
import com.cn.bookmarktomb.util.ThrowableUtil;
import com.cn.bookmarktomb.model.constant.ErrorCodeConstant;
import com.cn.bookmarktomb.model.factory.ApiErrorFactory;
import com.cn.bookmarktomb.model.vo.ApiErrorVO;
import com.cn.bookmarktomb.util.ThrowableUtil;
import com.sun.mail.util.MailConnectException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -125,11 +122,23 @@ public ResponseEntity<ApiErrorVO> dbOperationError(DbOperationException e){
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiErrorVO> dbOperationError(HttpRequestMethodNotSupportedException e){
public ResponseEntity<ApiErrorVO> httpMethodError(HttpRequestMethodNotSupportedException e){
log.error(ThrowableUtil.getStackTrace(e));
return ApiErrorFactory.requestError(HttpStatus.METHOD_NOT_ALLOWED, HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
}

@ExceptionHandler(AdminSetBeforeException.class)
public ResponseEntity<ApiErrorVO> adminInitBeforeError(AdminSetBeforeException e) {
log.error(ThrowableUtil.getStackTrace(e));
return ApiErrorFactory.serverError(ErrorCodeConstant.ADMIN_SET_BEFORE_CODE, e.getMessage());
}

@ExceptionHandler(SystemInitBeforeException.class)
public ResponseEntity<ApiErrorVO> systemInitBeforeError(SystemInitBeforeException e) {
log.error(ThrowableUtil.getStackTrace(e));
return ApiErrorFactory.serverError(ErrorCodeConstant.SYSTEM_INIT_BEFORE_CODE, e.getMessage());
}

private String getErrorCodeAndMsg(int code, String msg) {
return String.format("errorCode: %5d, errorMsg: %s", code, msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cn.bookmarktomb.excepotion;

import com.cn.bookmarktomb.model.constant.ErrorCodeConstant;

public class SystemInitBeforeException extends RuntimeException {

private Integer code = ErrorCodeConstant.SYSTEM_INIT_BEFORE_CODE;

public SystemInitBeforeException() {
super(ErrorCodeConstant.SYSTEM_INIT_BEFORE_MSG);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/cn/bookmarktomb/filter/AdminFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private boolean isApiInit(HttpServletRequest request) {
List<String> initApi = List.of("^/webjar.*$", "^/api/public/admin$", "^/api/public/db.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
List<String> initApi = List.of("^/$","^/(css|js|font|favicon|logo).*$", "^/#/.*$", "^/webjar.*$", "^/api/public/admin$", "^/api/public/db.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
for (String url: initApi) {
if (request.getRequestURI().matches(url)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private boolean isApiInit(HttpServletRequest request) {
List<String> initApi = List.of("^/webjar.*$", "^/api/public/db$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
List<String> initApi = List.of("^/$","^/(css|js|font|favicon|logo).*$", "^/#/.*$", "^/webjar.*$", "^/api/public/db$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
for (String url: initApi) {
if (request.getRequestURI().matches(url)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cn/bookmarktomb/filter/InitFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private boolean isApiInit(HttpServletRequest request) {
List<String> initApi = List.of("^/webjar.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
List<String> initApi = List.of("^/$","^/(css|js|font|favicon|logo).*$", "^/#/.*$", "^/webjar.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
for (String url: initApi) {
if (request.getRequestURI().matches(url)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cn/bookmarktomb/filter/StartFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private boolean isApiInit(HttpServletRequest request) {
List<String> initApi = List.of("^/webjar.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
List<String> initApi = List.of("^/$","^/(css|js|font|favicon|logo).*$", "^/#/.*$", "^/webjar.*$", "^/api/public/init$", "^/doc.html.*$", "^/swagger-resources$", "^/v3/api-docs$");
for (String url: initApi) {
if (request.getRequestURI().matches(url)) {
return true;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/cn/bookmarktomb/model/cache/ConfigCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cn.bookmarktomb.model.cache;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import cn.hutool.system.SystemUtil;
Expand All @@ -13,6 +14,7 @@
import java.io.File;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Objects;

/**
* @author fallen-angle
Expand Down Expand Up @@ -64,15 +66,20 @@ public static void initCache() {
return;
}

Database dbInfo = jsonObject.get(DATABASE, Database.class);
if (StrUtil.isBlank(dbInfo.getDbname()) || StrUtil.isBlank(dbInfo.getHost()) || Objects.isNull(dbInfo.getPort())) {
return;
}

configMap.put(INIT_FLAG, true);
configMap.put(SERVER_PORT, jsonObject.get(SERVER_PORT));
configMap.put(MD_5, MD5.create().digestHex(jsonObject.toString(), Charset.defaultCharset()));
configMap.put(DATABASE, jsonObject.get(DATABASE, Database.class));
configMap.put(EMAIL, jsonObject.get(EMAIL, Email.class));
configMap.put(EMAIL_ENABLE, jsonObject.get(EMAIL_ENABLE));
configMap.put(REGISTER_ENABLE, (Boolean) jsonObject.get(EMAIL_ENABLE) && (Boolean) jsonObject.get(REGISTER_ENABLE));
ApplicationHome h = new ApplicationHome(ConfigCache.class);
File jarFile = h.getSource();
ApplicationHome home = new ApplicationHome(ConfigCache.class);
File jarFile = home.getSource();
configMap.put(JAR_PATH, jarFile.getParentFile().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public class ErrorCodeConstant {
public static final int EMAIL_NOT_AUTH_CODE = 5007;
public static final String EMAIL_NOT_AUTH_MSG = "Email server auth error";

public static final int SYSTEM_INIT_BEFORE_CODE = 5008;
public static final String SYSTEM_INIT_BEFORE_MSG = "System has been initialized before";

public static final int ADMIN_SET_BEFORE_CODE = 5009;
public static final String ADMIN_SET_BEFORE_MSG = "Admin has been set before";

public static final int DB_OPERATION_CODE = 5100;
public static final String DB_OPERATION_ERROR = "Database operation error";

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/cn/bookmarktomb/model/vo/OnlineInfoVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class OnlineInfoVO {
@NoArgsConstructor
public static class GetOnlineInfoVO {

private String token;

private String username;

private Long id;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/cn/bookmarktomb/model/vo/SystemInfoVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Range;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
* @author fallen-angle
Expand Down Expand Up @@ -51,6 +55,7 @@ public static class EmailVO {

private String host;

@Range(max = 65535)
private Integer port;

private String username;
Expand All @@ -64,10 +69,14 @@ public static class EmailVO {
@NoArgsConstructor
public static class DatabaseVO {

@NotNull
String host;

@NotNull
@Range(max = 65535)
Integer port;

@NotNull
String dbname;

String username;
Expand All @@ -81,6 +90,7 @@ public static class DatabaseVO {
@ToString
@NoArgsConstructor
public static class InitVO {

@Valid
EmailVO email;

Expand Down
1 change: 0 additions & 1 deletion src/main/resources/static/css/app.1eef4076.css

This file was deleted.

Loading

0 comments on commit e607d2a

Please sign in to comment.