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 a90ac14
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
31 changes: 17 additions & 14 deletions cli/src/main/java/dev/enola/cli/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,8 @@
@CommandLine.Command(name = "server", description = "Start HTTP Server")
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"},
Expand All @@ -54,14 +45,14 @@ protected void run(EntityKindRepository ekr, EnolaServiceGrpc.EnolaServiceBlocki
throws Exception {

// gRPC API
if (grpcPort != null) {
if (ports.grpcPort != null) {
var grpcServer = new EnolaGrpcServer(esp.get(ekr));
grpcServer.start(grpcPort);
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));
var httpServer = new SunServer(new InetSocketAddress(ports.httpPort));
new UI(service).register(httpServer);
new RestAPI(service).register(httpServer);
httpServer.start();
Expand All @@ -76,4 +67,16 @@ protected void run(EntityKindRepository ekr, EnolaServiceGrpc.EnolaServiceBlocki
httpServer.close();
}
}

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

@CommandLine.Option(
names = {"--grpcPort"},
description = "gRPC API Port")
Integer grpcPort;
}
}
33 changes: 32 additions & 1 deletion 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 @@ -123,6 +123,37 @@ public void serve() {
assertThat(exec).hasExitCode(0).out().startsWith("gRPC API server now available on port ");
}

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

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

@Test
public void rosetta() throws IOException {
try (var r = TestResource.create(YAML_UTF_8)) {
Expand Down

0 comments on commit a90ac14

Please sign in to comment.