Crates.io | logimesh-macro |
lib.rs | logimesh-macro |
version | 0.1.9 |
source | src |
created_at | 2024-08-21 03:36:48.619507 |
updated_at | 2024-08-29 16:56:49.812946 |
description | logimesh-macro is Proc macros for logimesh. |
homepage | https://github.com/andeya/logimesh |
repository | https://github.com/andeya/logimesh |
max_upload_size | |
id | 1346085 |
size | 44,191 |
logimesh
is a Rust RPC Microservice 2.0 framework inspired by the Towards Modern Development of Cloud Applications paper.
(This is one of my amateur idea and is only developed in leisure-time.)
Add to your Cargo.toml
dependencies:
logimesh = "0.1"
The logimesh::component
attribute expands to a collection of items that form an component component.
These generated types make it easy and ergonomic to write servers with less boilerplate.
Simply implement the generated component trait, and you're off to the races!
This example uses tokio, so add the following dependencies to
your Cargo.toml
:
[lib]
name = "service"
path = "src/lib.rs"
...
[dependencies]
logimesh = { version = "0.1" }
anyhow = "1.0"
tokio = { version = "1.0", features = ["macros"] }
For a more real-world example, see logimesh-example.
First, let's set up the dependencies and component definition.
lib.rs
fileextern crate logimesh;
// This is the component definition. It looks a lot like a trait definition.
// It defines one RPC, hello, which takes one arg, name, and returns a String.
#[logimesh::component]
trait World {
/// Returns a greeting for name.
async fn hello(name: String) -> String;
}
This component definition generates a trait called World
. Next we need to
implement it for our Server struct.
# extern crate logimesh;
#
# // This is the component definition. It looks a lot like a trait definition.
# // It defines one RPC, hello, which takes one arg, name, and returns a String.
# #[logimesh::component]
# trait World {
# /// Returns a greeting for name.
# async fn hello(name: String) -> String;
# }
use logimesh::context;
use logimesh::transport::codec::Codec;
/// This is the type that implements the generated World trait. It is the business logic
/// and is used to start the server.
#[derive(Clone)]
pub struct CompHello;
impl World for CompHello {
const TRANSPORT_CODEC: Codec = Codec::Json;
async fn hello(self, ctx: context::Context, name: String) -> String {
format!("Hello, {name}! context: {:?}", ctx)
}
}
server.rs
fileextern crate logimesh;
extern crate tokio;
extern crate anyhow;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
logimesh::tokio_tcp_listen!(CompHello, logimesh::server::TcpConfig::new("[::1]:8888".parse::<std::net::SocketAddrV6>().unwrap()));
Ok(())
}
client.rs
fileextern crate logimesh;
extern crate tokio;
extern crate anyhow;
use logimesh::client::balance::RandomBalance;
use logimesh::client::discover::FixedDiscover;
use logimesh::client::lrcall::ConfigExt;
use logimesh::component::Endpoint;
use logimesh::context;
use service::{CompHello, World};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = CompHello
.logimesh_lrclient(
Endpoint::new("p.s.m"),
FixedDiscover::from_address(vec!["[::1]:8888".parse::<std::net::SocketAddrV6>().unwrap()]),
RandomBalance::new(),
ConfigExt::default(),
)
.await?;
// Send the request twice, just to be safe! ;)
let hello = tokio::select! {
hello1 = client.hello(context::current(), format!("{}1", flags.name)) => { hello1 }
hello2 = client.hello(context::current(), format!("{}2", flags.name)) => { hello2 }
};
match hello {
Ok(hello) => println!("{hello:?}"),
Err(e) => println!("{:?}", anyhow::Error::from(e)),
}
Ok(())
}