-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathReadBody.purs
57 lines (53 loc) · 1.6 KB
/
ReadBody.purs
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
50
51
52
53
54
55
56
57
module ReadBody where
import Prelude
import Control.Monad.Indexed ((:>>=), (:*>))
import Effect (Effect)
import Data.Either (Either(..))
import Data.HTTP.Method (Method(..))
import Hyper.Conn (Conn)
import Hyper.Middleware (Middleware)
import Hyper.Node.Server (defaultOptionsWithLogging, runServer)
import Hyper.Request (class ReadableBody, getRequestData, readBody)
import Hyper.Response (class Response, class ResponseWritable, ResponseEnded, StatusLineOpen, closeHeaders, respond, writeStatus)
import Hyper.Status (statusBadRequest, statusMethodNotAllowed)
onPost
:: forall m b req res c
. Monad m
=> ReadableBody req m String
=> Response res m b
=> ResponseWritable b m String
=> Middleware
m
(Conn req (res StatusLineOpen) c)
(Conn req (res ResponseEnded) c)
Unit
-- start snippet onPost
onPost =
readBody :>>=
case _ of
"" ->
writeStatus statusBadRequest
:*> closeHeaders
:*> respond "... anyone there?"
msg ->
writeStatus statusBadRequest
:*> closeHeaders
:*> respond ("You said: " <> msg)
-- end snippet onPost
main :: Effect Unit
main =
let
router =
_.method <$> getRequestData :>>=
case _ of
Left POST -> onPost
Left method ->
writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Method not supported: " <> show method)
Right customMethod ->
writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Custom method not supported: " <> show customMethod)
-- Let's run it.
in runServer defaultOptionsWithLogging {} router