draft: server: rpc client
parent
dea3d257ae
commit
876c4c188e
|
@ -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",
|
||||
]
|
||||
|
||||
|
|
|
@ -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"]
|
|
@ -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;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod daemon;
|
||||
mod models;
|
||||
mod rpc;
|
||||
mod server;
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
pub mod status;
|
|
@ -0,0 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Status {
|
||||
pub version: String,
|
||||
pub runtime: i64,
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<P: Into<PathBuf>>(socket: P) -> Self {
|
||||
Self {
|
||||
socket_path: socket.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn client(&self) -> Option<Client> {
|
||||
match connect(&self.socket_path).await {
|
||||
Ok(client) => Some(client),
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String>,
|
||||
|
||||
/// weywot daemon socket path
|
||||
#[argh(option, short = 's')]
|
||||
socket: Option<String>,
|
||||
}
|
||||
|
||||
#[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::<Config>())
|
||||
.manage(context)
|
||||
.ignite()
|
||||
.await?
|
||||
.launch()
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
use rocket;
|
||||
|
||||
#[rocket::get("/")]
|
||||
pub async fn hello() -> &'static str {
|
||||
"Hello, world!"
|
||||
}
|
|
@ -1 +1 @@
|
|||
pub mod hello;
|
||||
pub mod status;
|
||||
|
|
|
@ -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<Context>,
|
||||
) -> Result<Json<Status>, HttpStatus> {
|
||||
if let Some(client) = state.client().await {
|
||||
Ok(Json(client.status().await.unwrap()))
|
||||
} else {
|
||||
Err(HttpStatus::ServiceUnavailable)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue