mirror of
https://github.com/monadicsystems/okapi.git
synced 2024-11-27 13:59:13 +03:00
47 lines
1.1 KiB
Haskell
47 lines
1.1 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
module Main where
|
|
|
|
import Control.Applicative ((<|>))
|
|
import Control.Concurrent (killThread, threadDelay)
|
|
import Control.Monad (forever)
|
|
import Data.ByteString.Lazy.Char8 (pack)
|
|
import Data.Function ((&))
|
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
|
import Okapi
|
|
import SlaveThread (fork)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
eventSource <- newEventSource
|
|
threadID <- fork $
|
|
forever $ do
|
|
threadDelay 1000000
|
|
time <- getPOSIXTime
|
|
sendEvent eventSource $ Event Nothing Nothing $ pack $ show time
|
|
run id (api eventSource)
|
|
killThread threadID
|
|
|
|
type Okapi a = OkapiT IO a
|
|
|
|
api :: EventSource -> Okapi Response
|
|
api eventSource = do
|
|
methodGET
|
|
index <|> sse eventSource
|
|
where
|
|
index :: Okapi Response
|
|
index = do
|
|
pathEnd <|> pathParam @String `equals` ""
|
|
ok
|
|
& setHeader ("Content-Type", "text/html")
|
|
& setBodyFile "examples/sse/sse.html"
|
|
& return
|
|
|
|
sse :: EventSource -> Okapi Response
|
|
sse eventSource = do
|
|
pathParam @String `equals` "events"
|
|
ok
|
|
& setBodyEventSource eventSource
|
|
& return
|