WAI (Web Application Interface)

repository·master·Indexed 21 days ago

https://github.com/yesodweb/wai

A 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.

Tokens
2.3K
Snippets
8
Records
22
Agent score
70%

What's inside WAI

  1. Overview of Warp

    master
    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.
  2. Use wai-app-static for serving static files

    master
    The wai-app-static package 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.
  3. Run MonadCGI programs with WAI handlers

    master
    The wai-frontend-monadcgi package provides a way to execute programs written using the MonadCGI interface 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.
  4. Use time-manager for resettable timeouts and thread management

    master

    The time-manager package provides tools for managing action lifecycles via two primary mechanisms:

    1. Resettable Timeouts: Use System.TimeManager to run actions with timeouts that can be reset.
    2. Thread Management: Use ThreadManager to run actions that ensure all threads forked within that manager are automatically killed when the main action completes.
  5. Use wai-conduit for streaming data in WAI

    master
    Since 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 the wai-conduit library to provide this abstraction.
  6. What is auto-update and when should I use it?

    master

    The auto-update library solves the problem of performing periodic background tasks (like updating an IORef with 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-update uses a hybrid approach:

    1. High Volume: Actions are performed by a dedicated worker thread at a scheduled interval.
    2. 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.

  7. What is WAI (Web Application Interface)

    master
    WAI (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.
  8. Implement basic request dispatching

    master

    WAI applications can perform dispatching by inspecting the rawPathInfo field of the Request object. You can use pattern matching on the path to return different Response values 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"
  9. Extend WAI functionality with middleware

    master
    WAI 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.