From c91e742749f74c3296040b32d1670b032dd87b48 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 10 Oct 2023 14:15:50 -0400 Subject: [PATCH] Added application.properties support. --- pom.xml | 29 ++++++++++ .../d_package_search/DPackageSearch.java | 54 ++++++++++++++++--- .../d_package_search/WebApiRunner.java | 3 +- 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 23c2170..119906b 100644 --- a/pom.xml +++ b/pom.xml @@ -38,4 +38,33 @@ + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.6.0 + + + + com.andrewlalis.d_package_search.DPackageSearch + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java b/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java index 3a84e2d..e791819 100644 --- a/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java +++ b/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java @@ -4,18 +4,28 @@ import com.andrewlalis.d_package_search.impl.DubRegistryPackageFetcher; import com.andrewlalis.d_package_search.impl.LucenePackageIndexer; import com.andrewlalis.d_package_search.impl.LucenePackageSearcher; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.ArrayList; +import java.util.Properties; public class DPackageSearch { + public static Properties APPLICATION_PROPERTIES; + public static void main(String[] args) { + APPLICATION_PROPERTIES = loadApplicationProperties(); Path indexPath = Path.of("package-index"); - startIndexerThread(new IndexGenerator( - new DubRegistryPackageFetcher(), - () -> new LucenePackageIndexer(indexPath) - )); - new WebApiRunner(new LucenePackageSearcher(indexPath)).run(); + if (isPropTrue("indexer.enabled")) { + startIndexerThread(new IndexGenerator( + new DubRegistryPackageFetcher(), + () -> new LucenePackageIndexer(indexPath) + )); + } + if (isPropTrue("server.enabled")) { + new WebApiRunner(new LucenePackageSearcher(indexPath)).run(); + } } /** @@ -36,7 +46,7 @@ public class DPackageSearch { System.out.println("Re-indexing packages now."); indexGenerator.run(); try { - Thread.sleep(Duration.ofMinutes(60)); + Thread.sleep(Duration.ofMinutes(getIntProp("indexer.delay-minutes", 60))); } catch (InterruptedException e) { System.err.println("Indexing thread interrupted: " + e.getMessage()); break; @@ -44,4 +54,36 @@ public class DPackageSearch { } }); } + + private static Properties loadApplicationProperties() { + Properties props = new Properties(); + props.setProperty("server.port", "8080"); + props.setProperty("server.host", "localhost"); + props.setProperty("server.enabled", "true"); + props.setProperty("indexer.enabled", "true"); + props.setProperty("indexer.delay-minutes", "60"); + Path propsFilePath = Path.of("application.properties"); + if (Files.exists(propsFilePath)) { + try (var in = Files.newInputStream(propsFilePath)) { + props.load(in); + System.out.println("Loaded application properties from " + propsFilePath); + } catch (IOException e) { + System.err.println("Failed to load application properties from " + propsFilePath + ": " + e.getMessage()); + } + } + return props; + } + + public static boolean isPropTrue(String name) { + return APPLICATION_PROPERTIES.getProperty(name, "false").equalsIgnoreCase("true"); + } + + public static int getIntProp(String name, int defaultValue) { + String s = APPLICATION_PROPERTIES.getProperty(name, Integer.toString(defaultValue)); + return Integer.parseInt(s); + } + + public static String getStringProp(String name) { + return APPLICATION_PROPERTIES.getProperty(name); + } } diff --git a/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java b/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java index 2406f56..77c7720 100644 --- a/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java +++ b/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java @@ -37,7 +37,8 @@ public final class WebApiRunner extends Handler.Abstract implements Runnable { threadPool.setName("http-server"); Server server = new Server(threadPool); ServerConnector connector = new ServerConnector(server); - connector.setPort(8080); + connector.setPort(DPackageSearch.getIntProp("server.port", 8080)); + connector.setHost(DPackageSearch.getStringProp("server.host")); server.addConnector(connector); server.setHandler(this); try {