(service) Let singleservice configure ports and bind addresses

This commit is contained in:
Viktor Lofgren 2024-04-25 13:49:57 +02:00
parent 282022d64e
commit 3952ef6ca5
2 changed files with 71 additions and 16 deletions

View File

@ -140,13 +140,15 @@ public class ZkServiceRegistry implements ServiceRegistryIf {
@Override
public int requestPort(String externalHost,
ServiceKey<?> key) {
if (!Boolean.getBoolean("service.random-port")) {
return switch (key) {
case ServiceKey.Rest rest -> 80;
case ServiceKey.Grpc<?> grpc -> 81;
case ServiceKey.Rest rest -> Integer.getInteger("service.http-port", 80);
case ServiceKey.Grpc<?> grpc -> Integer.getInteger("service.grpc-port",81);
};
}
int portRangeLow = 12_000;
int portRangeHigh = 12_999;

View File

@ -1,6 +1,73 @@
package nu.marginalia;
/** Springboard for launching services outside of docker */
public class SingleService {
public static void main(String... args) {
if (!configure(args)) {
System.out.println("Usage: SingleService <service> bind-address:bind-port-http:bind-port-grpc announce-address [args...]");
return;
}
requireEnv("ZOOKEEPER_HOSTS", "Comma-separated list of zookeeper hosts");
requireEnv("WMSA_HOME", "Path to the install directory of the project");
String serviceName = args[0];
String[] serviceArgs = new String[args.length - 3];
System.arraycopy(args, 3, serviceArgs, 0, serviceArgs.length);
for (var service : Service.values()) {
if (service.name.equals(serviceName)) {
service.run(serviceArgs);
}
}
}
private static void requireEnv(String env, String desc) {
if (System.getenv(env) == null) {
throw new IllegalArgumentException("Missing environment variable: " + env + " - " + desc);
}
else {
System.out.println("Found environment variable: " + env + " = " + System.getenv(env));
}
}
/** Set system properties for the address and ports for the service.
*
* @return true if the configuration was successful
* */
private static boolean configure(String[] args) {
if (args.length < 3)
return false;
try {
final String bindAddress_http_grpc = args[1];
final String announceAddress = args[2];
final String[] bindParts = bindAddress_http_grpc.split(":");
if (bindParts.length < 3)
return false;
String bindAddress = bindParts[0];
int httpPort = Integer.parseInt(bindParts[1]);
int grpcPort = Integer.parseInt(bindParts[2]);
System.out.println("Configuring service with bind address: " + bindAddress + " http port: " + httpPort + " grpc port: " + grpcPort + " announce address: " + announceAddress);
System.setProperty("service.bind-address", bindAddress);
System.setProperty("service.http-port", Integer.toString(httpPort));
System.setProperty("service.grpc-port", Integer.toString(grpcPort));
System.setProperty("service.host", announceAddress);
return true;
}
catch (NumberFormatException e) {
return false;
}
}
enum Service {
IndexService("index", "nu.marginalia.index.IndexMain"),
ControlService("control", "nu.marginalia.control.ControlMain"),
@ -31,19 +98,5 @@ public class SingleService {
}
}
public static void main(String... args) {
if (args.length == 0) {
System.out.println("Usage: SingleService <service> [args...]");
}
String serviceName = args[0];
String[] serviceArgs = new String[args.length - 1];
System.arraycopy(args, 1, serviceArgs, 0, serviceArgs.length);
for (var service : Service.values()) {
if (service.name.equals(serviceName)) {
service.run(serviceArgs);
}
}
}
}