Skip to content

Commit

Permalink
Merge pull request #623 from spacious-team/task-disable-browser-on-debug
Browse files Browse the repository at this point in the history
Не открывать браузер в режиме debug
  • Loading branch information
vananiev authored Nov 18, 2024
2 parents 9c93bff + e20d387 commit 83e54f1
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#L37-L38

Added lines #L37 - L38 were not covered by tests
if (LoadingPageServerUtils.shouldOpenHomePageAfterStart()) {
loadingPageServer.start();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L40 was not covered by tests
}
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");
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");
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;

Check warning on line 78 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-L78

Added lines #L74 - L78 were not covered by tests
}
}

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);
try (Reader reader = Files.newBufferedReader(path)) { // default is UTF_8
properties.load(reader);
} catch (Exception e) {

Check warning on line 102 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#L97-L102

Added lines #L97 - L102 were not covered by tests
// 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));
Reader reader = new InputStreamReader(in, UTF_8)) {
properties.load(reader);

Check warning on line 106 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-L106

Added lines #L104 - L106 were not covered by tests
}
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 83e54f1

Please sign in to comment.