WAI (Web Application Interface)
repository·master·Indexed 21 days ago
https://github.com/yesodweb/waiA standard interface between Haskell web frameworks and web servers that enables portability and modularity through middleware. The ecosystem includes the Warp high-performance server, warp-tls for TLS support, and various utility packages such as wai-app-static for static files, wai-conduit for streaming, wai-websockets for WebSocket integration, and time-manager for resettable timeouts.
What's inside WAI
- Warp is a high-performance server library designed for Haskell web applications. It serves as the server implementation for the WAI (Web Application Interface) standard, supporting both HTTP/1.x and HTTP/2 protocols.
Overview of warp-tls
masterThewarp-tlspackage allows you to serve WAI (Web Application Interface) applications using the Warp webserver with TLS (Transport Layer Security) support, utilizing the Haskell TLS library.Overview of mime-types handling
masterThemime-typespackage provides basic types and functions for handling MIME types within the WAI ecosystem. It is used to manage content-type information for web requests and responses.Use websockets with WAI applications
masterThewai-websocketspackage allows you to integrate WebSocket support into WAI (Web Application Interface) applications. It is primarily designed for applications hosted using the Warp web server.Use wai-app-static for serving static files
masterThewai-app-staticpackage provides a WAI (Web Application Interface) application designed to serve static files. It can be integrated into any WAI-compliant web server to handle requests for static assets like images, CSS, or JavaScript files.Run MonadCGI programs with WAI handlers
masterThewai-frontend-monadcgipackage provides a way to execute programs written using theMonadCGIinterface within any WAI (Web Application Interface) handler. This is primarily used to bridge legacy or existing CGI applications into modern Haskell web server environments like Warp.Use time-manager for resettable timeouts and thread management
masterThe
time-managerpackage provides tools for managing action lifecycles via two primary mechanisms:- Resettable Timeouts: Use
System.TimeManagerto run actions with timeouts that can be reset. - Thread Management: Use
ThreadManagerto run actions that ensure all threads forked within that manager are automatically killed when the main action completes.
- Resettable Timeouts: Use
Use wai-conduit for streaming data in WAI
masterSince WAI version 3.0.0, the core WAI library does not include a built-in streaming data abstraction. If you require streaming functionality similar to what was available in WAI 2.x, you should use thewai-conduitlibrary to provide this abstraction.What is auto-update and when should I use it?
masterThe
auto-updatelibrary solves the problem of performing periodic background tasks (like updating anIORefwith the current time) without wasting resources during periods of low traffic.Instead of running a dedicated worker thread that consumes CPU and interferes with idle Garbage Collection (GC) even when no requests are being processed,
auto-updateuses a hybrid approach:- High Volume: Actions are performed by a dedicated worker thread at a scheduled interval.
- Low Volume: Actions are executed by the calling thread (the thread handling the web request) only when needed.
This prevents the 'pessimization' of running background threads when the request frequency is lower than the update interval.
What is WAI (Web Application Interface)
masterWAI (Web Application Interface) is a standard interface in Haskell that decouples web frameworks from web servers. By targeting WAI, a web application becomes portable: it can be deployed to any backend that provides a WAI adapter. The most common backend used with WAI is the Warp web server.Implement basic request dispatching
masterWAI applications can perform dispatching by inspecting the
rawPathInfofield of theRequestobject. You can use pattern matching on the path to return differentResponsevalues based on the URL requested.app3 :: Application app3 request respond = respond $ case rawPathInfo request of "/" -> index "/raw/" -> plainIndex _ -> notFound plainIndex :: Response plainIndex = responseFile status200 [("Content-Type", "text/plain")] "index.html" Nothing notFound :: Response notFound = responseLBS status404 [("Content-Type", "text/plain")] "404 - Not Found"Extend WAI functionality with middleware
masterWAI provides modularity and code-sharing through the use of middleware and WAI applications. Middleware allows you to write logic (such as logging, authentication, or compression) that can be reused across any web framework that targets the WAI interface.