Added application.properties support.
This commit is contained in:
parent
1d0df6ccaa
commit
c91e742749
29
pom.xml
29
pom.xml
|
@ -38,4 +38,33 @@
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.andrewlalis.d_package_search.DPackageSearch</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>make-assembly</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -4,19 +4,29 @@ import com.andrewlalis.d_package_search.impl.DubRegistryPackageFetcher;
|
||||||
import com.andrewlalis.d_package_search.impl.LucenePackageIndexer;
|
import com.andrewlalis.d_package_search.impl.LucenePackageIndexer;
|
||||||
import com.andrewlalis.d_package_search.impl.LucenePackageSearcher;
|
import com.andrewlalis.d_package_search.impl.LucenePackageSearcher;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class DPackageSearch {
|
public class DPackageSearch {
|
||||||
|
public static Properties APPLICATION_PROPERTIES;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
APPLICATION_PROPERTIES = loadApplicationProperties();
|
||||||
Path indexPath = Path.of("package-index");
|
Path indexPath = Path.of("package-index");
|
||||||
|
if (isPropTrue("indexer.enabled")) {
|
||||||
startIndexerThread(new IndexGenerator(
|
startIndexerThread(new IndexGenerator(
|
||||||
new DubRegistryPackageFetcher(),
|
new DubRegistryPackageFetcher(),
|
||||||
() -> new LucenePackageIndexer(indexPath)
|
() -> new LucenePackageIndexer(indexPath)
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
if (isPropTrue("server.enabled")) {
|
||||||
new WebApiRunner(new LucenePackageSearcher(indexPath)).run();
|
new WebApiRunner(new LucenePackageSearcher(indexPath)).run();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a new (virtual) thread that periodically re-generates the package
|
* Starts a new (virtual) thread that periodically re-generates the package
|
||||||
|
@ -36,7 +46,7 @@ public class DPackageSearch {
|
||||||
System.out.println("Re-indexing packages now.");
|
System.out.println("Re-indexing packages now.");
|
||||||
indexGenerator.run();
|
indexGenerator.run();
|
||||||
try {
|
try {
|
||||||
Thread.sleep(Duration.ofMinutes(60));
|
Thread.sleep(Duration.ofMinutes(getIntProp("indexer.delay-minutes", 60)));
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
System.err.println("Indexing thread interrupted: " + e.getMessage());
|
System.err.println("Indexing thread interrupted: " + e.getMessage());
|
||||||
break;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,8 @@ public final class WebApiRunner extends Handler.Abstract implements Runnable {
|
||||||
threadPool.setName("http-server");
|
threadPool.setName("http-server");
|
||||||
Server server = new Server(threadPool);
|
Server server = new Server(threadPool);
|
||||||
ServerConnector connector = new ServerConnector(server);
|
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.addConnector(connector);
|
||||||
server.setHandler(this);
|
server.setHandler(this);
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue