mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
WIP use jsonrpsee rpcmodule
This commit is contained in:
parent
9090f883af
commit
6b75341d03
3 changed files with 285 additions and 17 deletions
|
@ -1,7 +1,11 @@
|
|||
use super::super::json_rpc::{JsonRpcExtractor, JsonRpcResponse, JsonRpcResult};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::super::definitions::network::StaticRoute;
|
||||
use super::super::definitions::system::User;
|
||||
use super::super::AppState;
|
||||
use axum::routing::post;
|
||||
use axum::{Json, Router};
|
||||
use jsonrpsee::types::Params;
|
||||
use tower_cookies::{Cookie, Cookies};
|
||||
|
||||
use axum::{
|
||||
|
@ -12,8 +16,29 @@ use axum::{
|
|||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use jsonrpsee::server::{RpcModule, Server};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
use custom_error::custom_error;
|
||||
|
||||
custom_error! { ApiError
|
||||
BadRequest = "Bad Request Parameters",
|
||||
}
|
||||
|
||||
struct RpcRequest<'a> {
|
||||
id: i64,
|
||||
params: Params<'a>,
|
||||
jsonrpc: String,
|
||||
method: String,
|
||||
}
|
||||
|
||||
struct RpcResponse<'a> {
|
||||
id: i64,
|
||||
payload: Params<'a>,
|
||||
jsonrpc: String,
|
||||
}
|
||||
|
||||
pub fn routes() -> Router<super::super::AppState> {
|
||||
Router::new().route("/api", post(api_handler))
|
||||
}
|
||||
|
@ -21,22 +46,49 @@ pub fn routes() -> Router<super::super::AppState> {
|
|||
async fn api_handler(
|
||||
State(state): State<AppState>,
|
||||
session: Extension<super::auth::Session>,
|
||||
req: JsonRpcExtractor,
|
||||
) -> JsonRpcResult {
|
||||
Json(rpc_request): Json<RpcRequest<'_>>,
|
||||
) -> impl IntoResponse {
|
||||
info!("api hit! user: {:?}", session.username);
|
||||
let req_id = req.get_request_id();
|
||||
let method = req.method();
|
||||
let response = match method {
|
||||
"add" => {
|
||||
let params: [i32; 2] = req.parse_params()?;
|
||||
JsonRpcResponse::success(req_id, params[0] + params[1])
|
||||
}
|
||||
"System.GetUsers" => JsonRpcResponse::success(
|
||||
req_id,
|
||||
state.config_manager.get_pending_config().system.users,
|
||||
),
|
||||
m => req.method_not_found(m),
|
||||
};
|
||||
let module = RpcModule::new(state);
|
||||
module
|
||||
.register_method("say_hello", |_, _| {
|
||||
println!("say_hello method called!");
|
||||
"Hello there!!"
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
Ok(response)
|
||||
module
|
||||
.register_method("System.GetUsers", get_users)
|
||||
.unwrap();
|
||||
module
|
||||
.register_method("Network.GetStaticRoutes", get_static_routes)
|
||||
.unwrap();
|
||||
|
||||
let res = module.call(&rpc_request.method, rpc_request.params).await;
|
||||
|
||||
match res {
|
||||
Ok(res) => RpcResponse {
|
||||
id: rpc_request.id,
|
||||
jsonrpc: rpc_request.jsonrpc,
|
||||
payload: res,
|
||||
},
|
||||
// TODO make Error Response
|
||||
Err(err) => RpcResponse {
|
||||
id: rpc_request.id,
|
||||
jsonrpc: rpc_request.jsonrpc,
|
||||
payload: res,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_users(_: Params, state: &AppState) -> Result<HashMap<String, User>, String> {
|
||||
Ok(state.config_manager.get_pending_config().system.users)
|
||||
}
|
||||
|
||||
fn get_static_routes(_: Params, state: &AppState) -> Result<Vec<StaticRoute>, String> {
|
||||
Ok(state
|
||||
.config_manager
|
||||
.get_pending_config()
|
||||
.network
|
||||
.static_routes)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue