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:
@@ -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
|
||||
```
|
||||
7
docs/Examples/BinaryRequest/Main.js
Normal file
7
docs/Examples/BinaryRequest/Main.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
exports.sha256sum = function(buffer) {
|
||||
return crypto.createHash('sha256').update(buffer).digest('hex');
|
||||
}
|
||||
24
docs/Examples/BinaryRequest/Main.purs
Normal file
24
docs/Examples/BinaryRequest/Main.purs
Normal 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 $ " └─────────────────────────────────────────────────────────┘"
|
||||
10
docs/Examples/BinaryRequest/Readme.md
Normal file
10
docs/Examples/BinaryRequest/Readme.md
Normal 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
|
||||
```
|
||||
@@ -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"
|
||||
10
docs/Examples/BinaryResponse/Readme.md
Normal file
10
docs/Examples/BinaryResponse/Readme.md
Normal 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
|
||||
```
|
||||
|
Before Width: | Height: | Size: 453 B After Width: | Height: | Size: 453 B |
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user