hyperbole-0.4.3: Interactive HTML apps using type-safe serverside Haskell
Safe HaskellNone
LanguageGHC2021

Web.Hyperbole.Application

Synopsis

Documentation

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"

defaultConnectionOptions :: ConnectionOptions #

The default connection options:

  • Nothing happens when a pong is received.
  • Compression is disabled.
  • Lenient unicode decoding.
  • 30 second timeout for connection establishment.

liveApp :: (ByteString -> ByteString) -> Eff '[Hyperbole, Server, Concurrent, IOE] Response -> Application Source #

Turn one or more Pages into a Wai Application. Respond using both HTTP and WebSockets

#EMBED Example/Docs/BasicPage.hs main

socketApp :: forall (es :: [Effect]). (IOE :> es, Concurrent :> es) => Eff (Hyperbole ': (Server ': es)) Response -> PendingConnection -> Eff es () Source #

basicDocument :: Text -> ByteString -> ByteString Source #

wrap HTML fragments in a simple document with a custom title and include required embeds

liveApp (basicDocument "App Title") (routeRequest router)

You may want to specify a custom document function to import custom javascript, css, or add other information to the <head>

import Data.String.Interpolate (i)
import Web.Hyperbole (scriptEmbed, cssResetEmbed)

#EMBED Example/Docs/App.hs customDocument

routeRequest :: forall (es :: [Effect]) route. (Hyperbole :> es, Route route) => (route -> Eff es Response) -> Eff es Response Source #

Route URL patterns to different pages

#EMBED ExampleDocsApp.hs import Example.Docs.Page

#EMBED ExampleDocsApp.hs type UserId

#EMBED ExampleDocsApp.hs data AppRoute

#EMBED ExampleDocsApp.hs instance Route

#EMBED ExampleDocsApp.hs router