Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor system configuration #1303

Merged
merged 3 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public DisableOnConditionAspect(HaloProperties haloProperties) {
this.haloProperties = haloProperties;
}

@Pointcut("execution(* run.halo.app.controller.*.*(..)) "
+ "&& @annotation(run.halo.app.annotation.DisableOnCondition)")
@Pointcut("within(run.halo.app.controller..*)")
public void pointcut() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class SensitiveConcealAspect {


@Pointcut("execution(* run.halo.app.repository.*.*(..)) "
@Pointcut("within(run.halo.app.repository..*) "
+ "&& @annotation(run.halo.app.annotation.SensitiveConceal)")
public void pointCut() {
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/run/halo/app/config/HaloConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
Expand Down Expand Up @@ -38,17 +37,20 @@
BaseRepositoryImpl.class)
public class HaloConfiguration {

@Autowired
private HaloProperties haloProperties;
private final HaloProperties haloProperties;

public HaloConfiguration(HaloProperties haloProperties) {
this.haloProperties = haloProperties;
}

@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
builder.failOnEmptyBeans(false);
return builder.build();
}

@Bean
public RestTemplate httpsRestTemplate(RestTemplateBuilder builder)
RestTemplate httpsRestTemplate(RestTemplateBuilder builder)
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate httpsRestTemplate = builder.build();
httpsRestTemplate.setRequestFactory(
Expand All @@ -59,7 +61,7 @@ public RestTemplate httpsRestTemplate(RestTemplateBuilder builder)

@Bean
@ConditionalOnMissingBean
public AbstractStringCacheStore stringCacheStore() {
AbstractStringCacheStore stringCacheStore() {
AbstractStringCacheStore stringCacheStore;
switch (haloProperties.getCache()) {
case "level":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ public class HaloProperties {
/**
* Doc api disabled. (Default is true)
*/
@Deprecated
private boolean docDisabled = true;

/**
* Production env. (Default is true)
*/
@Deprecated
private boolean productionEnv = true;

/**
* Authentication enabled
* Authentication enabled.
*/
private boolean authEnabled = true;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/listener/StartedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void initThemes() {
Path themePath = themeService.getBasePath();

// Fix the problem that the project cannot start after moving to a new server
if (!haloProperties.isProductionEnv() || Files.notExists(themePath)) {
if (!haloProperties.getMode().isProductionEnv() || Files.notExists(themePath)) {
FileUtils.copyFolder(source, themePath);
log.debug("Copied theme folder from [{}] to [{}]", source, themePath);
} else {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/run/halo/app/model/enums/Mode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.halo.app.model.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.lang.Nullable;

Expand All @@ -13,22 +14,22 @@
public enum Mode {

/**
* Production mode
* Production mode.
*/
PRODUCTION,

/**
* Develop mode
* Develop mode.
*/
DEVELOPMENT,

/**
* Demo mode
* Demo mode.
*/
DEMO,

/**
* Test mode
* Test mode.
*/
TEST;

Expand Down Expand Up @@ -57,4 +58,9 @@ public static Mode valueFrom(@Nullable String value) {
String getValue() {
return this.name().toLowerCase();
}

@JsonIgnore
public boolean isProductionEnv() {
return PRODUCTION.equals(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private AuthenticationFailureHandler getFailureHandler() {
// Create default authentication failure handler
DefaultAuthenticationFailureHandler failureHandler =
new DefaultAuthenticationFailureHandler();
failureHandler.setProductionEnv(haloProperties.isProductionEnv());
failureHandler.setProductionEnv(haloProperties.getMode().isProductionEnv());

this.failureHandler = failureHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public AdminAuthenticationFilter(AbstractStringCacheStore cacheStore,
// set failure handler
DefaultAuthenticationFailureHandler failureHandler =
new DefaultAuthenticationFailureHandler();
failureHandler.setProductionEnv(haloProperties.isProductionEnv());
failureHandler.setProductionEnv(haloProperties.getMode().isProductionEnv());
failureHandler.setObjectMapper(objectMapper);

setFailureHandler(failureHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ApiAuthenticationFilter(HaloProperties haloProperties,
// set failure handler
DefaultAuthenticationFailureHandler failureHandler =
new DefaultAuthenticationFailureHandler();
failureHandler.setProductionEnv(haloProperties.isProductionEnv());
failureHandler.setProductionEnv(haloProperties.getMode().isProductionEnv());
failureHandler.setObjectMapper(objectMapper);
setFailureHandler(failureHandler);
}
Expand Down Expand Up @@ -91,7 +91,7 @@ protected void doAuthenticate(HttpServletRequest request, HttpServletResponse re
Optional<String> optionalAccessKey =
optionService.getByProperty(ApiProperties.API_ACCESS_KEY, String.class);

if (!optionalAccessKey.isPresent()) {
if (optionalAccessKey.isEmpty()) {
// If the access key is not set
throw new AuthenticationException("API access key hasn't been set by blogger");
}
Expand Down
54 changes: 0 additions & 54 deletions src/main/resources/application-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,9 @@ server:
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
devtools:
add-properties: false
output:
ansi:
enabled: always
datasource:
type: com.zaxxer.hikari.HikariDataSource

# H2 database configuration.
driver-class-name: org.h2.Driver
url: jdbc:h2:file:~/halo-demo/db/halo
username: admin
password: 123456

# MySQL database configuration.
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
# username: root
# password: 123456

h2:
console:
settings:
web-allow-others: false
path: /h2-console
enabled: false
jpa:
hibernate:
ddl-auto: update
show-sql: false
open-in-view: false
flyway:
enabled: false
servlet:
multipart:
max-file-size: 10240MB
max-request-size: 10240MB
management:
endpoints:
web:
base-path: /api/admin/actuator
exposure:
include: [ 'httptrace', 'metrics','env','logfile','health' ]
logging:
level:
run.halo.app: INFO
file:
path: ${user.home}/halo-demo/logs

springfox:
documentation:
enabled: true

halo:
download-timeout: 5m
doc-disabled: false
production-env: false
auth-enabled: true
mode: demo
workDir: ${user.home}/halo-demo/
Expand Down
13 changes: 5 additions & 8 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ server:
port: 8090
forward-headers-strategy: native
compression:
enabled: false
enabled: true
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
output:
ansi:
enabled: always
devtools:
restart:
eanbled: true
datasource:
type: com.zaxxer.hikari.HikariDataSource

Expand Down Expand Up @@ -56,15 +59,9 @@ logging:
org.hibernate.type.descriptor.sql.BasicBinder: INFO
org.hibernate.type.descriptor.sql.BasicExtractor: INFO
file:
path: ${user.home}/halo-dev/logs

springfox:
documentation:
enabled: true
path: ${halo.work-dir}/newLogs

halo:
doc-disabled: false
production-env: false
auth-enabled: true
mode: development
workDir: ${user.home}/halo-dev/
Expand Down
36 changes: 0 additions & 36 deletions src/main/resources/application-user.yaml

This file was deleted.

23 changes: 7 additions & 16 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ server:
forward-headers-strategy: native
error:
include-message: always
compression:
enabled: false
spring:
devtools:
restart:
eanbled: false
mvc:
pathmatch:
use-suffix-pattern: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
devtools:
add-properties: false
output:
ansi:
enabled: always
Expand All @@ -21,20 +20,12 @@ spring:

# H2 database configuration.
driver-class-name: org.h2.Driver
url: jdbc:h2:file:~/.halo/db/halo
url: jdbc:h2:file:${halo.work-dir}/db/halo
username: admin
password: 123456

h2:
console:
settings:
web-allow-others: false
path: /h2-console
enabled: false
jpa:
hibernate:
ddl-auto: update
show-sql: false
open-in-view: false
flyway:
enabled: false
Expand All @@ -48,20 +39,20 @@ spring:
settings:
auto_import: /common/macro/global_macro.ftl as global
template-loader-path:
- file:///${halo.work-dir}templates/
- file:///${halo.work-dir}/templates/
- classpath:/templates/
management:
endpoints:
web:
base-path: /api/admin/actuator
exposure:
include: [ 'httptrace', 'metrics','env','logfile','health' ]
include: [ 'httptrace', 'metrics', 'env', 'logfile', 'health' ]
logging:
level:
run.halo.app: INFO
org.eclipse.jetty.server.HttpChannel: ERROR
file:
path: ${user.home}/.halo/logs
path: ${halo.work-dir}/logs

springfox:
documentation:
Expand Down