2019-09-30 03:57:15 +00:00
|
|
|
module Examples.CustomStack.Main where
|
|
|
|
|
|
|
|
import Prelude
|
2022-05-04 21:02:29 +00:00
|
|
|
|
2019-09-30 03:57:15 +00:00
|
|
|
import Control.Monad.Reader (class MonadAsk, ReaderT, asks, runReaderT)
|
2022-05-22 11:30:14 +00:00
|
|
|
import Data.Generic.Rep (class Generic)
|
|
|
|
import Data.Maybe (Maybe(..))
|
2019-09-30 03:57:15 +00:00
|
|
|
import Effect.Aff (Aff)
|
|
|
|
import Effect.Aff.Class (class MonadAff)
|
2021-11-19 06:16:35 +00:00
|
|
|
import Effect.Console (log)
|
2022-05-22 11:47:58 +00:00
|
|
|
import HTTPurple (Request, Response, ResponseM, ServerM, ok, serve)
|
2022-05-22 11:30:14 +00:00
|
|
|
import Routing.Duplex as RD
|
|
|
|
import Routing.Duplex.Generic as RG
|
|
|
|
|
|
|
|
data Route = SayHello
|
|
|
|
|
|
|
|
derive instance Generic Route _
|
|
|
|
|
|
|
|
route :: RD.RouteDuplex' Route
|
|
|
|
route = RD.root $ RG.sum
|
|
|
|
{ "SayHello": RG.noArgs
|
|
|
|
}
|
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 ::
|
2022-05-22 11:30:14 +00:00
|
|
|
forall route.
|
|
|
|
(Request route -> ReaderT Env Aff Response) ->
|
|
|
|
Request route ->
|
2021-11-19 06:16:35 +00:00
|
|
|
ResponseM
|
2019-09-30 03:57:15 +00:00
|
|
|
readerMiddleware router request = do
|
|
|
|
runReaderT (router request) { name: "joe" }
|
|
|
|
|
|
|
|
-- | Say 'hello, joe' when run
|
2022-05-22 11:30:14 +00:00
|
|
|
sayHello :: forall m. MonadAff m => MonadAsk Env m => Request Route -> 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 =
|
2022-08-24 17:59:06 +00:00
|
|
|
serve { hostname: "localhost", port: 8080, onStarted } { route, router: readerMiddleware sayHello }
|
2022-05-22 16:36:08 +00:00
|
|
|
where
|
|
|
|
onStarted = do
|
2021-11-19 06:16:35 +00:00
|
|
|
log " ┌───────────────────────────────────────┐"
|
|
|
|
log " │ Server now up on port 8080 │"
|
|
|
|
log " │ │"
|
|
|
|
log " │ To test, run: │"
|
|
|
|
log " │ > curl -v localhost:8080 │"
|
|
|
|
log " │ # => hello, joe │"
|
|
|
|
log " └───────────────────────────────────────┘"
|