draft: server: rpc client
parent
dea3d257ae
commit
876c4c188e
|
@ -807,9 +807,12 @@ dependencies = [
|
||||||
"futures 0.3.19",
|
"futures 0.3.19",
|
||||||
"jsonrpc-core",
|
"jsonrpc-core",
|
||||||
"jsonrpc-pubsub",
|
"jsonrpc-pubsub",
|
||||||
|
"jsonrpc-server-utils",
|
||||||
"log",
|
"log",
|
||||||
|
"parity-tokio-ipc",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,12 @@ figment = "^0.10"
|
||||||
jsonrpc-core = "~18.0"
|
jsonrpc-core = "~18.0"
|
||||||
jsonrpc-derive = "~18.0"
|
jsonrpc-derive = "~18.0"
|
||||||
jsonrpc-ipc-server = "~18.0"
|
jsonrpc-ipc-server = "~18.0"
|
||||||
jsonrpc-core-client = "~18.0"
|
|
||||||
serde = "^1.0"
|
serde = "^1.0"
|
||||||
|
|
||||||
|
[dependencies.jsonrpc-core-client]
|
||||||
|
version = "~18.0"
|
||||||
|
features = ["ipc"]
|
||||||
|
|
||||||
[dependencies.procfs]
|
[dependencies.procfs]
|
||||||
version = "^0.12"
|
version = "^0.12"
|
||||||
features = ["chrono"]
|
features = ["chrono"]
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::rpc::{Status, WeywotRpc};
|
use crate::models::status::Status;
|
||||||
|
use crate::rpc::WeywotRpc;
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local};
|
||||||
use jsonrpc_core::Result as JsonRpcResult;
|
use jsonrpc_core::Result as JsonRpcResult;
|
||||||
use procfs::process::Process;
|
use procfs::process::Process;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
mod daemon;
|
mod daemon;
|
||||||
|
mod models;
|
||||||
mod rpc;
|
mod rpc;
|
||||||
mod server;
|
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_core::Result;
|
||||||
use jsonrpc_derive::rpc;
|
use jsonrpc_derive::rpc;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
pub struct Status {
|
|
||||||
pub version: String,
|
|
||||||
pub runtime: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rpc]
|
#[rpc]
|
||||||
pub trait WeywotRpc {
|
pub trait WeywotRpc {
|
||||||
|
|
|
@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub address: String,
|
pub address: String,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
|
pub socket: String
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
@ -11,6 +12,7 @@ impl Default for Config {
|
||||||
Config {
|
Config {
|
||||||
address: "::".into(),
|
address: "::".into(),
|
||||||
port: 8998,
|
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 config;
|
||||||
|
mod context;
|
||||||
mod routes;
|
mod routes;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
use argh::FromArgs;
|
use argh::FromArgs;
|
||||||
|
use context::Context;
|
||||||
use rocket;
|
use rocket;
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::AdHoc;
|
||||||
use rocket::figment::providers::{Env, Format, Serialized, Toml};
|
use rocket::figment::providers::{Env, Format, Serialized, Toml};
|
||||||
|
@ -24,6 +26,10 @@ struct Arguments {
|
||||||
/// configuration file
|
/// configuration file
|
||||||
#[argh(option, short = 'c')]
|
#[argh(option, short = 'c')]
|
||||||
config: Option<String>,
|
config: Option<String>,
|
||||||
|
|
||||||
|
/// weywot daemon socket path
|
||||||
|
#[argh(option, short = 's')]
|
||||||
|
socket: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::main]
|
#[rocket::main]
|
||||||
|
@ -42,10 +48,22 @@ pub async fn main() -> Result<(), rocket::Error> {
|
||||||
if let Some(port) = args.port {
|
if let Some(port) = args.port {
|
||||||
figment = figment.merge(("port", 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)
|
rocket::custom(figment)
|
||||||
.mount("/", rocket::routes![routes::hello::hello])
|
.mount("/", rocket::routes![routes::status::get_status])
|
||||||
.attach(AdHoc::config::<Config>())
|
.attach(AdHoc::config::<Config>())
|
||||||
|
.manage(context)
|
||||||
.ignite()
|
.ignite()
|
||||||
.await?
|
.await?
|
||||||
.launch()
|
.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