Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:

- run: ./roc_nightly/roc version

- run: sudo apt install -y expect ncat
# expect for testing, ncat as test server for tests/tcp.roc
- run: sudo apt install -y expect ncat ripgrep
# expect for testing, ncat as test server for tests/tcp.roc, ripgrep for ci/check_all_exposed_funs_tested.roc

- name: print expect version
run: expect -v
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ examples/command
examples/sqlite
examples/sqlite3
examples/hello-web
examples/file-upload-form
examples/form-file-upload

# for file-upload-form test
# for form-file-upload test
curl_file_output.txt
# for form-url-encoded test
curl_form_output.txt

.DS_Store

Expand Down
2 changes: 2 additions & 0 deletions ci/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ for roc_file in $EXAMPLES_DIR*.roc; do
fi
done

$ROC ci/check_all_exposed_funs_tested.roc

# roc build
architecture=$(uname -m)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set timeout 7

spawn $env(EXAMPLES_DIR)file-upload-form
spawn $env(EXAMPLES_DIR)form-file-upload

expect "Listening on <http://127.0.0.1:8000>\r\n" {

Expand Down
8 changes: 8 additions & 0 deletions ci/expect_scripts/form-url-encoded-curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

curl 'http://localhost:8000/' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'name=Test User' \
--data-urlencode 'email=test@example.com' \
--data-urlencode 'message=This is a test message' \
> curl_form_output.txt 2>&1
27 changes: 27 additions & 0 deletions ci/expect_scripts/form-url-encoded.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/expect

# uncomment line below for debugging
# exp_internal 1

set timeout 7

spawn $env(EXAMPLES_DIR)form-url-encoded

expect "Listening on <http://127.0.0.1:8000>\r\n" {

set script_dir [file dirname [info script]]

exec bash $script_dir/form-url-encoded-curl.sh

set curlOutput [exec cat curl_form_output.txt]

if { [string match "*Form Data Received*" $curlOutput] && [string match "*Test+User*" $curlOutput] } {
exit 0
} else {
puts "Error: curl output was different than expected: $curlOutput"
exit 1
}
}

puts stderr "\nError: output was different than expected."
exit 1
99 changes: 0 additions & 99 deletions examples/file-upload-form.roc

This file was deleted.

109 changes: 109 additions & 0 deletions examples/form-file-upload.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
app [Model, init!, respond!] {
pf: platform "../platform/main.roc",
utils: "https://github.com/lukewilliamboswell/roc-utils/releases/download/0.3.0/w1rWVzjiBSrvc1VHPNX10o0bHI7rmBdT36hFQ0f5R_w.tar.br",
}

import utils.Base64
import pf.Http exposing [Request, Response]
import pf.MultipartFormData

# To run this example: check the README.md in this folder

# Demonstrates how to upload a (image) file.

image_upload_form : Result Response [ServerErr Str]_
image_upload_form =
Ok(
{
status: 200,
headers: [
{ name: "Content-Type", value: "text/html" },
],
body: Str.to_utf8(
"""
<!DOCTYPE html>
<html>
<head>
<title>Image Upload Form</title>
</head>
<body>

<h2>Upload an Image</h2>

<form action="/" method="post" enctype="multipart/form-data">
<label for="fileToUpload">Select image to upload:</label><br><br>
<input type="file" name="fileToUpload" id="fileToUpload" accept="image/*.png"><br><br>
<input type="submit" value="Upload .png Image" name="submit">
</form>

</body>
</html>
""",
),
},
)

display_uploaded_image! : Request => Result Response [ServerErr Str]_
display_uploaded_image! = |req|
page = |src|
Str.to_utf8(
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Embedded Image</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.image-container {
height: 200px;
background-image: url('data:image/png;base64,${src}');
background-repeat: no-repeat;
background-size: contain; /*scales the image to fit within the container while maintaining its aspect ratio*/
background-position: center;
}
</style>
</head>
<body>
<h1>You uploaded:</h1>
<div class="image-container"></div>
</body>
</html>
""",
)

maybe_image =
{ headers: req.headers, body: req.body }
|> MultipartFormData.parse_multipart_form_data
|> Result.try(List.first)
|> Result.map_ok(.data)
|> Result.map_ok(Base64.encode)

when maybe_image is
Ok(img) ->
Ok(
{
status: 200,
headers: [
{ name: "Content-Type", value: "text/html" },
],
body: page(img),
},
)

Err(err) -> Ok({ status: 500, headers: [], body: Str.to_utf8(Inspect.to_str(err)) })

respond! : Request, Model => Result Response [ServerErr Str]_
respond! = |req, _|
if req.method == GET then
image_upload_form
else if req.method == POST then
display_uploaded_image!(req)
else
Ok({ status: 500, headers: [], body: [] })

# Model is produced by `init`.
Model : {}

init! : {} => Result Model []
init! = |{}| Ok({})
Loading