Skip to content

Commit

Permalink
doesn't open browser in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vananiev committed Nov 18, 2024
1 parent 9c93bff commit e20d387
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/java/ru/investbook/InvestbookApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class InvestbookApplication {

public static void main(String[] args) {
try(LoadingPageServer loadingPageServer = new LoadingPageServer()) {
LoadingPageServerUtils.setSpringProfilesFromArgs(args);

Check warning on line 38 in src/main/java/ru/investbook/InvestbookApplication.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/InvestbookApplication.java#L38

Added line #L38 was not covered by tests
if (LoadingPageServerUtils.shouldOpenHomePageAfterStart()) {
loadingPageServer.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
Expand All @@ -36,13 +40,28 @@
@UtilityClass
public class LoadingPageServerUtils {

private static final String CONF_PROPERTIES = "app/application-conf.properties";
private static final String RESOURCE_CONF_PROPERTIES = "application-conf.properties";
private static final String PROPERTIES_LOCATION_DIR = "app";
private static final String DEFAULT_PROFILE = "conf";
private static final List<String> profiles = new CopyOnWriteArrayList<>();

Check warning on line 45 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L45

Added line #L45 was not covered by tests

public static void setSpringProfilesFromArgs(String[] args) {
for (String arg : args) {
if (arg.startsWith("--spring.profiles.active")) {
String[] _profiles = arg.replace("--spring.profiles.active", "")
.replace("=", "")
.trim()
.split(",");
profiles.clear();
profiles.addAll(List.of(_profiles));
Collections.reverse(profiles); // last spring profile property should win
profiles.add(DEFAULT_PROFILE);

Check warning on line 57 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L50-L57

Added lines #L50 - L57 were not covered by tests
}
}
}

Check warning on line 60 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L60

Added line #L60 was not covered by tests

public static int getMainAppPort() {
try {
Properties properties = loadProperties();
String value = properties.getProperty("server.port", "2030");
String value = getProperty("server.port", "2030");

Check warning on line 64 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L64

Added line #L64 was not covered by tests
return Integer.parseInt(value);
} catch (Exception e) {
log.warn("Can't find 'server.port' property, fallback to default value: 2030", e);
Expand All @@ -52,23 +71,37 @@ public static int getMainAppPort() {

public static boolean shouldOpenHomePageAfterStart() {
try {
Properties properties = loadProperties();
String value = properties.getProperty("investbook.open-home-page-after-start", "true");
String value = getProperty("investbook.open-home-page-after-start", "true");

Check warning on line 74 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L74

Added line #L74 was not covered by tests
return Boolean.parseBoolean(value);
} catch (Exception e) {
log.warn("Can't find 'investbook.open-home-page-after-start' fallback to default value: true", e);
return true;
}
}

private static Properties loadProperties() throws IOException {
private static String getProperty(String key, String defaultValue) {
for (String profile : profiles) {
try {
Properties properties = loadProperties(profile);
@Nullable String value = properties.getProperty(key, null);

Check warning on line 86 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L85-L86

Added lines #L85 - L86 were not covered by tests
if (value != null) {
return value;

Check warning on line 88 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L88

Added line #L88 was not covered by tests
}
} catch (Exception ignore) {
}
}
return defaultValue;

Check warning on line 93 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L90-L93

Added lines #L90 - L93 were not covered by tests
}

private static Properties loadProperties(String profile) throws IOException {
Properties properties = new Properties();
Path path = Path.of(CONF_PROPERTIES);
String file = "application-" + profile + ".properties";
Path path = Path.of(PROPERTIES_LOCATION_DIR).resolve(file);

Check warning on line 99 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L98-L99

Added lines #L98 - L99 were not covered by tests
try (Reader reader = Files.newBufferedReader(path)) { // default is UTF_8
properties.load(reader);
} catch (Exception e) {
// Properties file is not found in app installation path, read default file from class path
try (InputStream in = requireNonNull(LoadingPageServerUtils.class.getResourceAsStream(RESOURCE_CONF_PROPERTIES));
try (InputStream in = requireNonNull(LoadingPageServerUtils.class.getResourceAsStream("/" + file));

Check warning on line 104 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L104

Added line #L104 was not covered by tests
Reader reader = new InputStreamReader(in, UTF_8)) {
properties.load(reader);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ investbook.try-alt-index-logo-url = false

# Hides exceptions generated by Jackson when serializing Entity object with LAZY fields.
# Same works better done by annotation LAZY fields by: JsonIgnoreProperties({"hibernateLazyInitializer"})
#spring.jackson.serialization.fail-on-empty-beans=false
#spring.jackson.serialization.fail-on-empty-beans=false

investbook.open-home-page-after-start=false

0 comments on commit e20d387

Please sign in to comment.