SuaveIO / suave

Suave is a simple web development F# library providing a lightweight web server and a set of combinators to manipulate route flow and task composition.
https://suave.io
Other
1.32k stars 198 forks source link

How to have suave auto select and report the real listening port #754

Closed samuelventura closed 3 years ago

samuelventura commented 3 years ago

Code below with 0 port seems to correctly launch the server.

How can I find out programmatically the TCP port the server is listening at?

open System
open System.Threading
open Suave

[<EntryPoint>]
let main argv =
    let cts = new CancellationTokenSource()

    let conf =
        { defaultConfig with
              cancellationToken = cts.Token
              bindings = [ HttpBinding.createSimple HTTP "127.0.0.1" 0 ] }

    let listening, server =
        startWebServerAsync conf (Successful.OK "Hello World")

    printfn "Listening %O" listening
    printfn "Server %O" server

    Async.Start(server, cts.Token)
    printfn "Make requests now"
    Console.ReadKey true |> ignore

    cts.Cancel()

    0 // return an integer exit code

outputs

$ dotnet run
Listening Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.FSharpOption`1[Suave.Tcp+StartedData][]]                                                     n`1[Suave.Tcp+StartedData][]]
Server Microsoft.FSharp.Control.FSharpAsync`1[Microsoft.FSharp.Core.Unit]
Make requests now
[01:18:52 INF] Smooth! Suave listener started in 75.7ms with binding 127.0.0.1:0  
ademar commented 3 years ago

Hi Samuel, I've pushed a fix for this issue and will be releasing a new version in the following days.