Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 656c036

Browse files
committed
examples/rust: Add a minimal hello world rust example
This is about the smallest it can be. Its Unit application config would look like "applications": { "rust-hello-world": { "type": "wasm", "module": "/path/to/unit-wasm/examples/rust/hello-world/target/wasm32-wasi/debug/rust_hello_world.wasm", "request_handler": "uwr_request_handler", "malloc_handler": "luw_malloc_handler", "free_handler": "luw_free_handler" } } Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
1 parent 15b73fe commit 656c036

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ Create the following Unit config
305305
"action": {
306306
"pass": "applications/rust-upload-reflector"
307307
}
308+
},
309+
{
310+
"match": {
311+
"uri": "/hello-world*"
312+
},
313+
"action": {
314+
"pass": "applications/rust-hello-world"
315+
}
308316
}
309317
],
310318

@@ -344,6 +352,13 @@ Create the following Unit config
344352
"free_handler": "luw_free_handler",
345353
"request_end_handler": "uwr_request_end_handler",
346354
"response_end_handler": "uwr_response_end_handler"
355+
},
356+
"rust-hello-world": {
357+
"type": "wasm",
358+
"module": "/path/to/unit-wasm/examples/rust/hello-world/target/wasm32-wasi/debug/rust_hello_world.wasm",
359+
"request_handler": "uwr_request_handler",
360+
"malloc_handler": "luw_malloc_handler",
361+
"free_handler": "luw_free_handler"
347362
}
348363
}
349364
}

examples/rust/Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include ../../shared.mk
22

33
SDIR = examples/rust
44

5-
examples: rust-echo-request rust-upload-reflector
5+
examples: rust-echo-request rust-upload-reflector hello-world
66

77
rust-echo-request: echo-request/Cargo.toml echo-request/src/lib.rs
88
$(PP_GEN) $(SDIR)/echo-request/target/wasm32-wasi/
@@ -12,6 +12,10 @@ rust-upload-reflector: upload-reflector/Cargo.toml upload-reflector/src/lib.rs
1212
$(PP_GEN) $(SDIR)/upload-reflector/target/wasm32-wasi/
1313
$(v)cd upload-reflector; cargo build --target=wasm32-wasi
1414

15+
hello-world: hello-world/Cargo.toml hello-world/src/lib.rs
16+
$(PP_GEN) $(SDIR)/hello-world/target/wasm32-wasi/
17+
$(v)cd hello-world; cargo build --target=wasm32-wasi
18+
1519
clean:
16-
rm -f echo-request/Cargo.lock upload-reflector/Cargo.lock
17-
rm -rf echo-request/target upload-reflector/target
20+
rm -f */Cargo.lock
21+
rm -rf */target
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rust-hello-world"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
unit-wasm = { path = "../../../src/rust", version = "0.1.2" }
10+
11+
[lib]
12+
crate-type = ["cdylib"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
3+
/*
4+
* Copyright (C) Andrew Clayton
5+
* Copyright (C) Timo Stark
6+
* Copyright (C) F5, Inc.
7+
*/
8+
9+
use unit_wasm::rusty::*;
10+
11+
static mut REQUEST_BUF: *mut u8 = std::ptr::null_mut();
12+
13+
#[no_mangle]
14+
pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
15+
let ctx = &mut UWR_CTX_INITIALIZER();
16+
17+
uwr_init_ctx(ctx, addr, 4096);
18+
uwr_set_req_buf(ctx, unsafe { &mut REQUEST_BUF }, LUW_SRB_ALLOC);
19+
20+
uwr_write_str!(
21+
ctx,
22+
" * Welcome to WebAssembly in Rust on Unit! \
23+
[libunit-wasm ({}.{}.{}/{:#010x})] *\n\n",
24+
LUW_VERSION_MAJOR,
25+
LUW_VERSION_MINOR,
26+
LUW_VERSION_PATCH,
27+
LUW_VERSION_NUMBER,
28+
);
29+
30+
uwr_http_init_headers(ctx, 2, 0);
31+
uwr_http_add_header_content_type(ctx, "text/plain");
32+
uwr_http_add_header_content_len(ctx);
33+
uwr_http_send_headers(ctx);
34+
35+
uwr_http_send_response(ctx);
36+
uwr_http_response_end();
37+
38+
uwr_free(unsafe { REQUEST_BUF });
39+
40+
return 0;
41+
}

unit-wasm-conf.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
"action": {
4444
"pass": "applications/rust-upload-reflector"
4545
}
46+
},
47+
{
48+
"match": {
49+
"uri": "/rust-hello-world*"
50+
},
51+
"action": {
52+
"pass": "applications/rust-hello-world"
53+
}
4654
}
4755
],
4856

@@ -82,6 +90,13 @@
8290
"free_handler": "luw_free_handler",
8391
"request_end_handler": "uwr_request_end_handler",
8492
"response_end_handler": "uwr_response_end_handler"
93+
},
94+
"rust-hello-world": {
95+
"type": "wasm",
96+
"module": "/path/to/unit-wasm/examples/rust/hello-world/target/wasm32-wasi/debug/rust_hello_world.wasm",
97+
"request_handler": "uwr_request_handler",
98+
"malloc_handler": "luw_malloc_handler",
99+
"free_handler": "luw_free_handler"
85100
}
86101
}
87102
}

0 commit comments

Comments
 (0)