feat: dynamic server config #969

This commit is contained in:
tjq 2024-08-25 17:29:24 +08:00
parent 0fce33afc5
commit 06438b145d
5 changed files with 15 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package tech.powerjob.common.utils;
import tech.powerjob.common.OmsConstant;
import tech.powerjob.common.enums.ErrorCodes;
import tech.powerjob.common.exception.PowerJobException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -123,21 +124,21 @@ public class CommonUtils {
public static <T> T requireNonNull(T obj, String msg) {
if (obj == null) {
throw new PowerJobException(msg);
throw new PowerJobException(ErrorCodes.INVALID_REQUEST, msg);
}
if (obj instanceof String) {
if (StringUtils.isEmpty((String) obj)) {
throw new PowerJobException(msg);
throw new PowerJobException(ErrorCodes.INVALID_REQUEST, msg);
}
}
if (obj instanceof Collection) {
if (CollectionUtils.isEmpty((Collection<?>) obj)) {
throw new PowerJobException(msg);
throw new PowerJobException(ErrorCodes.INVALID_REQUEST, msg);
}
}
if (obj instanceof Map) {
if (MapUtils.isEmpty((Map<?, ?>) obj)) {
throw new PowerJobException(msg);
throw new PowerJobException(ErrorCodes.INVALID_REQUEST, msg);
}
}
return obj;

View File

@ -12,7 +12,7 @@ import tech.powerjob.server.common.options.WebOptionAbility;
@AllArgsConstructor
public enum ConfigItem implements WebOptionAbility {
AUTH_LOGIN_TYPE_BLACKLIST("oms.auth.login-type.blacklist", "禁用的登录方式")
AUTH_LOGIN_TYPE_BLACKLIST("oms.auth.login-type.blacklist", "需要禁用的登录方式,多值逗号分割")
;
@ -27,6 +27,6 @@ public enum ConfigItem implements WebOptionAbility {
@Override
public String getLabel() {
return desc;
return String.format("%s%s", code, desc);
}
}

View File

@ -74,7 +74,7 @@ public class DynamicServerConfigCrudServiceImpl implements DynamicServerConfigCr
@Override
public void delete(String key) {
Optional<SundryDO> deletedOpt = sundryRepository.deleteByPkeyAndSkey(PKEY, key);
Optional<Long> deletedOpt = sundryRepository.deleteByPkeyAndSkey(PKEY, key);
if (deletedOpt.isPresent()) {
log.info("[DynamicServerConfigCrudService] delete config[{}] successfully, origin data: {}", key, deletedOpt.get());
} else {

View File

@ -1,8 +1,10 @@
package tech.powerjob.server.persistence.remote.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import tech.powerjob.server.persistence.remote.model.SundryDO;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
@ -18,5 +20,7 @@ public interface SundryRepository extends JpaRepository<SundryDO, Long> {
Optional<SundryDO> findByPkeyAndSkey(String pkey, String skey);
Optional<SundryDO> deleteByPkeyAndSkey(String pkey, String skey);
@Modifying
@Transactional(rollbackOn = Exception.class)
Optional<Long> deleteByPkeyAndSkey(String pkey, String skey);
}

View File

@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import tech.powerjob.common.response.ResultDTO;
import tech.powerjob.common.utils.CommonUtils;
import tech.powerjob.server.auth.Permission;
import tech.powerjob.server.auth.RoleScope;
import tech.powerjob.server.auth.interceptor.ApiPermission;
@ -31,6 +32,7 @@ public class SystemConfigController {
@PostMapping("/save")
@ApiPermission(name = "Config-Save", roleScope = RoleScope.GLOBAL, requiredPermission = Permission.SU)
public ResultDTO<Void> saveConfig(@RequestBody Config config) {
CommonUtils.requireNonNull(config.getKey(), "ConfigKey can't be null or empty!");
dynamicServerConfigCrudService.save(config);
return ResultDTO.success(null);
}