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

@@ -1,10 +0,0 @@
# Binary Example
This is a basic example of sending binary data. It serves an image file as
binary data on any URL.
To run the server, run:
```bash
make example EXAMPLE=Binary
```

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
```

View File

@@ -1,4 +1,4 @@
module Examples.Binary.Main where
module Examples.BinaryResponse.Main where
import Prelude
import Effect.Console as Console
@@ -7,7 +7,7 @@ import HTTPure as HTTPure
-- | The path to the file containing the response to send
filePath :: String
filePath = "./docs/Examples/Binary/circle.png"
filePath = "./docs/Examples/BinaryResponse/circle.png"
responseHeaders :: HTTPure.Headers
responseHeaders = HTTPure.header "Content-Type" "image/png"

View File

@@ -0,0 +1,10 @@
# Binary Response Example
This is a basic example of sending binary response data. It serves an image
file as binary data on any URL.
To run the server, run:
```bash
make example EXAMPLE=BinaryResponse
```

View File

Before

Width:  |  Height:  |  Size: 453 B

After

Width:  |  Height:  |  Size: 453 B

View File

@@ -6,7 +6,7 @@ import HTTPure as HTTPure
-- | Route to the correct handler
router :: HTTPure.Request -> HTTPure.ResponseM
router { body, method: HTTPure.Post } = HTTPure.ok body
router { body, method: HTTPure.Post } = HTTPure.toString body >>= HTTPure.ok
router _ = HTTPure.notFound
-- | Boot up the server