@@ -18,6 +18,7 @@ use std::collections::HashMap;
1818
1919use anyhow:: anyhow;
2020use fastedge:: key_value:: { Store , Error as StoreError } ;
21+ use serde_json:: json;
2122use wstd:: http:: body:: Body ;
2223use 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