allow closure handlers
This commit is contained in:
parent
cbbeff1a6d
commit
fe476ce884
|
@ -8,7 +8,7 @@ pub struct ApiMethod {
|
||||||
pub description: &'static str,
|
pub description: &'static str,
|
||||||
pub parameters: Schema,
|
pub parameters: Schema,
|
||||||
pub returns: Schema,
|
pub returns: Schema,
|
||||||
pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>,
|
pub handler: Box<Fn(Value, &ApiMethod) -> Result<Value, Error> + Send + Sync>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum SubRoute {
|
pub enum SubRoute {
|
||||||
|
|
|
@ -173,7 +173,7 @@ fn handle_sync_api_request(
|
||||||
tokio::spawn(task);
|
tokio::spawn(task);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let res = (info.handler)(params, info)?;
|
let res = (*info.handler)(params, info)?;
|
||||||
|
|
||||||
Ok(res)
|
Ok(res)
|
||||||
|
|
||||||
|
|
14
src/api3.rs
14
src/api3.rs
|
@ -25,25 +25,23 @@ fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn test_subdir_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
|
||||||
println!("This is a subdir {}", param);
|
|
||||||
|
|
||||||
Ok(json!(null))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn router() -> Router {
|
pub fn router() -> Router {
|
||||||
|
|
||||||
let route3 = Router::new()
|
let route3 = Router::new()
|
||||||
.get(ApiMethod {
|
.get(ApiMethod {
|
||||||
handler: test_subdir_api_handler,
|
|
||||||
description: "Another Endpoint.",
|
description: "Another Endpoint.",
|
||||||
parameters: parameter!{},
|
parameters: parameter!{},
|
||||||
returns: Schema::Null,
|
returns: Schema::Null,
|
||||||
|
handler: Box::new(|param, _info| {
|
||||||
|
println!("This is a clousure handler: {}", param);
|
||||||
|
|
||||||
|
Ok(json!(null))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let route2 = Router::new()
|
let route2 = Router::new()
|
||||||
.get(ApiMethod {
|
.get(ApiMethod {
|
||||||
handler: test_sync_api_handler,
|
handler: Box::new(test_sync_api_handler),
|
||||||
description: "This is a simple test.",
|
description: "This is a simple test.",
|
||||||
parameters: parameter!{
|
parameters: parameter!{
|
||||||
force => Boolean!{
|
force => Boolean!{
|
||||||
|
|
Loading…
Reference in New Issue