on master help sign in
Merge pull request #30 from BardurArantsson/add-library-interface

Add library interface

commitbd340ff5280984fc20ef55d299d26815603b486e authorGreg Weber <greg@gregweber.info> dateFri 13 Jan 2012, 02:57 (15 years ago) parent5c56adbe1d17c5
4 files changed+53−22
wai-eventsource/src/Main.hs+2−19 wai-eventsource/src/Network/Wai/EventSource.hs+32 wai-eventsource/src/EventStream.hs → wai-eventsource/src/Network/Wai/EventSource/EventStream.hs+1−1 wai-eventsource/wai-eventsource.cabal+18−2
wai-eventsource/src/Main.hs+2−19
@@ -2,41 +2,24 @@
22module Main where
33
44import Control.Monad
5−import Control.Monad.Trans
65import Control.Concurrent (forkIO, threadDelay)
76import Control.Concurrent.Chan
87import Network.Wai.Handler.Warp (run)
98import Network.Wai
109import Network.HTTP.Types (statusOK)
11−import Data.Enumerator (run_, ($$))
12−import Data.Enumerator.List (generateM)
1310import Data.Time.Clock.POSIX
1411import Blaze.ByteString.Builder.Char.Utf8 (fromString)
1512
16−import EventStream
17−
13+import Network.Wai.EventSource
1814
1915app :: Chan ServerEvent -> Application
2016app chan req =
2117 case pathInfo req of
2218 [] -> return $ ResponseFile statusOK [("Content-Type", "text/html")] "static/index.html" Nothing
23− ["es"] -> do
24− chan' <- liftIO $ dupChan chan
25− return $ res chan'
19+ ["es"] -> eventSourceApp chan req
2620 _ -> error "unexpected pathInfo"
2721
2822
29−res :: Chan ServerEvent -> Response
30−res chan = ResponseEnumerator (resE chan)
31−
32−
33−resE :: Chan ServerEvent -> ResponseEnumerator a
34−resE chan genIter =
35− run_ $ generateM (fmap eventToBuilder $ readChan chan) $$ iter
36− where
37− iter = genIter statusOK [("Content-Type", "text/event-stream")]
38−
39−
4023source :: Chan ServerEvent -> IO ()
4124source chan = forever $ do
4225 threadDelay 1000000
wai-eventsource/src/Network/Wai/EventSource.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.EventSource (
3+ ServerEvent(..),
4+ eventSourceApp
5+ ) where
6+
7+import Blaze.ByteString.Builder (Builder)
8+import Control.Concurrent.Chan (Chan, dupChan, readChan)
9+import Control.Monad.Trans (liftIO)
10+import qualified Data.Conduit as C
11+import Network.HTTP.Types (statusOK)
12+import Network.Wai (Application, Response(..))
13+
14+import Network.Wai.EventSource.EventStream
15+
16+-- | Make a new WAI EventSource application reading events from
17+-- the given channel.
18+eventSourceApp :: Chan ServerEvent -> Application
19+eventSourceApp chan _ = do
20+ chan' <- liftIO $ dupChan chan
21+ return $ res chan'
22+
23+res :: Chan ServerEvent -> Response
24+res chan = ResponseSource statusOK [("Content-Type", "text/event-stream")] $ resE chan
25+
26+resE :: Chan ServerEvent -> C.Source IO Builder
27+resE chan =
28+ C.sourceIO ign (const ign) (const $ fmap (f . eventToBuilder) $ readChan chan)
29+ where
30+ ign = return ()
31+ f Nothing = C.Closed
32+ f (Just b) = C.Open b
wai-eventsource/src/EventStream.hs → wai-eventsource/src/Network/Wai/EventSource/EventStream.hsrenamed+1−1
@@ -5,7 +5,7 @@
55 A WAI adapter to the HTML5 Server-Sent Events API. Push-mode and
66 pull-mode interfaces are both available.
77-}
8−module EventStream (
8+module Network.Wai.EventSource.EventStream (
99 ServerEvent(..),
1010 eventToBuilder
1111 ) where
wai-eventsource/wai-eventsource.cabal+18−2
@@ -12,7 +12,23 @@ Cabal-version: >=1.2
1212
1313flag example
1414
15−-- TODO: Library interface
15+
16+Library
17+ Build-depends:
18+ base >= 4.3 && < 5
19+ , bytestring >= 0.9.1.4 && < 0.10
20+ , blaze-builder >= 0.3 && < 0.4
21+ , conduit >= 0.0 && < 0.1
22+ , MonadCatchIO-transformers >= 0.2.1 && < 0.3
23+ , mtl >= 2 && < 3
24+ , http-types >= 0.6 && < 0.7
25+ , wai >= 0.4
26+ , warp >= 0.4
27+
28+ ghc-options: -Wall
29+ hs-source-dirs: src
30+ exposed-modules: Network.Wai.EventSource
31+ other-modules: Network.Wai.EventSource.EventStream
1632
1733Executable wai-eventsource
1834 if flag(example)
@@ -27,10 +43,10 @@ Executable wai-eventsource
2743 base >= 4.3 && < 5
2844 , bytestring >= 0.9.1.4 && < 0.10
2945 , blaze-builder >= 0.3 && < 0.4
46+ , conduit >= 0.0 && < 0.1
3047 , MonadCatchIO-transformers >= 0.2.1 && < 0.3
3148 , mtl >= 2 && < 3
3249 , http-types >= 0.6 && < 0.7
33− , enumerator >= 0.4.8 && < 0.5
3450 , time >= 1.2 && < 1.4
3551 , wai >= 0.4
3652 , warp >= 0.4