purescript-httpurple/docs/Examples/Image/Main.purs
Petri Lehtinen 3e94aa6f9d Support binary response body (#99)
* Support binary response body

Fixes #98

* Address PR comments

- Expose data constructors for Body and use them for construction and
  pattern matching instead of various helpers

- Add an example and integration test for binary response

- Adjust the middleware example to be a bit nicer
2018-08-19 19:50:07 -07:00

37 lines
1.3 KiB
Haskell

module Examples.Image.Main where
import Prelude
import Effect.Console as Console
import Node.FS.Aff as FS
import HTTPure as HTTPure
-- | Serve the example server on this port
port :: Int
port = 8090
-- | Shortcut for `show port`
portS :: String
portS = show port
-- | The path to the file containing the response to send
filePath :: String
filePath = "./docs/Examples/Image/circle.png"
-- | Respond with image data when run
image :: HTTPure.Request -> HTTPure.ResponseM
image _ =
FS.readFile filePath >>= HTTPure.binaryResponse' 200 headers
where
headers = HTTPure.header "Content-Type" "image/png"
-- | Boot up the server
main :: HTTPure.ServerM
main = HTTPure.serve port image do
Console.log $ " ┌────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port " <> portS <> ""
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -o circle.png localhost:" <> portS <> ""
Console.log $ " └────────────────────────────────────────────┘"