2019-09-30 03:57:15 +00:00
|
|
|
module Examples.CustomStack.Main where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
import Control.Monad.Reader (class MonadAsk, ReaderT, asks, runReaderT)
|
|
|
|
import Effect.Aff (Aff)
|
|
|
|
import Effect.Aff.Class (class MonadAff)
|
|
|
|
import Effect.Console as Console
|
|
|
|
import HTTPure as HTTPure
|
|
|
|
|
|
|
|
-- | A type to hold the environment for our ReaderT
|
2021-03-22 19:02:36 +00:00
|
|
|
type Env
|
|
|
|
= { name :: String
|
|
|
|
}
|
2019-09-30 03:57:15 +00:00
|
|
|
|
|
|
|
-- | A middleware that introduces a ReaderT
|
2021-03-22 19:02:36 +00:00
|
|
|
readerMiddleware ::
|
|
|
|
(HTTPure.Request -> ReaderT Env Aff HTTPure.Response) ->
|
|
|
|
HTTPure.Request ->
|
|
|
|
HTTPure.ResponseM
|
2019-09-30 03:57:15 +00:00
|
|
|
readerMiddleware router request = do
|
|
|
|
runReaderT (router request) { name: "joe" }
|
|
|
|
|
|
|
|
-- | Say 'hello, joe' when run
|
|
|
|
sayHello :: forall m. MonadAff m => MonadAsk Env m => HTTPure.Request -> m HTTPure.Response
|
|
|
|
sayHello _ = do
|
|
|
|
name <- asks _.name
|
|
|
|
HTTPure.ok $ "hello, " <> name
|
|
|
|
|
|
|
|
-- | Boot up the server
|
|
|
|
main :: HTTPure.ServerM
|
2021-03-22 19:02:36 +00:00
|
|
|
main =
|
|
|
|
HTTPure.serve 8080 (readerMiddleware sayHello) do
|
|
|
|
Console.log $ " ┌───────────────────────────────────────┐"
|
|
|
|
Console.log $ " │ Server now up on port 8080 │"
|
|
|
|
Console.log $ " │ │"
|
|
|
|
Console.log $ " │ To test, run: │"
|
|
|
|
Console.log $ " │ > curl -v localhost:8080 │"
|
|
|
|
Console.log $ " │ # => hello, joe │"
|
|
|
|
Console.log $ " └───────────────────────────────────────┘"
|