Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions contrib/tinyweb/example_auth.ae
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
26 changes: 15 additions & 11 deletions contrib/tinyweb/example_composition.ae
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions contrib/tinyweb/example_sse.ae
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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)
}
19 changes: 14 additions & 5 deletions contrib/tinyweb/example_static.ae
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 13 additions & 4 deletions contrib/tinyweb/example_websocket.ae
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Original file line number Diff line number Diff line change
@@ -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.
Loading