Improve and simplify APIs (#68)
This commit is contained in:
@@ -3,8 +3,9 @@ module Headers where
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Data.StrMap as StrMap
|
||||
import Data.Tuple as Tuple
|
||||
import HTTPure as HTTPure
|
||||
import HTTPure ((!!))
|
||||
|
||||
-- | Serve the example server on this port
|
||||
port :: Int
|
||||
@@ -14,16 +15,13 @@ port = 8082
|
||||
portS :: String
|
||||
portS = show port
|
||||
|
||||
-- | Read X-Input back to the body and set the X-Example header
|
||||
sayHello :: HTTPure.Headers -> HTTPure.Response
|
||||
sayHello = HTTPure.OK responseHeaders <<< flip HTTPure.lookup "X-Input"
|
||||
where
|
||||
responseHeaders = StrMap.singleton "X-Example" "hello world!"
|
||||
-- | The headers that will be included in every response.
|
||||
responseHeaders :: HTTPure.Headers
|
||||
responseHeaders = HTTPure.headers [Tuple.Tuple "X-Example" "hello world!"]
|
||||
|
||||
-- | Route to the correct handler
|
||||
router :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
router (HTTPure.Get headers _) = pure $ sayHello headers
|
||||
router _ = pure $ HTTPure.NotFound StrMap.empty
|
||||
router { headers } = HTTPure.ok' responseHeaders $ headers !! "X-Input"
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.ServerM (console :: Console.CONSOLE | e)
|
||||
|
||||
@@ -3,7 +3,6 @@ module HelloWorld where
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Data.StrMap as StrMap
|
||||
import HTTPure as HTTPure
|
||||
|
||||
-- | Serve the example server on this port
|
||||
@@ -16,7 +15,7 @@ portS = show port
|
||||
|
||||
-- | Say 'hello world!' when run
|
||||
sayHello :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
sayHello _ = pure $ HTTPure.OK StrMap.empty "hello world!"
|
||||
sayHello _ = HTTPure.ok "hello world!"
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.ServerM (console :: Console.CONSOLE | e)
|
||||
|
||||
@@ -3,7 +3,6 @@ module MultiRoute where
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Data.StrMap as StrMap
|
||||
import HTTPure as HTTPure
|
||||
|
||||
-- | Serve the example server on this port
|
||||
@@ -16,9 +15,9 @@ portS = show port
|
||||
|
||||
-- | Specify the routes
|
||||
router :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
router (HTTPure.Get _ "/hello") = pure $ HTTPure.OK StrMap.empty "hello"
|
||||
router (HTTPure.Get _ "/goodbye") = pure $ HTTPure.OK StrMap.empty "goodbye"
|
||||
router _ = pure $ HTTPure.NotFound StrMap.empty
|
||||
router { path: [ "hello" ] } = HTTPure.ok "hello"
|
||||
router { path: [ "goodbye" ] } = HTTPure.ok "goodbye"
|
||||
router _ = HTTPure.notFound
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.ServerM (console :: Console.CONSOLE | e)
|
||||
|
||||
34
docs/Examples/PathSegments/Main.purs
Normal file
34
docs/Examples/PathSegments/Main.purs
Normal file
@@ -0,0 +1,34 @@
|
||||
module PathSegments where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import HTTPure as HTTPure
|
||||
import HTTPure ((!!))
|
||||
|
||||
-- | Serve the example server on this port
|
||||
port :: Int
|
||||
port = 8086
|
||||
|
||||
-- | Shortcut for `show port`
|
||||
portS :: String
|
||||
portS = show port
|
||||
|
||||
-- | Specify the routes
|
||||
router :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
router { path }
|
||||
| path !! 0 == "segment" = HTTPure.ok $ path !! 1
|
||||
| otherwise = HTTPure.ok $ show path
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.ServerM (console :: Console.CONSOLE | e)
|
||||
main = HTTPure.serve port router do
|
||||
Console.log $ " ┌───────────────────────────────────────────────┐"
|
||||
Console.log $ " │ Server now up on port " <> portS <> " │"
|
||||
Console.log $ " │ │"
|
||||
Console.log $ " │ To test, run: │"
|
||||
Console.log $ " │ > curl localhost:" <> portS <> "/segment/<anything> │"
|
||||
Console.log $ " │ # => <anything> │"
|
||||
Console.log $ " │ > curl localhost:" <> portS <> "/<anything>/<else>/... │"
|
||||
Console.log $ " │ # => [ <anything>, <else>, ... ] │"
|
||||
Console.log $ " └───────────────────────────────────────────────┘"
|
||||
11
docs/Examples/PathSegments/Readme.md
Normal file
11
docs/Examples/PathSegments/Readme.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Path Segments Example
|
||||
|
||||
This is a basic example that demonstrates working with URL segments. It includes
|
||||
code that fetches the whole set of URL segments as an array of strings, and code
|
||||
that routes based on the value of specific segments.
|
||||
|
||||
To run the example server, run:
|
||||
|
||||
```bash
|
||||
make example EXAMPLE=PathSegments
|
||||
```
|
||||
@@ -3,7 +3,6 @@ module Post where
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Data.StrMap as StrMap
|
||||
import HTTPure as HTTPure
|
||||
|
||||
-- | Serve the example server on this port
|
||||
@@ -16,8 +15,8 @@ portS = show port
|
||||
|
||||
-- | Route to the correct handler
|
||||
router :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
router (HTTPure.Post _ _ body) = pure $ HTTPure.OK StrMap.empty body
|
||||
router _ = pure $ HTTPure.NotFound StrMap.empty
|
||||
router { body, method: HTTPure.Post } = HTTPure.ok body
|
||||
router _ = HTTPure.notFound
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.ServerM (console :: Console.CONSOLE | e)
|
||||
|
||||
@@ -3,7 +3,6 @@ module SSL where
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Data.StrMap as StrMap
|
||||
import HTTPure as HTTPure
|
||||
|
||||
-- | Serve the example server on this port
|
||||
@@ -24,7 +23,7 @@ key = "./docs/Examples/SSL/Key.key"
|
||||
|
||||
-- | Say 'hello world!' when run
|
||||
sayHello :: forall e. HTTPure.Request -> HTTPure.ResponseM e
|
||||
sayHello _ = pure $ HTTPure.OK StrMap.empty "hello world!"
|
||||
sayHello _ = HTTPure.ok "hello world!"
|
||||
|
||||
-- | Boot up the server
|
||||
main :: forall e. HTTPure.SecureServerM (console :: Console.CONSOLE | e)
|
||||
|
||||
Reference in New Issue
Block a user