Skip to content

Commit e66a061

Browse files
committed
update README
1 parent 081a67e commit e66a061

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ jobs:
1919
- name: Build
2020
run: make plugin
2121
- name: Release
22-
uses: actions/upload-artifact@v4
22+
uses: softprops/action-gh-release@v2
2323
with:
24-
name: plugin
25-
path: target/plugin/**
26-
include-hidden-files: true
24+
files: |
25+
target/plugin/plugin.wasm
26+
target/plugin/.traefik.yml
27+
target/plugin/LICENSE
2728
2829

.traefik.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ displayName: Demo Plugin Wasm Rust
22
runtime: wasm
33
type: middleware
44

5-
summary: '[Demo WASM]'
5+
summary: 'a starting point for a plugin written in rust'
66

77
testData:
88
rules:

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,80 @@
11
# plugin-wasm-rust-demo
22

3-
Demo Traefik plugin written in rust
3+
Demo Traefik plugin written in rust. Inspired by https://github.com/juliens/traefik-plugin-rust-demo
44

5-
https://http-wasm.io/http-handler-abi/
5+
## Examples
6+
set header and forward to the next middleware
7+
```rust
8+
fn handle_request(&self, request: Request, _response: Response) -> (bool, i32) {
9+
request.header().add("X-Foo".as_bytes(), "Bar".as_bytes());
10+
(true, 0)
11+
}
12+
```
13+
14+
match uri and respond with error
15+
```rust
16+
fn handle_request(&self, request: Request, response: Response) -> (bool, i32) {
17+
match request.uri() {
18+
Some(s) if s.starts_with("/.config".as_bytes()) => {
19+
response.set_status_code(403);
20+
return (false, 0);
21+
}
22+
_ => {}
23+
}
24+
(true, 0)
25+
}
26+
```
27+
28+
29+
## Building
30+
31+
if not already installed, add the wasm-target
32+
33+
```shell
34+
rustup target add wasm32-wasip1
35+
```
36+
37+
Build the plugin with
38+
39+
```shell
40+
make
41+
```
42+
43+
The artifacts are found in target/plugin/
44+
45+
## Installation
46+
47+
This Demo is not meant for installation, it is just a showcase. However, traefik supports a manual installation.
48+
49+
```shell
50+
mkdir -p <traefik>/plugins-local/src/plugindemowasm/
51+
cp target/plugin/plugin.wasm <traefik>/plugins-local/src/plugindemowasm/
52+
53+
```
54+
Configure the static configuration (and restart traefik)
55+
```yaml
56+
# Static configuration
57+
58+
experimental:
59+
localPlugins:
60+
plugindemowasm:
61+
moduleName: plugindemowasm
62+
```
63+
Call the middleware from one of your routers
64+
```yaml
65+
# Dynamic configuration
66+
67+
http:
68+
routers:
69+
my-router:
70+
[...]
71+
middlewares:
72+
- plugindemowasm-mw
73+
[...]
74+
middlewares:
75+
plugindemowasm-mw:
76+
plugin:
77+
plugindemowasm:
78+
rules:
79+
- ""
80+
```

src/main.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use http_wasm_guest::{
2-
feature::{enable_feature, Feature},
32
host::{get_config, Request, Response},
43
info, register, Guest,
54
};
@@ -13,8 +12,14 @@ struct Config {
1312
struct Plugin {}
1413

1514
impl Guest for Plugin {
16-
fn handle_request(&self, request: Request, _response: Response) -> (bool, i32) {
17-
info!("uri: {}", to_str(request.uri().as_deref()));
15+
fn handle_request(&self, request: Request, response: Response) -> (bool, i32) {
16+
match request.uri() {
17+
Some(s) if s.starts_with("/.config".as_bytes()) => {
18+
response.set_status_code(403);
19+
return (false, 0);
20+
}
21+
_ => {}
22+
}
1823
(true, 0)
1924
}
2025
}
@@ -23,14 +28,7 @@ fn main() {
2328
.and_then(|v| serde_json::from_slice(&v).ok())
2429
.unwrap();
2530
info!("rules {:?}", &config.rules);
26-
enable_feature(Feature::BufferRequest);
2731

2832
let plugin = Plugin {};
2933
register(plugin);
3034
}
31-
32-
fn to_str(vec: Option<&[u8]>) -> String {
33-
vec.and_then(|v| std::str::from_utf8(v).ok())
34-
.unwrap_or_default()
35-
.to_string()
36-
}

0 commit comments

Comments
 (0)