diff --git a/contrib/tinyweb/example_auth.ae b/contrib/tinyweb/example_auth.ae index 8d4f265c6..b8e8220b0 100644 --- a/contrib/tinyweb/example_auth.ae +++ b/contrib/tinyweb/example_auth.ae @@ -55,16 +55,23 @@ main() { } } - println("Starting server on localhost:8080...") - println("") - println("Test the auth flow:") - println(" curl http://localhost:8080/api/profile") - println(" -> 403 Try logging in again") - println("") - println(" curl -H 'Cookie: session=valid-session-abc' http://localhost:8080/api/profile") - println(" -> 200 User is logged in: alice@example.com") + http.monitor(tinyweb.http_server(server)) { + listening() |host: string, port: int| { + println("Test the auth flow:") + println(" curl http://localhost:${port}/api/profile") + println(" -> 403 Try logging in again") + println("") + println(" curl -H 'Cookie: session=valid-session-abc' http://localhost:${port}/api/profile") + println(" -> 200 User is logged in: alice@example.com") + println("\nPress Ctrl+C to stop.") + } + } + println("Starting server on localhost:8080...") result = tinyweb.tw_start(server) - if result != "" { println("ERROR: Failed to start server: ${result}") } + if result != "" { + println("ERROR: Failed to start server: ${result}") + } + tinyweb.tw_stop(server) } diff --git a/contrib/tinyweb/example_composition.ae b/contrib/tinyweb/example_composition.ae index ef1b03fad..d056dfaa5 100644 --- a/contrib/tinyweb/example_composition.ae +++ b/contrib/tinyweb/example_composition.ae @@ -75,19 +75,23 @@ main() { } } + http.monitor(tinyweb.http_server(server)) { + listening() |host: string, port: int| { + println("Server running! Try:") + println(" curl http://localhost:${port}/api/v1/health") + println(" curl http://localhost:${port}/api/v1/greeting/Jane/Doe") + println(" curl -H 'X-Admin-Token: secret42' http://localhost:${port}/admin/dashboard") + println(" curl http://localhost:${port}/admin/dashboard # -> 403") + println(" curl http://localhost:${port}/shop/catalog/products/99") + println(" curl http://localhost:${port}/extra/ping") + println("\nPress Ctrl+C to stop.") + } + } + println("Starting server on localhost:8080...") result = tinyweb.tw_start(server) - - if result == "" { - println("Server running! Try:") - println(" curl http://localhost:8080/api/v1/health") - println(" curl http://localhost:8080/api/v1/greeting/Jane/Doe") - println(" curl -H 'X-Admin-Token: secret42' http://localhost:8080/admin/dashboard") - println(" curl http://localhost:8080/admin/dashboard # -> 403") - println(" curl http://localhost:8080/shop/catalog/products/99") - println(" curl http://localhost:8080/extra/ping") - } else { - println("ERROR: Failed to start server") + if result != "" { + println("ERROR: Failed to start server: ${result}") } tinyweb.tw_stop(server) diff --git a/contrib/tinyweb/example_sse.ae b/contrib/tinyweb/example_sse.ae index 291ed2fe3..711babf1b 100644 --- a/contrib/tinyweb/example_sse.ae +++ b/contrib/tinyweb/example_sse.ae @@ -1,6 +1,7 @@ // Server-Sent Events (SSE) and chunked Example — streaming to browser clients import contrib.tinyweb +import std.http main() { println("=== TinyWeb SSE + Chunked Example ===\n") @@ -58,10 +59,19 @@ main() { } } - println("Open http://localhost:8080/ or: curl -N http://localhost:8080/sse/events") - println("Chunked: curl -N http://localhost:8080/countdown") + http.monitor(tinyweb.http_server(server)) { + listening() |host: string, port: int| { + println("Open http://localhost:${port}/ or: curl -N http://localhost:${port}/sse/events") + println("Chunked: curl -N http://localhost:${port}/countdown") + println("\nPress Ctrl+C to stop.") + } + } + println("Starting server on localhost:8080...") result = tinyweb.tw_start(server) - if result != "" { println("ERROR: Failed to start server: ${result}") } + if result != "" { + println("ERROR: Failed to start server: ${result}") + } + tinyweb.tw_stop(server) } diff --git a/contrib/tinyweb/example_static.ae b/contrib/tinyweb/example_static.ae index e18d75901..9f1b2ef40 100644 --- a/contrib/tinyweb/example_static.ae +++ b/contrib/tinyweb/example_static.ae @@ -36,12 +36,21 @@ main() { } } - println("Starting server on localhost:8080...") - println(" http://localhost:8080/ — index page") - println(" http://localhost:8080/static/... — static files from cwd") - println(" http://localhost:8080/api/health — JSON health check") + http.monitor(tinyweb.http_server(server)) { + listening() |host: string, port: int| { + println("Server running! Try:") + println(" http://localhost:${port}/ — index page") + println(" http://localhost:${port}/static/... — static files from cwd") + println(" http://localhost:${port}/api/health — JSON health check") + println("\nPress Ctrl+C to stop.") + } + } + println("Starting server on localhost:8080...") result = tinyweb.tw_start(server) - if result != "" { println("ERROR: Failed to start server: ${result}") } + if result != "" { + println("ERROR: Failed to start server: ${result}") + } + tinyweb.tw_stop(server) } diff --git a/contrib/tinyweb/example_websocket.ae b/contrib/tinyweb/example_websocket.ae index 36083946f..1eb89a40c 100644 --- a/contrib/tinyweb/example_websocket.ae +++ b/contrib/tinyweb/example_websocket.ae @@ -48,12 +48,21 @@ main() { } } - println("Open http://localhost:8080/ in a browser for the demo page.") - println(" ws://localhost:8080/echo — simple echo") - println(" ws://localhost:8080/foo/eee — echo x3") + http.monitor(tinyweb.http_server(server)) { + listening() |host: string, port: int| { + println("Open http://localhost:${port}/ in a browser for the demo page.") + println(" ws://localhost:${port}/echo — simple echo") + println(" ws://localhost:${port}/foo/eee — echo x3") + println("\nPress Ctrl+C to stop.") + } + } + println("Starting server on localhost:8080...") result = tinyweb.tw_start(server) - if result != "" { println("ERROR: Failed to start server: ${result}") } + if result != "" { + println("ERROR: Failed to start server: ${result}") + } + tinyweb.tw_stop(server) println("Server stopped.") } diff --git a/new_changelogs/20261029T183900Z-changed-tinyweb-example-hints.md b/new_changelogs/20261029T183900Z-changed-tinyweb-example-hints.md new file mode 100644 index 000000000..afe680e8a --- /dev/null +++ b/new_changelogs/20261029T183900Z-changed-tinyweb-example-hints.md @@ -0,0 +1,6 @@ +- **The TinyWeb examples print their usage hints once the server is + listening.** `example_auth`, `example_composition`, `example_sse`, + `example_static` and `example_websocket` printed curl hints before + `tw_start`, so the hints appeared even when the bind failed, and named a + hard-coded port. They now print from `http.monitor`'s `listening` callback + with the bound port, as `example_app` does.