Skip to content

Commit

Permalink
feat (cli): enola server CLI with --httpPort and/or --grpcPort
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 30, 2023
1 parent f561edc commit 228336b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
1 change: 1 addition & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ java_binary(
"//core/lib:core_java_grpc",
"//core/lib:core_java_proto",
"//core/lib:lib_java",
"//web/api",
"//web/rest",
"//web/sun",
"//web/ui-soy",
Expand Down
65 changes: 42 additions & 23 deletions cli/src/main/java/dev/enola/cli/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.enola.core.grpc.EnolaGrpcServer;
import dev.enola.core.meta.EntityKindRepository;
import dev.enola.core.proto.EnolaServiceGrpc;
import dev.enola.web.WebServer;
import dev.enola.web.rest.RestAPI;
import dev.enola.web.sun.SunServer;
import dev.enola.web.ui.UI;
Expand All @@ -28,52 +29,70 @@

import java.net.InetSocketAddress;

@CommandLine.Command(name = "server", description = "Start HTTP Server")
@CommandLine.Command(name = "server", description = "Start HTTP and/or gRPC Server/s")
public class ServerCommand extends CommandWithModel {

@CommandLine.Option(
names = {"--httpPort"},
required = true,
description = "HTTP Port")
int httpPort;

@CommandLine.Option(
names = {"--grpcPort"},
required = false,
description = "gRPC API Port")
Integer grpcPort;
@CommandLine.ArgGroup(exclusive = false, multiplicity = "1")
HttpAndOrGrpcPorts ports;

@CommandLine.Option(
names = {"--immediateExitOnlyForTest"},
defaultValue = "false",
hidden = true)
boolean immediateExitOnlyForTest;

private EnolaGrpcServer grpcServer;
private WebServer httpServer;

@Override
protected void run(EntityKindRepository ekr, EnolaServiceGrpc.EnolaServiceBlockingStub service)
throws Exception {

// gRPC API
if (grpcPort != null) {
var grpcServer = new EnolaGrpcServer(esp.get(ekr));
grpcServer.start(grpcPort);
if (ports.grpcPort != null) {
grpcServer = new EnolaGrpcServer(esp.get(ekr));
grpcServer.start(ports.grpcPort);
out.println("gRPC API server now available on port " + grpcServer.getPort());
}

// HTML UI + JSON REST API
var httpServer = new SunServer(new InetSocketAddress(httpPort));
new UI(service).register(httpServer);
new RestAPI(service).register(httpServer);
httpServer.start();
out.println(
"HTTP JSON REST API + HTML UI server started; open http:/"
+ httpServer.getInetAddress()
+ "/ui ...");
if (ports.httpPort != null) {
httpServer = new SunServer(new InetSocketAddress(ports.httpPort));
new UI(service).register(httpServer);
new RestAPI(service).register(httpServer);
httpServer.start();
out.println(
"HTTP JSON REST API + HTML UI server started; open http:/"
+ httpServer.getInetAddress()
+ "/ui ...");
}

if (!immediateExitOnlyForTest) {
Thread.currentThread().join();
} else {
close();
}
}

public void close() throws InterruptedException {
if (grpcServer != null) {
grpcServer.close();
}
if (httpServer != null) {
httpServer.close();
}
// TODO Thread.currentThread().interrupt();
}

static class HttpAndOrGrpcPorts {
@CommandLine.Option(
names = {"--httpPort"},
description = "HTTP Port")
Integer httpPort;

@CommandLine.Option(
names = {"--grpcPort"},
description = "gRPC API Port")
Integer grpcPort;
}
}
38 changes: 36 additions & 2 deletions cli/src/test/java/dev/enola/cli/EnolaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void get() {
}

@Test
public void serve() {
public void serveBothHttpAndGRPC() {
var exec =
cli(
"-v",
Expand All @@ -120,7 +120,41 @@ public void serve() {
"--grpcPort=0",
"--immediateExitOnlyForTest=true");
assertThat(exec).err().isEmpty();
assertThat(exec).hasExitCode(0).out().startsWith("gRPC API server now available on port ");
var out = assertThat(exec).hasExitCode(0).out();
out.startsWith("gRPC API server now available on port ");
out.contains("HTTP JSON REST API + HTML UI server started; open ");
}

@Test
public void serveOnlyHttp() {
var exec =
cli(
"-v",
"server",
"--model",
"classpath:cli-test-model.textproto",
"--httpPort=0",
"--immediateExitOnlyForTest=true");
assertThat(exec).err().isEmpty();
var out = assertThat(exec).hasExitCode(0).out();
out.startsWith("HTTP JSON REST API + HTML UI server started; open ");
out.doesNotContain("gRPC");
}

@Test
public void serveOnlyGrpc() {
var exec =
cli(
"-v",
"server",
"--model",
"classpath:cli-test-model.textproto",
"--grpcPort=0",
"--immediateExitOnlyForTest=true");
assertThat(exec).err().isEmpty();
var out = assertThat(exec).hasExitCode(0).out();
out.startsWith("gRPC API server now available on port ");
out.doesNotContain("HTML");
}

@Test
Expand Down
1 change: 1 addition & 0 deletions web/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
name = "api",
srcs = glob(["src/main/java/**/*.java"]),
visibility = [
"//cli:__subpackages__",
"//web:__subpackages__",
],
deps = [
Expand Down

0 comments on commit 228336b

Please sign in to comment.