Skip to content

Commit

Permalink
* json-rpc - big update to use the rpc-router crate
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Mar 14, 2024
1 parent bd34652 commit 3515abd
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 525 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ resolver = "2"
members = [
# -- Application Libraries
"crates/libs/lib-utils", # e.g., base64, time.
"crates/libs/lib-rpc", # e.g., rpc routing.
"crates/libs/lib-auth", # e.g., for pwd, token.
"crates/libs/lib-core", # e.g., model, ctx, config.
"crates/libs/lib-rpc", # e.g., app rpc handlers (using rpc-router crate)
"crates/libs/lib-auth", # e.g., for pwd, token.
"crates/libs/lib-core", # e.g., model, ctx, config.

# -- Application Services
"crates/services/web-server",
Expand All @@ -25,5 +25,8 @@ members = [
serde_with = {version = "3", features = ["time_0_3"] }
# -- Data
modql = { version = "0.4.0-rc", features = ["with-sea-query"]}
# -- JSON-RPC
# Lock to specific version during 0.1.x
rpc-router = { version = "=0.1.3" }
# -- Others
derive_more = {version = "1.0.0-beta", features = ["from", "display"] }
6 changes: 6 additions & 0 deletions crates/libs/lib-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ doctest = false
[lints]
workspace = true

[features]
with-rpc = ["rpc-router"]

[dependencies]
# -- App Libs
lib-utils = { path = "../../libs/lib-utils"}
Expand All @@ -31,5 +34,8 @@ uuid = {version = "1", features = ["v4","fast-rng",]}
time = {version = "0.3", features = ["formatting", "parsing", "serde"]}
derive_more = { workspace = true }

# -- Feature: with-rpc
rpc-router = { workspace = true, optional = true }

[dev-dependencies]
serial_test = "3"
1 change: 1 addition & 0 deletions crates/libs/lib-core/src/ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use self::error::{Error, Result};

// endregion: --- Modules

#[cfg_attr(feature = "with-rpc", derive(rpc_router::RpcResource))]
#[derive(Clone, Debug)]
pub struct Ctx {
user_id: i64,
Expand Down
1 change: 1 addition & 0 deletions crates/libs/lib-core/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::model::store::new_db_pool;

// region: --- ModelManager

#[cfg_attr(feature = "with-rpc", derive(rpc_router::RpcResource))]
#[derive(Clone)]
pub struct ModelManager {
dbx: Dbx,
Expand Down
4 changes: 3 additions & 1 deletion crates/libs/lib-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ workspace = true

[dependencies]
# -- App Libs
lib-core = { path = "../../libs/lib-core"}
lib-core = { path = "../../libs/lib-core", features = ["with-rpc"] }
# -- Async
tokio = { version = "1", features = ["full"] }
futures = "0.3"
Expand All @@ -21,6 +21,8 @@ serde_json = "1"
serde_with = { workspace = true }
# -- Data
modql = { workspace = true }
# -- Rpc
rpc-router = { workspace = true }
# -- Others
paste = "1"
derive_more = { workspace = true }
27 changes: 19 additions & 8 deletions crates/libs/lib-rpc/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
//! This module encompasses errors for all `lib_rpc` modules and rpc handlers.
//! Variants from our application's library errors can be added as required by the handlers.
//!
//! # Note on the `rpc-router::Error` scheme
//!
//! - When used in an rpc handler with the `rpc-router` crate,
//! this type will be encapsulated as a `rpc-router::Error::Handler(RpcHandlerError)` within a
//! "TypeMap" and can subsequently be retrieved (see `web-server::web::Error` for reference).
//!
//! - For this application error to be utilized in the `rpc-router`, it must
//! implement the `IntoRpcHandlerError` trait. This trait has a suitable default implementation,
//! so simply adding `impl rpc_router::IntoRpcHandlerError for Error {}` would suffice.
//!
//! - Alternatively, the `#[derive(RpcHandlerError)]` annotation can be used as demonstrated here, which will
//! automatically provide the `impl rpc_router::IntoRpcHandlerError for Error {}` for this type.

use derive_more::From;
use rpc_router::RpcHandlerError;
use serde::Serialize;
use serde_with::{serde_as, DisplayFromStr};

pub type Result<T> = core::result::Result<T, Error>;

#[serde_as]
#[derive(Debug, From, Serialize)]
#[derive(Debug, From, Serialize, RpcHandlerError)]
pub enum Error {
MissingCtx,

// -- RPC Router
RpcMethodUnknown(String),
RpcIntoParamsMissing,

// -- Modules
// -- App Libs
#[from]
Model(lib_core::model::Error),

Expand Down
5 changes: 0 additions & 5 deletions crates/libs/lib-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// region: --- Modules

mod error;
mod resources;
mod rpc_params;
mod rpc_result;
mod rpcs;

pub mod router;

pub use self::error::{Error, Result};
pub use resources::RpcResources;
pub use router::RpcRequest;
pub use rpc_params::*;

pub use rpcs::*;
Expand Down
27 changes: 0 additions & 27 deletions crates/libs/lib-rpc/src/resources.rs

This file was deleted.

8 changes: 0 additions & 8 deletions crates/libs/lib-rpc/src/router/from_resources.rs

This file was deleted.

33 changes: 0 additions & 33 deletions crates/libs/lib-rpc/src/router/into_params.rs

This file was deleted.

174 changes: 0 additions & 174 deletions crates/libs/lib-rpc/src/router/mod.rs

This file was deleted.

Loading

0 comments on commit 3515abd

Please sign in to comment.