Skip to content

Commit

Permalink
feat: Add crate salvo-craft
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Sep 23, 2024
1 parent 87f8836 commit 89f7ec5
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ salvo-rate-limiter = { version = "0.72.2", path = "crates/rate-limiter", default
salvo-serde-util = { version = "0.72.2", path = "crates/serde-util", default-features = true }
salvo-serve-static = { version = "0.72.2", path = "crates/serve-static", default-features = false }
salvo-session = { version = "0.72.2", path = "crates/session", default-features = false }
salvo-craft-macros = { version = "0.1.1", path = "crates/craft-macros", default-features = false }
salvo-craft = { version = "0.72.2", path = "crates/craft", default-features = false }
salvo-craft-macros = { version = "0.72.2", path = "crates/craft-macros", default-features = false }

aead = "0.5"
aes-gcm = "0.10"
Expand Down
3 changes: 3 additions & 0 deletions crates/craft-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ syn = { workspace = true, features = ["full", "parsing"] }
[dev-dependencies]
salvo = { path = "../salvo", features = ["oapi"] }
tokio.workspace = true

[lints]
workspace = true
2 changes: 2 additions & 0 deletions crates/craft-macros/examples/add.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(missing_docs)]

use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_craft_macros::craft;
Expand Down
28 changes: 28 additions & 0 deletions crates/craft/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "salvo-craft"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }
documentation = "https://docs.rs/salvo-craft"
readme = "README.md"
description = "Salvo Handler modular craft."
keywords = ["http", "async", "web", "framework", "server"]
categories = [
"web-programming::http-server",
"web-programming::websocket",
"network-programming",
"asynchronous",
]
authors = ["Andeya Lee <[email protected]>"]

[dependencies]
salvo-craft-macros = { workspace = true }

[dev-dependencies]
salvo = { path = "../salvo", features = ["oapi"] }
tokio.workspace = true

[lints]
workspace = true
66 changes: 66 additions & 0 deletions crates/craft/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# salvo-craft

[`Salvo`](https://github.com/salvo-rs/salvo) `Handler` modular craft macros.

[![Crates.io](https://img.shields.io/crates/v/salvo-craft)](https://crates.io/crates/salvo-craft)
[![Documentation](https://shields.io/docsrs/salvo-craft)](https://docs.rs/salvo-craft)

## `#[craft]`

`#[craft]` is an attribute macro used to batch convert methods in an `impl` block into [`Salvo`'s `Handler`](https://github.com/salvo-rs/salvo).

```rust
use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_craft::craft;
use std::sync::Arc;

#[tokio::main]
async fn main() {
let service = Arc::new(Service::new(1));
let router = Router::new()
.push(Router::with_path("add1").get(service.add1()))
.push(Router::with_path("add2").get(service.add2()))
.push(Router::with_path("add3").get(Service::add3()));
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;
}

#[derive(Clone)]
pub struct Service {
state: i64,
}

#[craft]
impl Service {
fn new(state: i64) -> Self {
Self { state }
}
/// doc line 1
/// doc line 2
#[craft(handler)]
fn add1(&self, left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
(self.state + *left + *right).to_string()
}
/// doc line 3
/// doc line 4
#[craft(handler)]
pub(crate) fn add2(
self: ::std::sync::Arc<Self>,
left: QueryParam<i64, true>,
right: QueryParam<i64, true>,
) -> String {
(self.state + *left + *right).to_string()
}
/// doc line 5
/// doc line 6
#[craft(handler)]
pub fn add3(left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
(*left + *right).to_string()
}
}
```

Sure, you can also replace `#[craft(handler)]` with `#[craft(endpoint(...))]`.

NOTE: If the receiver of a method is `&self`, you need to implement the `Clone` trait for the type.
55 changes: 55 additions & 0 deletions crates/craft/examples/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![allow(missing_docs)]

use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_craft::craft;
use std::sync::Arc;

#[tokio::main]
async fn main() {
let service = Arc::new(Service::new(1));
let router = Router::new()
.push(Router::with_path("add1").get(service.add1()))
.push(Router::with_path("add2").get(service.add2()))
.push(Router::with_path("add3").get(Service::add3()));
let doc = OpenApi::new("Example API", "0.0.1").merge_router(&router);
let router = router
.push(doc.into_router("/api-doc/openapi.json"))
.push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"));
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;
}

#[derive(Clone)]
pub struct Service {
state: i64,
}

#[craft]
impl Service {
fn new(state: i64) -> Self {
Self { state }
}
/// doc line 1
/// doc line 2
#[craft(handler)]
fn add1(&self, left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
(self.state + *left + *right).to_string()
}
/// doc line 3
/// doc line 4
#[craft(endpoint)]
pub(crate) fn add2(
self: ::std::sync::Arc<Self>,
left: QueryParam<i64, true>,
right: QueryParam<i64, true>
) -> String {
(self.state + *left + *right).to_string()
}
/// doc line 5
/// doc line 6
#[craft(endpoint(responses((status_code = 400, description = "Wrong request parameters."))))]
pub fn add3(left: QueryParam<i64, true>, right: QueryParam<i64, true>) -> String {
(*left + *right).to_string()
}
}
3 changes: 3 additions & 0 deletions crates/craft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! [`Salvo`](https://github.com/salvo-rs/salvo) `Handler` modular craft.
pub use salvo_craft_macros::*;
2 changes: 1 addition & 1 deletion crates/salvo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ salvo-serve-static = { workspace = true, features = ["full"], optional = true }
salvo-proxy = { workspace = true, features = ["full"], optional = true }
salvo-otel = { workspace = true, optional = true }
salvo-oapi = { workspace = true, features = ["full"], optional = true }
salvo-craft-macros = { workspace = true }
salvo-craft = { workspace = true }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crates/salvo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,5 @@ pub mod prelude {
pub use crate::oapi::redoc::ReDoc;
pub use crate::oapi::scalar::Scalar;
}
pub use salvo_craft_macros::*;
pub use salvo_craft::*;
}

0 comments on commit 89f7ec5

Please sign in to comment.