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)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Effect.Console (log)
|
|
|
|
import HTTPure (Request, Response, ResponseM, ServerM, serve, ok)
|
2019-09-30 03:57:15 +00:00
|
|
|
|
|
|
|
-- | A type to hold the environment for our ReaderT
|
2021-11-06 19:37:31 +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 ::
|
2021-11-19 06:16:35 +00:00
|
|
|
(Request -> ReaderT Env Aff Response) ->
|
|
|
|
Request ->
|
|
|
|
ResponseM
|
2019-09-30 03:57:15 +00:00
|
|
|
readerMiddleware router request = do
|
|
|
|
runReaderT (router request) { name: "joe" }
|
|
|
|
|
|
|
|
-- | Say 'hello, joe' when run
|
2021-11-19 06:16:35 +00:00
|
|
|
sayHello :: forall m. MonadAff m => MonadAsk Env m => Request -> m Response
|
2019-09-30 03:57:15 +00:00
|
|
|
sayHello _ = do
|
|
|
|
name <- asks _.name
|
2021-11-19 06:16:35 +00:00
|
|
|
ok $ "hello, " <> name
|
2019-09-30 03:57:15 +00:00
|
|
|
|
|
|
|
-- | Boot up the server
|
2021-11-19 06:16:35 +00:00
|
|
|
main :: ServerM
|
2021-03-22 19:02:36 +00:00
|
|
|
main =
|
2021-11-19 06:16:35 +00:00
|
|
|
serve 8080 (readerMiddleware sayHello) do
|
|
|
|
log " ┌───────────────────────────────────────┐"
|
|
|
|
log " │ Server now up on port 8080 │"
|
|
|
|
log " │ │"
|
|
|
|
log " │ To test, run: │"
|
|
|
|
log " │ > curl -v localhost:8080 │"
|
|
|
|
log " │ # => hello, joe │"
|
|
|
|
log " └───────────────────────────────────────┘"
|