Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- waiApp :: (ByteString -> ByteString) -> Eff '[Hyperbole, Server, IOE] Response -> Application
- websocketsOr :: ConnectionOptions -> ServerApp -> Application -> Application
- defaultConnectionOptions :: ConnectionOptions
- liveApp :: (ByteString -> ByteString) -> Eff '[Hyperbole, Server, IOE] Response -> Application
- socketApp :: IOE :> es => Eff (Hyperbole : (Server : es)) Response -> PendingConnection -> Eff es ()
- runServerSockets :: IOE :> es => Connection -> Eff (Server : es) Response -> Eff es Response
- runServerWai :: IOE :> es => (ByteString -> ByteString) -> Request -> (Response -> IO ResponseReceived) -> Eff (Server : es) a -> Eff es (Maybe ResponseReceived)
- basicDocument :: Text -> ByteString -> ByteString
- routeRequest :: (Hyperbole :> es, Route route) => (route -> Eff es Response) -> Eff es Response
Documentation
waiApp :: (ByteString -> ByteString) -> Eff '[Hyperbole, Server, IOE] Response -> Application Source #
websocketsOr :: ConnectionOptions -> ServerApp -> Application -> Application #
Upgrade a websockets
ServerApp
to a wai
Application
. Uses
the given backup Application
to handle Request
s that are not
WebSocket requests.
websocketsOr opts ws_app backup_app = \req respond -> casewebsocketsApp
opts ws_app req ofNothing
-> backup_app req send_responseJust
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_connsendTextData
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, IOE] Response -> Application Source #
Turn one or more Page
s into a Wai Application. Respond using both HTTP and WebSockets
main = do run 3000 $ do liveApp (basicDocument "Example") $ do page mainPage
socketApp :: IOE :> es => Eff (Hyperbole : (Server : es)) Response -> PendingConnection -> Eff es () Source #
runServerSockets :: IOE :> es => Connection -> Eff (Server : es) Response -> Eff es Response Source #
runServerWai :: IOE :> es => (ByteString -> ByteString) -> Request -> (Response -> IO ResponseReceived) -> Eff (Server : es) a -> Eff es (Maybe ResponseReceived) 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 instead:
myDocument :: ByteString -> ByteString myDocument content = [i|<html> <head> <title>#{title}</title> <script type="text/javascript">#{scriptEmbed}</script> <style type type="text/css">#{cssResetEmbed}</style> </head> <body>#{content}</body> </html>|]
routeRequest :: (Hyperbole :> es, Route route) => (route -> Eff es Response) -> Eff es Response Source #
Route URL patterns to different pages
import Page.Messages qualified as Messages import Page.Users qualified as Users data AppRoute = Main | Messages | Users UserId deriving (Eq, Generic,Route
) router :: (Hyperbole
:> es) => AppRoute ->Eff
esResponse
router Messages =page
Messages.page router (Users uid) =page
$ Users.page uid router Main = doview
$ doel_
"click a link below to visit a page"route
Messages id "Messages" main = dorun
3000 $ doliveApp
(basicDocument
"Example") (routeRequest router)