Skip to content

Commit

Permalink
axum integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Sep 8, 2023
1 parent f08ee98 commit 7e1f850
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ members = [
]

[workspace.package]
version = "0.1.3"
version = "0.1.4"

[workspace.dependencies]
rscx = { path = "./rscx", version = "0.1.3" }
rscx-macros = { path = "./rscx-macros", version = "0.1.3" }
rscx = { path = "./rscx", version = "0.1.4" }
rscx-macros = { path = "./rscx-macros", version = "0.1.4" }
5 changes: 5 additions & 0 deletions rscx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ readme = "../README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = { version = "0.6.20", features = ["macros"], optional = true }
rscx-macros = { workspace = true }
tokio = { version = "1.32.0", features = ["full"] }
tokio-util = { version = "0.7.8", features = ["rt"], optional = true }
typed-builder = "0.16.0"

[features]
default = []
axum = ["dep:axum", "dep:tokio-util"]
26 changes: 26 additions & 0 deletions rscx/src/axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use axum::response::Html;
use std::{future::Future, sync::OnceLock, thread::available_parallelism};
use tokio_util::task::LocalPoolHandle;

use crate::context;

fn get_rendering_pool() -> LocalPoolHandle {
static LOCAL_POOL: OnceLock<LocalPoolHandle> = OnceLock::new();
LOCAL_POOL
.get_or_init(|| LocalPoolHandle::new(available_parallelism().map(Into::into).unwrap_or(1)))
.clone()
}

pub async fn render<F, O>(f: F) -> Html<O>
where
F: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
get_rendering_pool()
.spawn_pinned(move || async {
let h = context::spawn_local(f).await.unwrap();
Html(h)
})
.await
.unwrap()
}
2 changes: 2 additions & 0 deletions rscx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "axum")]
pub mod axum;
pub mod context;

pub extern crate rscx_macros;
Expand Down

0 comments on commit 7e1f850

Please sign in to comment.