Skip to content

Commit 415ec65

Browse files
authored
feat: add random port for the web app (#369)
1 parent 6530469 commit 415ec65

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Terrabuild/Web/GraphServer.fs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,11 @@ let start (graphArgs: ParseResults<ConsoleArgs>) (logEnabled: bool) (debugEnable
326326
]
327327
|> List.append (processDir |> Option.map (fun dir -> Path.Combine(dir, "ui")) |> Option.toList)
328328
|> List.map (fun path -> Path.GetFullPath(path))
329-
let port = graphArgs.TryGetResult(ConsoleArgs.Port) |> Option.defaultValue 5179
330-
let url = $"http://127.0.0.1:{port}"
329+
let portOpt = graphArgs.TryGetResult(ConsoleArgs.Port)
330+
let url =
331+
match portOpt with
332+
| Some port -> $"http://127.0.0.1:{port}"
333+
| None -> "http://127.0.0.1:0"
331334
let builder = WebApplication.CreateBuilder()
332335
builder.Logging.ClearProviders() |> ignore
333336
if debugEnabled then
@@ -618,8 +621,15 @@ let start (graphArgs: ParseResults<ConsoleArgs>) (logEnabled: bool) (debugEnable
618621
}))
619622
|> ignore
620623

621-
let runTask = app.RunAsync()
624+
app.StartAsync().Wait()
625+
let boundUrl =
626+
match portOpt with
627+
| Some _ -> url
628+
| None ->
629+
app.Urls
630+
|> Seq.tryHead
631+
|> Option.defaultValue url
622632
if shouldOpenBrowser then
623-
openBrowser url
624-
runTask.Wait()
633+
openBrowser boundUrl
634+
app.WaitForShutdownAsync().Wait()
625635
0

0 commit comments

Comments
 (0)