This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMain.purs
More file actions
56 lines (47 loc) · 1.74 KB
/
Main.purs
File metadata and controls
56 lines (47 loc) · 1.74 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
50
51
52
53
54
55
56
module Test.Main where
import Prelude
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Foreign.Class (class Decode, class Encode)
import Foreign.Generic (defaultOptions, genericDecode, genericEncode)
import Foreign.Generic.Types (Options)
import QuickServe (Capture(..), GET, JSON(..), POST, RequestBody(..), quickServe)
newtype Message = Message { message :: String }
derive instance genericMessage :: Generic Message _
jsonOpts :: Options
jsonOpts = defaultOptions { unwrapSingleConstructors = true }
instance decodeMessage :: Decode Message where
decode = genericDecode jsonOpts
instance encodeMessage :: Encode Message where
encode = genericEncode jsonOpts
-- | This will serve three endpoints:
-- |
-- | - `/hello`, which returns the plain text string "Hello World!"
-- | - `/echo1`, which receives a JSON message in a POST body
-- | - `/echo2/<arg>`, which receives a plain text message to echo as a path argument
-- |
-- | Each of these can be tested with cURL:
-- |
-- | ```
-- | curl http://localhost:3000/hello
-- | curl http://localhost:3000/echo1 -XPOST -d '{"message": "test"}'
-- | curl http://localhost:3000/echo2/test
-- | ```
main :: Effect Unit
main = do
let opts = { hostname: "localhost", port: 3000, backlog: Nothing }
quickServe opts $
let
echo1 :: RequestBody (JSON Message)
-> POST (JSON Message)
echo1 (RequestBody (JSON (Message { message }))) = do
liftEffect (log message)
pure (JSON (Message { message }))
echo2 :: Capture -> GET String
echo2 (Capture message) = pure message
hello :: GET String
hello = pure "Hello, World!"
in { echo1, echo2, hello }