Skip to content

Commit

Permalink
CLI UI: start on port 8092 by default (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi authored Oct 25, 2023
1 parent 429db1a commit 1cd22a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void debug(Object message) {
}

@CommandLine.Spec protected CommandLine.Model.CommandSpec command;
private final CLILogger logger = new CLILoggerImpl(() -> getRootCmd(), () -> command);
@Getter private final CLILogger logger = new CLILoggerImpl(() -> getRootCmd(), () -> command);
private final GithubRepositoryDownloader githubRepositoryDownloader =
new GithubRepositoryDownloader(
new JGitClient(), logger, LangStreamCLI.getLangstreamCLIHomeDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package ai.langstream.cli.commands.applications;

import ai.langstream.cli.CLILogger;
import ai.langstream.cli.NamedProfile;
import ai.langstream.cli.api.model.Gateways;
import io.undertow.Handlers;
Expand Down Expand Up @@ -56,6 +57,12 @@ public class UIAppCmd extends BaseApplicationCmd {
@CommandLine.Parameters(description = "Application ID")
private String applicationId;

@CommandLine.Option(
names = {"-p", "--port"},
description = "Port for the local webserver and UI. If 0, a random port will be used. ",
defaultValue = "8092")
private int port = 8092;

@Override
@SneakyThrows
public void run() {
Expand Down Expand Up @@ -91,16 +98,17 @@ public void run(Consumer<String> lineConsumer) {
}
}
};
final Undertow server = startServer(() -> appModel, apiGatewayUrl, logSupplier);
final int port =
((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort();
log("Started UI at http://localhost:" + port);
startServer(port, () -> appModel, apiGatewayUrl, logSupplier, getLogger());
Thread.sleep(Long.MAX_VALUE);
}

@SneakyThrows
public static Undertow startServer(
Supplier<AppModel> appModel, String apiGatewayUrl, LogSupplier logsStream) {
int port,
Supplier<AppModel> appModel,
String apiGatewayUrl,
LogSupplier logsStream,
CLILogger logger) {
String forwardHost;
if (apiGatewayUrl.startsWith("wss://")) {
forwardHost = "https://" + apiGatewayUrl.substring("wss://".length());
Expand Down Expand Up @@ -138,15 +146,15 @@ public static Undertow startServer(
}
};

AtomicInteger port = new AtomicInteger();
AtomicInteger actualPort = new AtomicInteger();

ResourceHandler resourceHandler =
Handlers.resource(
new ClassPathResourceManager(UIAppCmd.class.getClassLoader(), "app-ui"));
HttpHandler appConfigHandler =
exchange -> {
final AppModel result = appModel.get();
result.setBaseUrl("ws://localhost:" + port.get());
result.setBaseUrl("ws://localhost:" + actualPort.get());
final String json = jsonBodyWriter.writeValueAsString(result);
exchange.getResponseHeaders()
.put(HttpString.tryFromString("Content-Type"), "application/json");
Expand All @@ -161,13 +169,15 @@ public static Undertow startServer(

Undertow server =
Undertow.builder()
.addHttpListener(0, "localhost")
.addHttpListener(port, "localhost")
.setHandler(routingHandler)
.build();
server.start();
port.set(((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort());
actualPort.set(
((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort());

openBrowserAtPort("open", port.get());
logger.log("Started UI at http://localhost:" + actualPort.get());
openBrowserAtPort("open", actualPort.get());

return server;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public class LocalRunApplicationCmd extends BaseDockerCmd {
description = "Start the UI")
private boolean startUI = true;

@CommandLine.Option(
names = {"-up", "--ui-port"},
description = "Port for the local webserver and UI. If 0, a random port will be used.",
defaultValue = "8092")
private int uiPort = 8092;

@CommandLine.Option(
names = {"--only-agent"},
description = "Run only one agent")
Expand Down Expand Up @@ -501,6 +507,7 @@ private void startUI(String tenant, String applicationId, Path outputLog, Proces
final String finalBody = body;
final String mermaidDefinition = MermaidAppDiagramGenerator.generate(finalBody);
UIAppCmd.startServer(
uiPort,
() -> {
final UIAppCmd.AppModel appModel = new UIAppCmd.AppModel();
appModel.setTenant(tenant);
Expand All @@ -512,7 +519,8 @@ private void startUI(String tenant, String applicationId, Path outputLog, Proces
return appModel;
},
"ws://localhost:8091",
getTailLogSupplier(outputLog));
getTailLogSupplier(outputLog),
getLogger());
}

private UIAppCmd.LogSupplier getTailLogSupplier(Path outputLog) {
Expand Down

0 comments on commit 1cd22a4

Please sign in to comment.