Skip to content

Commit

Permalink
Add craft example
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Sep 23, 2024
1 parent 203be38 commit 1bb1b75
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
12 changes: 6 additions & 6 deletions crates/craft-macros/examples/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::sync::Arc;

#[tokio::main]
async fn main() {
let service = Arc::new(Service::new(1));
let opts = Arc::new(Opts::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()));
.push(Router::with_path("add1").get(opts.add1()))
.push(Router::with_path("add2").get(opts.add2()))
.push(Router::with_path("add3").get(Opts::add3()));
let doc = OpenApi::new("Example API", "0.0.1").merge_router(&router);
let router = router
.push(doc.into_router("/api-doc/openapi.json"))
Expand All @@ -21,12 +21,12 @@ async fn main() {
}

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

#[craft]
impl Service {
impl Opts {
fn new(state: i64) -> Self {
Self { state }
}
Expand Down
12 changes: 6 additions & 6 deletions crates/craft/examples/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::sync::Arc;

#[tokio::main]
async fn main() {
let service = Arc::new(Service::new(1));
let opts = Arc::new(Opts::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()));
.push(Router::with_path("add1").get(opts.add1()))
.push(Router::with_path("add2").get(opts.add2()))
.push(Router::with_path("add3").get(Opts::add3()));
let doc = OpenApi::new("Example API", "0.0.1").merge_router(&router);
let router = router
.push(doc.into_router("/api-doc/openapi.json"))
Expand All @@ -21,12 +21,12 @@ async fn main() {
}

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

#[craft]
impl Service {
impl Opts {
fn new(state: i64) -> Self {
Self { state }
}
Expand Down
11 changes: 11 additions & 0 deletions examples/craft/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "example-craft"
version.workspace = true
edition.workspace = true
publish.workspace = true

[dependencies]
salvo = { workspace = true, features = ["craft", "oapi"] }
tokio = { workspace = true, features = ["macros"] }
tracing.workspace = true
tracing-subscriber.workspace = true
52 changes: 52 additions & 0 deletions examples/craft/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use salvo::oapi::extract::*;
use salvo::prelude::*;
use std::sync::Arc;

#[tokio::main]
async fn main() {
let opts = Arc::new(Opts::new(1));
let router = Router::new()
.push(Router::with_path("add1").get(opts.add1()))
.push(Router::with_path("add2").get(opts.add2()))
.push(Router::with_path("add3").get(Opts::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 Opts {
state: i64,
}

#[craft]
impl Opts {
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()
}
}

0 comments on commit 1bb1b75

Please sign in to comment.