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

39 lines
1.5 KiB
Haskell
Raw Normal View History

module Examples.BinaryRequest.Main where
import Prelude
2022-05-22 11:30:14 +00:00
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, ok, serve, toBuffer)
import Node.Buffer (Buffer)
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
}
foreign import sha256sum :: Buffer -> String
-- | Respond with file's sha256sum
2022-05-22 11:30:14 +00:00
router :: Request Route -> ResponseM
router { body } = toBuffer body >>= sha256sum >>> ok
-- | Boot up the server
main :: ServerM
main =
2022-05-22 11:30:14 +00:00
serve 8080 { route, router, notFoundHandler: Nothing } do
log " ┌─────────────────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -XPOST --data-binary @circle.png localhost:8080 │"
log " │ # => d5e776724dd5... │"
log " └─────────────────────────────────────────────────────────┘"