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

49 lines
1.9 KiB
Haskell
Raw Normal View History

2017-10-26 21:19:30 +00:00
module Examples.QueryParameters.Main where
import Prelude
2022-05-22 11:30:14 +00:00
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect (Effect)
2022-05-22 11:30:14 +00:00
import Effect.Class.Console (log)
import HTTPurple (Request, ResponseM, ServerM, notFound, ok, serve)
2022-05-22 11:30:14 +00:00
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as G
data Route = Route { foo :: Boolean, bar :: Maybe String, baz :: Maybe String }
derive instance Generic Route _
route :: RouteDuplex' Route
route = RD.root $ G.sum
{ "Route": RD.params { foo: RD.flag <<< RD.string, bar: RD.optional <<< RD.string, baz: RD.optional <<< RD.string }
}
-- | Specify the routes
2022-05-22 11:30:14 +00:00
router :: Request Route -> ResponseM
router { route: (Route { foo: true }) } = ok "foo"
router { route: (Route { bar: Just "test" }) } = ok "bar"
router { route: (Route { bar: Just _ }) } = ok ""
router { route: Route { baz: Just baz } } = ok $ baz
router _ = notFound
-- | Boot up the server
main :: ServerM
main =
2022-08-24 17:59:06 +00:00
serve { hostname: "localhost", port: 8080, onStarted } { route, router }
where
onStarted :: Effect Unit
onStarted = do
log " ┌───────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080?foo │"
log " │ # => foo │"
log " │ > curl localhost:8080?bar=test │"
log " │ # => bar │"
log " │ > curl localhost:8080?baz=<anything> │"
log " │ # => <anything> │"
log " └───────────────────────────────────────┘"