Skip to content

Commit

Permalink
Add separate properties class for config
Browse files Browse the repository at this point in the history
Provides better IDE experience when editing settings.
  • Loading branch information
geirsagberg committed Jun 27, 2024
1 parent db09b89 commit 0baf4f1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
6 changes: 6 additions & 0 deletions db-scheduler-ui-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.github.kagkarlsson</groupId>
<artifactId>db-scheduler-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@
import no.bekk.dbscheduler.ui.controller.LogController;
import no.bekk.dbscheduler.ui.controller.TaskController;
import no.bekk.dbscheduler.ui.controller.UIController;
import no.bekk.dbscheduler.ui.model.DbSchedulerUiConfig;
import no.bekk.dbscheduler.ui.service.LogLogic;
import no.bekk.dbscheduler.ui.service.TaskLogic;
import no.bekk.dbscheduler.ui.util.Caching;
import no.bekk.dbscheduler.uistarter.config.DbSchedulerUiProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@ConditionalOnProperty(value = "db-scheduler-ui.enabled", matchIfMissing = true)
@EnableConfigurationProperties(DbSchedulerUiProperties.class)
public class UiApiAutoConfiguration {

private static final Logger logger = LoggerFactory.getLogger(UiApiAutoConfiguration.class);

@Value("${db-scheduler-ui.task-data:true}")
boolean showTaskData;

@Value("${db-scheduler-ui.history:false}")
boolean showHistory;

UiApiAutoConfiguration() {
logger.info("UiApiAutoConfiguration created");
}
Expand All @@ -59,8 +54,8 @@ Caching caching() {

@Bean
@ConditionalOnMissingBean
TaskLogic taskLogic(Scheduler scheduler, Caching caching) {
return new TaskLogic(scheduler, caching, showTaskData);
TaskLogic taskLogic(Scheduler scheduler, Caching caching, DbSchedulerUiProperties properties) {
return new TaskLogic(scheduler, caching, properties.isTaskData());
}

@Bean
Expand All @@ -70,12 +65,16 @@ TaskLogic taskLogic(Scheduler scheduler, Caching caching) {
name = "history",
havingValue = "true",
matchIfMissing = false)
LogLogic logLogic(DataSource dataSource, Caching caching, DbSchedulerCustomizer customizer) {
LogLogic logLogic(
DataSource dataSource,
Caching caching,
DbSchedulerCustomizer customizer,
DbSchedulerUiProperties properties) {
return new LogLogic(
dataSource,
customizer.serializer().orElse(Serializer.DEFAULT_JAVA_SERIALIZER),
caching,
showTaskData);
properties.isTaskData());
}

@Bean
Expand All @@ -95,12 +94,6 @@ LogController logController(LogLogic logLogic) {
return new LogController(logLogic);
}

@Bean
@ConditionalOnMissingBean
DbSchedulerUiConfig dbSchedulerUiConfig() {
return new DbSchedulerUiConfig(showHistory);
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnWebApplication(type = Type.SERVLET)
Expand All @@ -110,7 +103,7 @@ UIController uiController() {

@Bean
@ConditionalOnMissingBean
ConfigController configController() {
return new ConfigController();
ConfigController configController(DbSchedulerUiProperties properties) {
return new ConfigController(properties.isHistory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) Bekk
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package no.bekk.dbscheduler.uistarter.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Setter
@Getter
@ConfigurationProperties("db-scheduler-ui")
public class DbSchedulerUiProperties {
private boolean enabled = true;
private boolean taskData = true;
private boolean history = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*/
package no.bekk.dbscheduler.ui.controller;

import no.bekk.dbscheduler.ui.model.DbSchedulerUiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import no.bekk.dbscheduler.ui.model.ConfigResponse;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,10 +24,14 @@
@RequestMapping("/db-scheduler-api/config")
public class ConfigController {

@Autowired private DbSchedulerUiConfig dbSchedulerUiConfig;
private final boolean showHistory;

public ConfigController(boolean showHistory) {
this.showHistory = showHistory;
}

@GetMapping
public DbSchedulerUiConfig getConfig() {
return dbSchedulerUiConfig;
public ConfigResponse getConfig() {
return new ConfigResponse(showHistory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@Getter
@Setter
@AllArgsConstructor
public class DbSchedulerUiConfig {

private boolean history;
public class ConfigResponse {
private boolean showHistory;
}

0 comments on commit 0baf4f1

Please sign in to comment.