Get JsonRPC Working

This commit is contained in:
Samuel Lorch 2023-10-28 15:44:12 +02:00
parent 0a603b6642
commit 42cc14cb14
6 changed files with 160 additions and 66 deletions

48
src/api/mod.rs Normal file
View file

@ -0,0 +1,48 @@
mod network;
mod system;
use crate::state::RpcState;
use jsonrpsee::{
types::{error::ErrorCode, ErrorObject},
RpcModule,
};
use custom_error::custom_error;
use tracing::info;
custom_error! { pub ApiError
InvalidParams = "Invalid Parameters",
Leet = "1337",
}
impl Into<ErrorObject<'static>> for ApiError {
fn into(self) -> ErrorObject<'static> {
match self {
Self::InvalidParams => ErrorCode::InvalidParams,
Self::Leet => ErrorCode::ServerError(1337),
_ => ErrorCode::InternalError,
}
.into()
}
}
pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
let mut module = RpcModule::new(state);
module
.register_method("ping", |_, _| {
info!("ping called");
"pong"
})
.unwrap();
module
.register_method("System.GetUsers", system::get_users)
.unwrap();
module
.register_method("Network.GetStaticRoutes", network::get_static_routes)
.unwrap();
module
}

13
src/api/network.rs Normal file
View file

@ -0,0 +1,13 @@
use jsonrpsee::types::Params;
use crate::{definitions::network::StaticRoute, state::RpcState};
use super::ApiError;
pub fn get_static_routes(_: Params, state: &RpcState) -> Result<Vec<StaticRoute>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.network
.static_routes)
}

10
src/api/system.rs Normal file
View file

@ -0,0 +1,10 @@
use std::collections::HashMap;
use crate::{definitions::system::User, state::RpcState};
use jsonrpsee::types::Params;
use super::ApiError;
pub fn get_users(_: Params, state: &RpcState) -> Result<HashMap<String, User>, ApiError> {
Ok(state.config_manager.get_pending_config().system.users)
}