Skip to content
Snippets Groups Projects

Refactor the settings code to allow incremental updates and protected keys

8 files
+ 176
127
Compare changes
  • Side-by-side
  • Inline

Files

package nl.tudelft.ewi.auta.core.controller;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import nl.tudelft.ewi.auta.core.settings.FileGlobalSettingsWriter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import nl.tudelft.ewi.auta.core.response.Response;
import nl.tudelft.ewi.auta.core.settings.FileGlobalSettingsLoader;
import nl.tudelft.ewi.auta.core.settings.GlobalSettings;
@RestController
public class SettingsController {
/**
* The path to the (updated) settings file.
*/
private static final String SETTINGS_PATH = "/srv/auta/settings.json";
/**
* The settings.
*/
private GlobalSettings settings;
private final GlobalSettings settings;
/**
* The gson.
* The service to write the settings to disk with.
*/
private final Gson gson = new Gson();
private final FileGlobalSettingsWriter writer;
/**
* Creates a new settings controller.
* @param settings the settings.
*
* @param settings the settings
* @param writer the settings writer
*/
public SettingsController(final GlobalSettings settings) {
public SettingsController(
final GlobalSettings settings, final FileGlobalSettingsWriter writer
) {
this.settings = settings;
this.writer = writer;
}
@@ -69,12 +65,8 @@ public class SettingsController {
return ResponseEntity.badRequest().body(res);
}
try (var writer = new FileWriter(SETTINGS_PATH)) {
this.gson.toJson(req, writer);
}
final var fileSettings = new FileGlobalSettingsLoader(this.settings, SETTINGS_PATH);
fileSettings.load();
this.settings.adopt(req);
this.writer.store(this.settings);
return ResponseEntity.ok(res);
}
Loading