Skip to content

Commit

Permalink
Introduce shuttle-ntex service
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlospt committed Apr 1, 2024
1 parent e33329b commit bfe59f1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ workflows:
- resources/opendal
- services/shuttle-actix-web
- services/shuttle-axum
- services/shuttle-ntex
- services/shuttle-poem
- services/shuttle-rocket
- services/shuttle-salvo
Expand Down Expand Up @@ -893,6 +894,7 @@ workflows:
path:
- services/shuttle-actix-web
- services/shuttle-axum
- services/shuttle-ntex
- services/shuttle-poem
- services/shuttle-rocket
- services/shuttle-salvo
Expand Down
17 changes: 17 additions & 0 deletions services/shuttle-ntex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "shuttle-ntex"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
description = "Service implementation to run a Ntex webserver on shuttle"
keywords = ["shuttle-service", "ntex"]

[workspace]

[dependencies]
ntex = { version = "1.2.1"}
shuttle-runtime = { path = "../../runtime", version = "0.42.0", default-features = false }
num_cpus = "1.16.0"

[dev-dependencies]
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] }
22 changes: 22 additions & 0 deletions services/shuttle-ntex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Shuttle service integration for the Ntex Web framework

### Example

```rust,no_run
use ntex::web::{get, ServiceConfig};
use shuttle_ntex::ShuttleNtexWeb;
#[get("/")]
async fn hello_world() -> &'static str {
"Hello World!"
}
#[shuttle_runtime::main]
async fn ntex_web() -> ShuttleNtexWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
let config = move |cfg: &mut ServiceConfig| {
cfg.service(hello_world);
};
Ok(config.into())
}
```
40 changes: 40 additions & 0 deletions services/shuttle-ntex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![doc = include_str!("../README.md")]
use std::net::SocketAddr;

/// A wrapper type for a closure that returns an [ntex::web::ServiceConfig] so we can implement
/// [shuttle_runtime::Service] for it.
#[derive(Clone)]
pub struct NtexWebService<F>(pub F);

#[shuttle_runtime::async_trait]
impl<F> shuttle_runtime::Service for NtexWebService<F>
where
F: FnOnce(&mut ntex::web::ServiceConfig) + Send + Clone + 'static,
{
async fn bind(mut self, addr: SocketAddr) -> Result<(), shuttle_runtime::Error> {
// Start a worker for each cpu, but no more than 4.
let worker_count = num_cpus::get().min(4);

let server =
ntex::web::HttpServer::new(move || ntex::web::App::new().configure(self.0.clone()))
.workers(worker_count)
.bind(addr)?
.run();

server.await.map_err(shuttle_runtime::CustomError::new)?;

Ok(())
}
}

impl<F> From<F> for NtexWebService<F>
where
F: FnOnce(&mut ntex::web::ServiceConfig) + Send + Clone + 'static,
{
fn from(service_config: F) -> Self {
Self(service_config)
}
}

#[doc = include_str!("../README.md")]
pub type ShuttleNtexWeb<F> = Result<NtexWebService<F>, shuttle_runtime::Error>;

0 comments on commit bfe59f1

Please sign in to comment.