Add portable mode

This commit is contained in:
death-claw
2019-09-23 16:43:52 +01:00
parent 2217fea0ad
commit c40d3e2377
5 changed files with 29 additions and 9 deletions
@@ -1,8 +1,10 @@
package tn.mnlr.vripper;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -12,6 +14,7 @@ import tn.mnlr.vripper.services.AppSettingsService;
import tn.mnlr.vripper.services.PersistenceService;
import tn.mnlr.vripper.services.VipergirlsAuthService;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -21,7 +24,7 @@ public class VripperApplication {
private static final Logger logger = LoggerFactory.getLogger(VripperApplication.class);
public static final String dataPath = System.getProperty("user.home", ".") + File.separator + ".vripper" + File.separator + "data.json";
// public static final String dataPath = System.getProperty("user.home", ".") + File.separator + ".vripper" + File.separator + "data.json";
public static final ExecutorService commonExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
@@ -46,6 +49,17 @@ public class VripperApplication {
@Autowired
private AppSettingsService appSettingsService;
@Value("${base.dir}")
private String baseDir;
@Getter
private String dataPath;
@PostConstruct
public void init() {
dataPath = baseDir + File.separator + ".vripper" + File.separator + "data.json";
}
@Override
public void run(String... args) {
@@ -39,6 +39,9 @@ public class PersistenceService {
@Autowired
private AppStateService stateService;
@Autowired
private VripperApplication.AppCommandRunner appCommandRunner;
private ObjectMapper om;
private Disposable subscription;
@@ -61,7 +64,7 @@ public class PersistenceService {
@PostConstruct
public void init() {
File dataFile = new File(VripperApplication.dataPath);
File dataFile = new File(appCommandRunner.getDataPath());
if (!dataFile.exists()) {
try {
dataFile.getParentFile().mkdirs();
@@ -95,7 +98,7 @@ public class PersistenceService {
public void persist(Map<String, Post> currentPosts) {
try (PrintWriter out = new PrintWriter(VripperApplication.dataPath, "UTF-8")) {
try (PrintWriter out = new PrintWriter(appCommandRunner.getDataPath(), "UTF-8")) {
out.print(om.writeValueAsString(currentPosts));
} catch (IOException e) {
logger.error("Failed to persist app state", e);
@@ -109,11 +112,11 @@ public class PersistenceService {
} catch (IOException e) {
logger.error("Failed to read app state", e);
long timestamp = new Date().getTime();
logger.warn(String.format("trying to rename old data file from %s to %s", VripperApplication.dataPath, VripperApplication.dataPath + "." + timestamp + ".old"));
logger.warn(String.format("trying to rename old data file from %s to %s", appCommandRunner.getDataPath(), appCommandRunner.getDataPath() + "." + timestamp + ".old"));
try {
Files.move(new File(VripperApplication.dataPath).toPath(), new File(VripperApplication.dataPath + "." + timestamp + ".old").toPath());
Files.move(new File(appCommandRunner.getDataPath()).toPath(), new File(appCommandRunner.getDataPath() + "." + timestamp + ".old").toPath());
} catch (IOException ex) {
logger.error(String.format("Failed to rename %s to %s", VripperApplication.dataPath, VripperApplication.dataPath + ".old"));
logger.error(String.format("Failed to rename %s to %s", appCommandRunner.getDataPath(), appCommandRunner.getDataPath() + ".old"));
SpringContext.close();
}
}
@@ -125,7 +128,7 @@ public class PersistenceService {
String jsonContent = null;
try {
jsonContent = Files.readAllLines(Paths.get(VripperApplication.dataPath), StandardCharsets.UTF_8).stream().collect(Collectors.joining());
jsonContent = Files.readAllLines(Paths.get(appCommandRunner.getDataPath()), StandardCharsets.UTF_8).stream().collect(Collectors.joining());
} catch (Exception e) {
logger.error("data file cannot be read, previous state cannot be restored", e);
SpringContext.close();
@@ -0,0 +1 @@
base.dir=${user.home}
@@ -0,0 +1 @@
base.dir=${user.dir}
@@ -1,8 +1,9 @@
logging.level.org.springframework.web=INFO
logging.level.root=INFO
logging.level.org.apache.http=INFO
logging.file=${user.home}/.vripper/vripper.log
logging.file=${base.dir}/.vripper/vripper.log
server.port=${vripper.server.port:8080}
management.endpoints.web.exposure.include=shutdown
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
endpoints.shutdown.enabled=true
spring.profiles.active=portable