Skip to content

Commit 594e0a5

Browse files
Reqwest wasm example (cloudevents#43)
* Fixed cloudevents sdk to compile with wasm Signed-off-by: Francesco Guardiani <[email protected]> * Fixed warning Signed-off-by: Francesco Guardiani <[email protected]> * cargo fmt Signed-off-by: Francesco Guardiani <[email protected]> * Fixed Signed-off-by: Francesco Guardiani <[email protected]> * Added cors to actix-web example Signed-off-by: Francesco Guardiani <[email protected]> * Added reqwest wasm example Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 2586322 commit 594e0a5

File tree

18 files changed

+6465
-28
lines changed

18 files changed

+6465
-28
lines changed

.github/workflows/master.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
target:
1818
- x86_64-unknown-linux-gnu
1919
- x86_64-unknown-linux-musl
20+
- wasm32-unknown-unknown
2021
steps:
2122
- uses: actions/checkout@v2
2223
- run: sudo apt-get update
@@ -44,12 +45,22 @@ jobs:
4445
target: ${{ matrix.target }}
4546
override: true
4647
- uses: actions-rs/cargo@v1
48+
if: matrix.target != 'wasm32-unknown-unknown'
4749
with:
4850
command: build
4951
toolchain: ${{ matrix.toolchain }}
5052
args: --target ${{ matrix.target }} --all
5153
- uses: actions-rs/cargo@v1
54+
if: matrix.target != 'wasm32-unknown-unknown'
5255
with:
5356
command: test
5457
toolchain: ${{ matrix.toolchain }}
5558
args: --target ${{ matrix.target }} --all
59+
60+
# If wasm, then we don't need to compile --all
61+
- uses: actions-rs/cargo@v1
62+
if: matrix.target == 'wasm32-unknown-unknown'
63+
with:
64+
command: build
65+
toolchain: ${{ matrix.toolchain }}
66+
args: --target wasm32-unknown-unknown

Cargo.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ serde_json = "^1.0"
1616
serde-value = "^0.6"
1717
chrono = { version = "^0.4", features = ["serde"] }
1818
delegate = "^0.4"
19-
uuid = { version = "^0.8", features = ["serde", "v4"] }
20-
hostname = "^0.1"
2119
base64 = "^0.12"
2220
url = { version = "^2.1", features = ["serde"] }
2321
snafu = "^0.6"
2422
lazy_static = "1.4.0"
2523

24+
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
25+
hostname = "^0.3"
26+
uuid = { version = "^0.8", features = ["v4"] }
27+
28+
[target.'cfg(target_arch = "wasm32")'.dependencies]
29+
web-sys = { version = "^0.3", features = ["Window", "Location"] }
30+
uuid = { version = "^0.8", features = ["v4", "wasm-bindgen"] }
31+
2632
[dev-dependencies]
2733
rstest = "0.6"
2834
claim = "0.3.1"
@@ -37,5 +43,6 @@ members = [
3743
"cloudevents-sdk-reqwest"
3844
]
3945
exclude = [
40-
"example-projects/actix-web-example"
46+
"example-projects/actix-web-example",
47+
"example-projects/reqwest-wasm-example"
4148
]

cloudevents-sdk-actix-web/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ lazy_static = "1.4.0"
1414
bytes = "^0.5"
1515
futures = "^0.3"
1616
serde_json = "^1.0"
17+
18+
[dev-dependencies]
1719
url = { version = "^2.1", features = ["serde"] }

cloudevents-sdk-reqwest/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ cloudevents-sdk = { path = ".." }
1111
lazy_static = "1.4.0"
1212
bytes = "^0.5"
1313
serde_json = "^1.0"
14-
url = { version = "^2.1", features = ["serde"] }
1514

1615
[dependencies.reqwest]
1716
version = "0.10.4"
@@ -20,4 +19,5 @@ features = ["rustls-tls"]
2019

2120
[dev-dependencies]
2221
mockito = "0.25.1"
23-
tokio = { version = "^0.2", features = ["full"] }
22+
tokio = { version = "^0.2", features = ["full"] }
23+
url = { version = "^2.1" }

example-projects/actix-web-example/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cloudevents-sdk = { path = "../.." }
99
cloudevents-sdk-actix-web = { path = "../../cloudevents-sdk-actix-web" }
1010
actix-web = "2"
1111
actix-rt = "1"
12+
actix-cors = "^0.2.0"
1213
lazy_static = "1.4.0"
1314
bytes = "^0.5"
1415
futures = "^0.3"

example-projects/actix-web-example/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ async fn main() -> std::io::Result<()> {
3535
HttpServer::new(|| {
3636
App::new()
3737
.wrap(actix_web::middleware::Logger::default())
38+
.wrap(actix_cors::Cors::new().finish())
3839
.service(post_event)
3940
.service(get_event)
4041
})
41-
.bind("127.0.0.1:8080")?
42+
.bind("127.0.0.1:9000")?
4243
.workers(1)
4344
.run()
4445
.await
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**/node_modules
2+
pkg
3+
*.swp
4+
**/dist
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "reqwest-wasm-example"
3+
version = "0.1.0"
4+
authors = ["Francesco Guardiani <[email protected]>"]
5+
edition = "2018"
6+
7+
# Config mostly pulled from: https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/Cargo.toml
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[dependencies]
13+
reqwest = "0.10.4"
14+
cloudevents-sdk = { path = "../.." }
15+
cloudevents-sdk-reqwest = { path = "../../cloudevents-sdk-reqwest" }
16+
url = { version = "^2.1" }
17+
web-sys = { version = "0.3.39", features = ["Window", "Location"] }
18+
wasm-bindgen-futures = "0.4.12"
19+
wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] }
20+
21+
[workspace]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Example usage of CLoudEvents sdk/Reqwest from WASM
2+
3+
Install the dependencies with:
4+
5+
npm install
6+
7+
Then build the example locally with:
8+
9+
npm run serve
10+
11+
and then visiting http://localhost:8080 in a browser should run the example!
12+
13+
This example is loosely based off of [this example](https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs), an example usage of `fetch` from `wasm-bindgen`, and from [reqwest repo](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_header).
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<html>
2+
<head>
3+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
4+
</head>
5+
<body>
6+
<form class="form-horizontal">
7+
<fieldset>
8+
9+
<!-- Form Name -->
10+
<legend>CloudEvents sender</legend>
11+
12+
<!-- Text input-->
13+
<div class="form-group">
14+
<label class="col-md-4 control-label" for="event_target">Target</label>
15+
<div class="col-md-4">
16+
<input id="event_target" name="event_target" type="text" placeholder="http://localhost:9000" class="form-control input-md" required="">
17+
18+
</div>
19+
</div>
20+
21+
<!-- Text input-->
22+
<div class="form-group">
23+
<label class="col-md-4 control-label" for="event_type">Event Type</label>
24+
<div class="col-md-4">
25+
<input id="event_type" name="event_type" type="text" placeholder="example" class="form-control input-md" required="">
26+
</div>
27+
</div>
28+
29+
<!-- Text input-->
30+
<div class="form-group">
31+
<label class="col-md-4 control-label" for="event_datacontenttype">Event Data Content Type</label>
32+
<div class="col-md-4">
33+
<input id="event_datacontenttype" name="event_datacontenttype" type="text" placeholder="application/json" class="form-control input-md" required="">
34+
</div>
35+
</div>
36+
37+
<!-- Textarea -->
38+
<div class="form-group">
39+
<label class="col-md-4 control-label" for="event_data">Event Data</label>
40+
<div class="col-md-4">
41+
<textarea class="form-control" id="event_data" name="event_data">{"hello":"world"}</textarea>
42+
</div>
43+
</div>
44+
45+
<!-- Button -->
46+
<div class="form-group">
47+
<label class="col-md-4 control-label" for="send">Send Event</label>
48+
<div class="col-md-4">
49+
<button id="send" name="send" class="btn btn-primary" type="button">Send</button>
50+
</div>
51+
</div>
52+
53+
</fieldset>
54+
</form>
55+
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)