first commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
id("java-library-conventions")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":main-infrastructure"))
|
||||
implementation(project(":gateway-module-policy:policy-contract-subjects"))
|
||||
implementation(Dependencies.jacksonYaml)
|
||||
implementation("org.springframework.boot:spring-boot-starter")
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package net.bquarkz.ai.gateway.configuration;
|
||||
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import net.bquarkz.ai.gateway.policy.contracts.PolicyConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Component
|
||||
public class PolicyConfigLoader implements Supplier<PolicyConfig> {
|
||||
private static final Logger log = LoggerFactory.getLogger(PolicyConfigLoader.class);
|
||||
|
||||
private final Path configPath;
|
||||
private final ObjectMapper yamlMapper;
|
||||
private final AtomicReference<PolicyConfig> current = new AtomicReference<>();
|
||||
private volatile FileTime lastModified;
|
||||
|
||||
public PolicyConfigLoader(@Value("${gateway.policy.config-path}") String configPath) {
|
||||
this.configPath = Path.of(configPath);
|
||||
this.yamlMapper = JsonMapper.builder(new YAMLFactory())
|
||||
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
|
||||
.build();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
reload();
|
||||
}
|
||||
|
||||
public PolicyConfig get() {
|
||||
return current.get();
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${gateway.policy.reload-interval-ms:2000}")
|
||||
void checkForChanges() {
|
||||
try {
|
||||
FileTime modified = Files.getLastModifiedTime(configPath);
|
||||
if (lastModified == null || modified.compareTo(lastModified) > 0) {
|
||||
reload();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to check policy file modification time", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
try {
|
||||
String yaml = Files.readString(configPath);
|
||||
PolicyConfig config = yamlMapper.readValue(yaml, PolicyConfig.class);
|
||||
current.set(config);
|
||||
lastModified = Files.getLastModifiedTime(configPath);
|
||||
log.info("Policy config loaded from {} (modified: {})", configPath, lastModified);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to load policy config from: {}", configPath, e);
|
||||
if (current.get() == null) {
|
||||
throw new RuntimeException("Failed to load initial policy config from: " + configPath, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user