Skip to content

Commit 9f40778

Browse files
authored
Merge pull request #63 from G-Core/fix/plugin-docs
example updates as per copilot request
2 parents 1205d92 + adcabd7 commit 9f40778

4 files changed

Lines changed: 102 additions & 99 deletions

File tree

examples/cdn/key_value/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ crate-type = ["cdylib"]
1212
proxy-wasm = "0.2"
1313
fastedge = { version = "0.3", features = ["proxywasm"] }
1414
querystring = "1.1"
15+
serde_json = "1"

examples/cdn/key_value/src/lib.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Defaults to action=get if not specified.
1717
use fastedge::proxywasm::key_value::Store;
1818
use proxy_wasm::traits::*;
1919
use proxy_wasm::types::*;
20+
use serde_json::json;
2021
use std::collections::HashMap;
2122

2223
proxy_wasm::main! {{
@@ -53,7 +54,7 @@ impl HttpContext for KvStoreContext {
5354

5455
fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
5556
if !end_of_stream {
56-
return Action::StopIterationAndBuffer;
57+
return Action::Pause;
5758
}
5859

5960
let query = self
@@ -117,34 +118,32 @@ impl KvStoreContext {
117118
match store.get(key) {
118119
Ok(Some(value)) => {
119120
let value_str = String::from_utf8_lossy(&value);
120-
Ok(format!(
121-
r#"{{"store":"{}","action":"get","key":"{}","response":"{}"}}"#,
122-
params.get("store").unwrap_or(&""),
123-
key,
124-
value_str
125-
))
121+
Ok(json!({
122+
"store": params.get("store").unwrap_or(&""),
123+
"action": "get",
124+
"key": key,
125+
"response": value_str.as_ref()
126+
}).to_string())
126127
}
127-
Ok(None) => Ok(format!(
128-
r#"{{"store":"{}","action":"get","key":"{}","response":null}}"#,
129-
params.get("store").unwrap_or(&""),
130-
key
131-
)),
128+
Ok(None) => Ok(json!({
129+
"store": params.get("store").unwrap_or(&""),
130+
"action": "get",
131+
"key": key,
132+
"response": null
133+
}).to_string()),
132134
Err(e) => Err(format!("KV get error: {}", e)),
133135
}
134136
}
135137

136138
fn handle_scan(&self, store: &Store, params: &HashMap<&str, &str>) -> Result<String, String> {
137139
let pattern = *params.get("match").ok_or("Missing required param 'match' for 'scan' action")?;
138140
match store.scan(pattern) {
139-
Ok(keys) => {
140-
let keys_json: Vec<String> = keys.iter().map(|k| format!(r#""{}""#, k)).collect();
141-
Ok(format!(
142-
r#"{{"store":"{}","action":"scan","match":"{}","response":[{}]}}"#,
143-
params.get("store").unwrap_or(&""),
144-
pattern,
145-
keys_json.join(",")
146-
))
147-
}
141+
Ok(keys) => Ok(json!({
142+
"store": params.get("store").unwrap_or(&""),
143+
"action": "scan",
144+
"match": pattern,
145+
"response": keys
146+
}).to_string()),
148147
Err(e) => Err(format!("KV scan error: {}", e)),
149148
}
150149
}
@@ -164,21 +163,21 @@ impl KvStoreContext {
164163

165164
match store.zrange_by_score(key, min, max) {
166165
Ok(entries) => {
167-
let entries_json: Vec<String> = entries
166+
let entries_json: Vec<serde_json::Value> = entries
168167
.iter()
169168
.map(|(value, score)| {
170169
let value_str = String::from_utf8_lossy(value);
171-
format!(r#"{{"value":"{}","score":{}}}"#, value_str, score)
170+
json!({"value": value_str.as_ref(), "score": score})
172171
})
173172
.collect();
174-
Ok(format!(
175-
r#"{{"store":"{}","action":"zrange","key":"{}","min":{},"max":{},"response":[{}]}}"#,
176-
params.get("store").unwrap_or(&""),
177-
key,
178-
min,
179-
max,
180-
entries_json.join(",")
181-
))
173+
Ok(json!({
174+
"store": params.get("store").unwrap_or(&""),
175+
"action": "zrange",
176+
"key": key,
177+
"min": min,
178+
"max": max,
179+
"response": entries_json
180+
}).to_string())
182181
}
183182
Err(e) => Err(format!("KV zrange error: {}", e)),
184183
}
@@ -190,20 +189,20 @@ impl KvStoreContext {
190189

191190
match store.zscan(key, pattern) {
192191
Ok(entries) => {
193-
let entries_json: Vec<String> = entries
192+
let entries_json: Vec<serde_json::Value> = entries
194193
.iter()
195194
.map(|(value, score)| {
196195
let value_str = String::from_utf8_lossy(value);
197-
format!(r#"{{"value":"{}","score":{}}}"#, value_str, score)
196+
json!({"value": value_str.as_ref(), "score": score})
198197
})
199198
.collect();
200-
Ok(format!(
201-
r#"{{"store":"{}","action":"zscan","key":"{}","match":"{}","response":[{}]}}"#,
202-
params.get("store").unwrap_or(&""),
203-
key,
204-
pattern,
205-
entries_json.join(",")
206-
))
199+
Ok(json!({
200+
"store": params.get("store").unwrap_or(&""),
201+
"action": "zscan",
202+
"key": key,
203+
"match": pattern,
204+
"response": entries_json
205+
}).to_string())
207206
}
208207
Err(e) => Err(format!("KV zscan error: {}", e)),
209208
}
@@ -214,13 +213,13 @@ impl KvStoreContext {
214213
let item = *params.get("item").ok_or("Missing required param 'item' for 'bfExists' action")?;
215214

216215
match store.bf_exists(key, item) {
217-
Ok(exists) => Ok(format!(
218-
r#"{{"store":"{}","action":"bfExists","key":"{}","item":"{}","response":{}}}"#,
219-
params.get("store").unwrap_or(&""),
220-
key,
221-
item,
222-
exists
223-
)),
216+
Ok(exists) => Ok(json!({
217+
"store": params.get("store").unwrap_or(&""),
218+
"action": "bfExists",
219+
"key": key,
220+
"item": item,
221+
"response": exists
222+
}).to_string()),
224223
Err(e) => Err(format!("KV bfExists error: {}", e)),
225224
}
226225
}
@@ -231,7 +230,7 @@ impl KvStoreContext {
231230
vec!["response", "status"],
232231
Some(b"500"),
233232
);
234-
let error_body = format!(r#"{{"error":"{}"}}"#, msg);
233+
let error_body = json!({"error": msg}).to_string();
235234
self.set_http_response_body(0, body_size, error_body.as_bytes()).ok();
236235
}
237236
}

examples/http/wasi/key_value/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ wstd = "0.6"
1313
fastedge = "0.3"
1414
anyhow = "1"
1515
querystring = "1.1"
16+
serde_json = "1"

examples/http/wasi/key_value/src/lib.rs

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::collections::HashMap;
1818

1919
use anyhow::anyhow;
2020
use fastedge::key_value::{Store, Error as StoreError};
21+
use serde_json::json;
2122
use wstd::http::body::Body;
2223
use wstd::http::{Request, Response};
2324

@@ -37,12 +38,14 @@ async fn main(req: Request<Body>) -> anyhow::Result<Response<Body>> {
3738
Err(StoreError::AccessDenied) => {
3839
return Ok(Response::builder()
3940
.status(403)
40-
.body(Body::from("access denied"))?);
41+
.header("content-type", "application/json")
42+
.body(Body::from(json!({"error": "access denied"}).to_string()))?);
4143
}
4244
Err(e) => {
4345
return Ok(Response::builder()
4446
.status(500)
45-
.body(Body::from(format!("store open error: {e}")))?);
47+
.header("content-type", "application/json")
48+
.body(Body::from(json!({"error": format!("store open error: {e}")}).to_string()))?);
4649
}
4750
};
4851

@@ -53,9 +56,10 @@ async fn main(req: Request<Body>) -> anyhow::Result<Response<Body>> {
5356
"zscan" => handle_zscan(&store, &params)?,
5457
"bfExists" => handle_bf_exists(&store, &params)?,
5558
_ => {
56-
return Ok(Response::builder().status(400).body(Body::from(format!(
57-
"Invalid action '{action}'. Supported: get, scan, zrange, zscan, bfExists"
58-
)))?);
59+
return Ok(Response::builder()
60+
.status(400)
61+
.header("content-type", "application/json")
62+
.body(Body::from(json!({"error": format!("Invalid action '{action}'. Supported: get, scan, zrange, zscan, bfExists")}).to_string()))?);
5963
}
6064
};
6165

@@ -70,18 +74,19 @@ fn handle_get(store: &Store, params: &HashMap<&str, &str>) -> anyhow::Result<Str
7074
match store.get(key) {
7175
Ok(Some(value)) => {
7276
let value_str = String::from_utf8_lossy(&value);
73-
Ok(format!(
74-
r#"{{"store":"{}","action":"get","key":"{}","response":"{}"}}"#,
75-
params.get("store").unwrap_or(&""),
76-
key,
77-
value_str
78-
))
77+
Ok(json!({
78+
"store": params.get("store").unwrap_or(&""),
79+
"action": "get",
80+
"key": key,
81+
"response": value_str.as_ref()
82+
}).to_string())
7983
}
80-
Ok(None) => Ok(format!(
81-
r#"{{"store":"{}","action":"get","key":"{}","response":null}}"#,
82-
params.get("store").unwrap_or(&""),
83-
key
84-
)),
84+
Ok(None) => Ok(json!({
85+
"store": params.get("store").unwrap_or(&""),
86+
"action": "get",
87+
"key": key,
88+
"response": null
89+
}).to_string()),
8590
Err(e) => Err(anyhow!("KV get error: {e}")),
8691
}
8792
}
@@ -91,15 +96,12 @@ fn handle_scan(store: &Store, params: &HashMap<&str, &str>) -> anyhow::Result<St
9196
.get("match")
9297
.ok_or(anyhow!("missing param 'match'"))?;
9398
match store.scan(pattern) {
94-
Ok(keys) => {
95-
let keys_json: Vec<String> = keys.iter().map(|k| format!(r#""{}""#, k)).collect();
96-
Ok(format!(
97-
r#"{{"store":"{}","action":"scan","match":"{}","response":[{}]}}"#,
98-
params.get("store").unwrap_or(&""),
99-
pattern,
100-
keys_json.join(",")
101-
))
102-
}
99+
Ok(keys) => Ok(json!({
100+
"store": params.get("store").unwrap_or(&""),
101+
"action": "scan",
102+
"match": pattern,
103+
"response": keys
104+
}).to_string()),
103105
Err(e) => Err(anyhow!("KV scan error: {e}")),
104106
}
105107
}
@@ -119,21 +121,21 @@ fn handle_zrange(store: &Store, params: &HashMap<&str, &str>) -> anyhow::Result<
119121

120122
match store.zrange_by_score(key, min, max) {
121123
Ok(entries) => {
122-
let entries_json: Vec<String> = entries
124+
let entries_json: Vec<serde_json::Value> = entries
123125
.iter()
124126
.map(|(value, score)| {
125127
let value_str = String::from_utf8_lossy(value);
126-
format!(r#"{{"value":"{}","score":{}}}"#, value_str, score)
128+
json!({"value": value_str.as_ref(), "score": score})
127129
})
128130
.collect();
129-
Ok(format!(
130-
r#"{{"store":"{}","action":"zrange","key":"{}","min":{},"max":{},"response":[{}]}}"#,
131-
params.get("store").unwrap_or(&""),
132-
key,
133-
min,
134-
max,
135-
entries_json.join(",")
136-
))
131+
Ok(json!({
132+
"store": params.get("store").unwrap_or(&""),
133+
"action": "zrange",
134+
"key": key,
135+
"min": min,
136+
"max": max,
137+
"response": entries_json
138+
}).to_string())
137139
}
138140
Err(e) => Err(anyhow!("KV zrange error: {e}")),
139141
}
@@ -147,20 +149,20 @@ fn handle_zscan(store: &Store, params: &HashMap<&str, &str>) -> anyhow::Result<S
147149

148150
match store.zscan(key, pattern) {
149151
Ok(entries) => {
150-
let entries_json: Vec<String> = entries
152+
let entries_json: Vec<serde_json::Value> = entries
151153
.iter()
152154
.map(|(value, score)| {
153155
let value_str = String::from_utf8_lossy(value);
154-
format!(r#"{{"value":"{}","score":{}}}"#, value_str, score)
156+
json!({"value": value_str.as_ref(), "score": score})
155157
})
156158
.collect();
157-
Ok(format!(
158-
r#"{{"store":"{}","action":"zscan","key":"{}","match":"{}","response":[{}]}}"#,
159-
params.get("store").unwrap_or(&""),
160-
key,
161-
pattern,
162-
entries_json.join(",")
163-
))
159+
Ok(json!({
160+
"store": params.get("store").unwrap_or(&""),
161+
"action": "zscan",
162+
"key": key,
163+
"match": pattern,
164+
"response": entries_json
165+
}).to_string())
164166
}
165167
Err(e) => Err(anyhow!("KV zscan error: {e}")),
166168
}
@@ -173,13 +175,13 @@ fn handle_bf_exists(store: &Store, params: &HashMap<&str, &str>) -> anyhow::Resu
173175
.ok_or(anyhow!("missing param 'item'"))?;
174176

175177
match store.bf_exists(key, item) {
176-
Ok(exists) => Ok(format!(
177-
r#"{{"store":"{}","action":"bfExists","key":"{}","item":"{}","response":{}}}"#,
178-
params.get("store").unwrap_or(&""),
179-
key,
180-
item,
181-
exists
182-
)),
178+
Ok(exists) => Ok(json!({
179+
"store": params.get("store").unwrap_or(&""),
180+
"action": "bfExists",
181+
"key": key,
182+
"item": item,
183+
"response": exists
184+
}).to_string()),
183185
Err(e) => Err(anyhow!("KV bfExists error: {e}")),
184186
}
185187
}

0 commit comments

Comments
 (0)