Add support for non string requests (#184)

* First version of supporting non-string requests

* Clean up

* Minor cleanup

* Simplify to directly export the stream

* Add nl

* Clean up & add more testing

Co-authored-by: sigma-andex <sigma.andex@pm.me>
This commit is contained in:
Connor Prussin
2021-11-15 20:02:36 -08:00
committed by GitHub
parent beb4621f4b
commit 6e886b91ac
15 changed files with 179 additions and 62 deletions

View File

@@ -0,0 +1,7 @@
'use strict';
const crypto = require('crypto');
exports.sha256sum = function(buffer) {
return crypto.createHash('sha256').update(buffer).digest('hex');
}

View File

@@ -0,0 +1,24 @@
module Examples.BinaryRequest.Main where
import Prelude
import Effect.Console as Console
import Node.Buffer (Buffer)
import HTTPure as HTTPure
foreign import sha256sum :: Buffer -> String
-- | Respond with file's sha256sum
router :: HTTPure.Request -> HTTPure.ResponseM
router { body } = HTTPure.toBuffer body >>= sha256sum >>> HTTPure.ok
-- | Boot up the server
main :: HTTPure.ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌─────────────────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -XPOST --data-binary @circle.png localhost:8080 │"
Console.log $ " │ # => d5e776724dd5... │"
Console.log $ " └─────────────────────────────────────────────────────────┘"

View File

@@ -0,0 +1,10 @@
# Binary Request Example
This is a basic example of sending binary request data. It will read in the
binary file and send back the file's sha256 checksum.
To run the server, run:
```bash
make example EXAMPLE=BinaryRequest
```