Documentation Language: Swift

Method

run()

Starts the HTTP server and processes connections until termination.

func run() async throws

Discussion

This method binds to the specified port and begins accepting HTTP connections. It runs indefinitely until the process receives a termination signal (e.g., SIGINT from Ctrl+C).

The server handles each connection concurrently using structured concurrency, allowing multiple simultaneous clients.

Implementation Notes

This method uses NIOAsyncChannel to bridge SwiftNIO’s channel pipeline with Swift’s async/await model. Each accepted connection is handled in a separate task within a task group.

Example

let server = PreviewServer(rootDirectory: docsURL, port: 8080)
print("Starting server at http://localhost:8080/")
try await server.run()