From 876c4c188e7da7c4d264fb868647983f72d9d978 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 8 Jan 2022 21:29:51 -0600 Subject: [PATCH] draft: server: rpc client --- backend/Cargo.lock | 3 +++ backend/Cargo.toml | 7 +++++-- backend/src/daemon/rpc.rs | 3 ++- backend/src/main.rs | 1 + backend/src/models/mod.rs | 1 + backend/src/models/status.rs | 7 +++++++ backend/src/rpc.rs | 8 +------- backend/src/server/config.rs | 2 ++ backend/src/server/context.rs | 25 +++++++++++++++++++++++++ backend/src/server/mod.rs | 20 +++++++++++++++++++- backend/src/server/routes/hello.rs | 6 ------ backend/src/server/routes/mod.rs | 2 +- backend/src/server/routes/status.rs | 16 ++++++++++++++++ 13 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 backend/src/models/mod.rs create mode 100644 backend/src/models/status.rs create mode 100644 backend/src/server/context.rs delete mode 100644 backend/src/server/routes/hello.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 80323f7..11a1466 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -807,9 +807,12 @@ dependencies = [ "futures 0.3.19", "jsonrpc-core", "jsonrpc-pubsub", + "jsonrpc-server-utils", "log", + "parity-tokio-ipc", "serde", "serde_json", + "tokio", "url", ] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 1a85e52..2a1e5ae 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -10,13 +10,16 @@ figment = "^0.10" jsonrpc-core = "~18.0" jsonrpc-derive = "~18.0" jsonrpc-ipc-server = "~18.0" -jsonrpc-core-client = "~18.0" serde = "^1.0" +[dependencies.jsonrpc-core-client] +version = "~18.0" +features = ["ipc"] + [dependencies.procfs] version = "^0.12" features = ["chrono"] [dependencies.rocket] version = "^0.5.0-rc.1" -features = ["json", "secrets", "tls"] +features = ["json", "secrets", "tls"] \ No newline at end of file diff --git a/backend/src/daemon/rpc.rs b/backend/src/daemon/rpc.rs index 7a08dcd..2d50093 100644 --- a/backend/src/daemon/rpc.rs +++ b/backend/src/daemon/rpc.rs @@ -1,4 +1,5 @@ -use crate::rpc::{Status, WeywotRpc}; +use crate::models::status::Status; +use crate::rpc::WeywotRpc; use chrono::{DateTime, Local}; use jsonrpc_core::Result as JsonRpcResult; use procfs::process::Process; diff --git a/backend/src/main.rs b/backend/src/main.rs index e008ab6..cf0a755 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,4 +1,5 @@ mod daemon; +mod models; mod rpc; mod server; diff --git a/backend/src/models/mod.rs b/backend/src/models/mod.rs new file mode 100644 index 0000000..16a1865 --- /dev/null +++ b/backend/src/models/mod.rs @@ -0,0 +1 @@ +pub mod status; \ No newline at end of file diff --git a/backend/src/models/status.rs b/backend/src/models/status.rs new file mode 100644 index 0000000..61e9a6e --- /dev/null +++ b/backend/src/models/status.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; +#[derive(Debug, Deserialize, Serialize)] +pub struct Status { + pub version: String, + pub runtime: i64, +} + diff --git a/backend/src/rpc.rs b/backend/src/rpc.rs index 627e984..4e70eb0 100644 --- a/backend/src/rpc.rs +++ b/backend/src/rpc.rs @@ -1,12 +1,6 @@ +use crate::models::status::Status; use jsonrpc_core::Result; use jsonrpc_derive::rpc; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize, Serialize)] -pub struct Status { - pub version: String, - pub runtime: i64, -} #[rpc] pub trait WeywotRpc { diff --git a/backend/src/server/config.rs b/backend/src/server/config.rs index 57854ca..d9f601e 100644 --- a/backend/src/server/config.rs +++ b/backend/src/server/config.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; pub struct Config { pub address: String, pub port: u16, + pub socket: String } impl Default for Config { @@ -11,6 +12,7 @@ impl Default for Config { Config { address: "::".into(), port: 8998, + socket: "weywot.sock".into(), } } } diff --git a/backend/src/server/context.rs b/backend/src/server/context.rs new file mode 100644 index 0000000..3f4c658 --- /dev/null +++ b/backend/src/server/context.rs @@ -0,0 +1,25 @@ +use crate::rpc::gen_client::Client; +use jsonrpc_core_client::transports::ipc::connect; +use std::path::PathBuf; + +pub struct Context { + socket_path: PathBuf, +} + +impl Context { + pub fn new>(socket: P) -> Self { + Self { + socket_path: socket.into(), + } + } + + pub async fn client(&self) -> Option { + match connect(&self.socket_path).await { + Ok(client) => Some(client), + Err(e) => { + eprintln!("{}", e); + None + } + } + } +} diff --git a/backend/src/server/mod.rs b/backend/src/server/mod.rs index e4aa81b..2aa6995 100644 --- a/backend/src/server/mod.rs +++ b/backend/src/server/mod.rs @@ -1,8 +1,10 @@ mod config; +mod context; mod routes; use config::Config; use argh::FromArgs; +use context::Context; use rocket; use rocket::fairing::AdHoc; use rocket::figment::providers::{Env, Format, Serialized, Toml}; @@ -24,6 +26,10 @@ struct Arguments { /// configuration file #[argh(option, short = 'c')] config: Option, + + /// weywot daemon socket path + #[argh(option, short = 's')] + socket: Option, } #[rocket::main] @@ -42,10 +48,22 @@ pub async fn main() -> Result<(), rocket::Error> { if let Some(port) = args.port { figment = figment.merge(("port", port)); } + if let Some(socket) = args.socket { + figment = figment.merge(("socket", socket)); + } + + let context = Context::new( + figment + .find_value("socket") + .expect("No daemon socket path configured") + .as_str() + .expect("Invalid daemon socket path"), + ); rocket::custom(figment) - .mount("/", rocket::routes![routes::hello::hello]) + .mount("/", rocket::routes![routes::status::get_status]) .attach(AdHoc::config::()) + .manage(context) .ignite() .await? .launch() diff --git a/backend/src/server/routes/hello.rs b/backend/src/server/routes/hello.rs deleted file mode 100644 index ca8411c..0000000 --- a/backend/src/server/routes/hello.rs +++ /dev/null @@ -1,6 +0,0 @@ -use rocket; - -#[rocket::get("/")] -pub async fn hello() -> &'static str { - "Hello, world!" -} diff --git a/backend/src/server/routes/mod.rs b/backend/src/server/routes/mod.rs index 152adf1..822c729 100644 --- a/backend/src/server/routes/mod.rs +++ b/backend/src/server/routes/mod.rs @@ -1 +1 @@ -pub mod hello; +pub mod status; diff --git a/backend/src/server/routes/status.rs b/backend/src/server/routes/status.rs index e69de29..b12e85f 100644 --- a/backend/src/server/routes/status.rs +++ b/backend/src/server/routes/status.rs @@ -0,0 +1,16 @@ +use crate::models::status::Status; +use crate::server::context::Context; +use rocket::http::Status as HttpStatus; +use rocket::serde::json::Json; +use rocket::State; + +#[rocket::get("/status")] +pub async fn get_status( + state: &State, +) -> Result, HttpStatus> { + if let Some(client) = state.client().await { + Ok(Json(client.status().await.unwrap())) + } else { + Err(HttpStatus::ServiceUnavailable) + } +}