hyperbole-0.2.0: Web Framework inspired by HTMX
Safe HaskellSafe-Inferred
LanguageGHC2021

Web.Hyperbole.Application

Synopsis

Documentation

waiApplication :: Route route => (ByteString -> ByteString) -> (route -> Eff '[Hyperbole, IOE] ()) -> Application Source #

application :: Route route => (ByteString -> ByteString) -> (route -> Eff '[Hyperbole, IOE] ()) -> Application Source #

Start both a websockets and a WAI server. Wai app serves initial pages, and attempt to process actions via sockets If the socket connection is unavailable, will fall back to the WAI app to process actions

websocketsOr :: ConnectionOptions -> ServerApp -> Application -> Application #

Upgrade a websockets ServerApp to a wai Application. Uses the given backup Application to handle Requests that are not WebSocket requests.

websocketsOr opts ws_app backup_app = \req respond ->
    case websocketsApp opts ws_app req of
        Nothing  -> backup_app req send_response
        Just res -> respond res

For example, below is an Application that sends "Hello, client!" to each connected client.

app :: Application
app = websocketsOr defaultConnectionOptions wsApp backupApp
  where
    wsApp :: ServerApp
    wsApp pending_conn = do
        conn <- acceptRequest pending_conn
        sendTextData conn ("Hello, client!" :: Text)

    backupApp :: Application
    backupApp _ respond = respond $ responseLBS status400 [] "Not a WebSocket request"