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-04 21:02:29 +00:00
|
|
|
|
2022-05-22 11:30:14 +00:00
|
|
|
import Data.Either (Either(..))
|
|
|
|
import Data.Generic.Rep (class Generic)
|
|
|
|
import Data.Maybe (Maybe(..))
|
2021-11-19 06:16:35 +00:00
|
|
|
import Effect.Class (liftEffect)
|
|
|
|
import Effect.Console (log)
|
2022-05-22 11:47:58 +00:00
|
|
|
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
|
2022-05-22 16:36:08 +00:00
|
|
|
loggingMiddleware ::
|
|
|
|
forall route.
|
2022-05-22 11:30:14 +00:00
|
|
|
(Request route -> ResponseM) ->
|
|
|
|
Request route ->
|
2021-11-19 06:16:35 +00:00
|
|
|
ResponseM
|
2017-09-27 19:34:00 +00:00
|
|
|
loggingMiddleware router request = do
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect $ log $ "Request starting for " <> path
|
2017-09-27 19:34:00 +00:00
|
|
|
response <- router request
|
2021-11-19 06:16:35 +00:00
|
|
|
liftEffect $ log $ "Request ending for " <> path
|
2017-09-27 19:34:00 +00:00
|
|
|
pure response
|
2017-09-29 16:52:21 +00:00
|
|
|
where
|
2021-11-19 06:16:35 +00:00
|
|
|
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
|
2022-05-22 16:36:08 +00:00
|
|
|
headerMiddleware ::
|
|
|
|
forall route.
|
2022-05-22 11:30:14 +00:00
|
|
|
(Request route -> ResponseM) ->
|
|
|
|
Request route ->
|
2021-11-19 06:16:35 +00:00
|
|
|
ResponseM
|
2017-09-27 19:34:00 +00:00
|
|
|
headerMiddleware router request = do
|
2018-08-20 02:50:07 +00:00
|
|
|
response@{ headers } <- router request
|
2021-11-19 06:16:35 +00:00
|
|
|
pure $ response { headers = header' <> headers }
|
2017-09-27 19:34:00 +00:00
|
|
|
where
|
2021-11-19 06:16:35 +00:00
|
|
|
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
|
2022-05-22 16:36:08 +00:00
|
|
|
pathMiddleware ::
|
|
|
|
forall route.
|
2022-05-22 11:30:14 +00:00
|
|
|
(Request route -> ResponseM) ->
|
2022-05-22 16:36:08 +00:00
|
|
|
Request (Middleware <+> route) ->
|
2021-11-19 06:16:35 +00:00
|
|
|
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
|
2021-11-19 06:16:35 +00:00
|
|
|
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
|
2021-11-19 06:16:35 +00:00
|
|
|
middlewareStack = loggingMiddleware <<< headerMiddleware <<< pathMiddleware
|
2017-09-27 19:34:00 +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-05-22 16:36:08 +00:00
|
|
|
serve { port: 8080, onStarted } { route: middlewareRoute <+> sayHelloRoute, router: middlewareStack sayHello }
|
|
|
|
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 " │ # => ... │"
|
|
|
|
log " │ # => ...< X-Middleware: router │"
|
|
|
|
log " │ # => ... │"
|
|
|
|
log " │ # => hello │"
|
|
|
|
log " │ > curl -v localhost:8080/middleware │"
|
|
|
|
log " │ # => ... │"
|
|
|
|
log " │ # => ...< X-Middleware: middleware │"
|
|
|
|
log " │ # => ... │"
|
|
|
|
log " │ # => Middleware! │"
|
|
|
|
log " └───────────────────────────────────────┘"
|