purescript-httpurple/docs/Examples/Middleware/Main.purs

101 lines
3.6 KiB
Haskell
Raw Normal View History

2017-10-26 21:19:30 +00:00
module Examples.Middleware.Main where
2017-09-27 19:34:00 +00:00
2022-05-22 11:30:14 +00:00
import Prelude hiding ((/))
2022-05-22 11:30:14 +00:00
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPurple (type (<+>), Request, ResponseM, ServerM, fullPath, header, ok, ok', serve, (<+>))
2022-05-22 11:30:14 +00:00
import Record as Record
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
import Routing.Duplex.Generic.Syntax ((/))
import Type.Prelude (Proxy(..))
data Middleware = Middleware
derive instance Generic Middleware _
middlewareRoute :: RD.RouteDuplex' Middleware
middlewareRoute = RD.root $ RG.sum
{ "Middleware": "middleware" / RG.noArgs
}
data SayHello = SayHello
derive instance Generic SayHello _
sayHelloRoute :: RD.RouteDuplex' SayHello
sayHelloRoute = RD.root $ RG.sum
{ "SayHello": RG.noArgs
}
2017-09-27 19:34:00 +00:00
-- | A middleware that logs at the beginning and end of each request
loggingMiddleware ::
forall route.
2022-05-22 11:30:14 +00:00
(Request route -> ResponseM) ->
Request route ->
ResponseM
2017-09-27 19:34:00 +00:00
loggingMiddleware router request = do
liftEffect $ log $ "Request starting for " <> path
2017-09-27 19:34:00 +00:00
response <- router request
liftEffect $ log $ "Request ending for " <> path
2017-09-27 19:34:00 +00:00
pure response
where
path = fullPath request
2017-09-27 19:34:00 +00:00
-- | A middleware that adds the X-Middleware header to the response, if it
-- | wasn't already in the response
headerMiddleware ::
forall route.
2022-05-22 11:30:14 +00:00
(Request route -> ResponseM) ->
Request route ->
ResponseM
2017-09-27 19:34:00 +00:00
headerMiddleware router request = do
response@{ headers } <- router request
pure $ response { headers = header' <> headers }
2017-09-27 19:34:00 +00:00
where
header' = header "X-Middleware" "middleware"
2017-09-27 19:34:00 +00:00
-- | A middleware that sends the body "Middleware!" instead of running the
-- | router when requesting /middleware
pathMiddleware ::
forall route.
2022-05-22 11:30:14 +00:00
(Request route -> ResponseM) ->
Request (Middleware <+> route) ->
ResponseM
2022-05-22 11:30:14 +00:00
pathMiddleware _ { route: Left Middleware } = ok "Middleware!"
pathMiddleware router request@{ route: Right r } = router $ Record.set (Proxy :: _ "route") r request
2017-09-27 19:34:00 +00:00
-- | Say 'hello' when run, and add a default value to the X-Middleware header
2022-05-22 11:30:14 +00:00
sayHello :: Request SayHello -> ResponseM
sayHello _ = ok' (header "X-Middleware" "router") "hello"
-- | The stack of middlewares to use for the server
2022-05-22 11:30:14 +00:00
middlewareStack :: forall route. (Request route -> ResponseM) -> Request (Either Middleware route) -> ResponseM
middlewareStack = loggingMiddleware <<< headerMiddleware <<< pathMiddleware
2017-09-27 19:34:00 +00:00
-- | Boot up the server
main :: ServerM
main =
serve { port: 8080, onStarted } { route: middlewareRoute <+> sayHelloRoute, router: middlewareStack sayHello }
where
onStarted = do
log " ┌───────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -v localhost:8080 │"
log " │ # => ... │"
log " │ # => ...< X-Middleware: router │"
log " │ # => ... │"
log " │ # => hello │"
log " │ > curl -v localhost:8080/middleware │"
log " │ # => ... │"
log " │ # => ...< X-Middleware: middleware │"
log " │ # => ... │"
log " │ # => Middleware! │"
log " └───────────────────────────────────────┘"