-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRoutingReaderT.purs
More file actions
49 lines (39 loc) · 1.38 KB
/
RoutingReaderT.purs
File metadata and controls
49 lines (39 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Examples.RoutingReaderT where
import Prelude
import Control.Monad.Indexed ((:*>))
import Control.Monad.Except (ExceptT)
import Control.Monad.Reader (ReaderT, ask, runReaderT)
import Data.Maybe (fromMaybe)
import Effect (Effect)
import Effect.Aff (Aff)
import Hyper.Node.Server (defaultOptionsWithLogging, runServer')
import Hyper.Response (closeHeaders, respond, writeStatus)
import Hyper.Trout.Router (RoutingError, router)
import Text.Smolder.HTML (p)
import Text.Smolder.Markup (text)
import Type.Proxy (Proxy(..))
import Type.Trout (type (:=), Resource)
import Type.Trout.ContentType.HTML (class EncodeHTML, HTML)
import Type.Trout.Method (Get)
data Greeting = Greeting String
type Site = "greeting" := Resource (Get Greeting HTML)
instance encodeHTMLGreeting :: EncodeHTML Greeting where
encodeHTML (Greeting g) = p (text g)
runAppM ∷ ∀ a. String -> ReaderT String Aff a → Aff a
runAppM = flip runReaderT
site :: Proxy Site
site = Proxy
greetingResource
:: forall m
. Monad m
=> {"GET" :: ExceptT RoutingError (ReaderT String m) Greeting}
greetingResource =
{"GET": Greeting <$> ask}
main :: Effect Unit
main =
let app = router site {"greeting": greetingResource} onRoutingError
onRoutingError status msg =
writeStatus status
:*> closeHeaders
:*> respond (fromMaybe "" msg)
in runServer' defaultOptionsWithLogging {} (runAppM "Hello") app