Skip to content

Commit 15afbd1

Browse files
committed
Introduce shuttle-ntex service
1 parent e33329b commit 15afbd1

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ workflows:
620620
- resources/opendal
621621
- services/shuttle-actix-web
622622
- services/shuttle-axum
623+
- services/shuttle-ntex
623624
- services/shuttle-poem
624625
- services/shuttle-rocket
625626
- services/shuttle-salvo
@@ -893,6 +894,7 @@ workflows:
893894
path:
894895
- services/shuttle-actix-web
895896
- services/shuttle-axum
897+
- services/shuttle-ntex
896898
- services/shuttle-poem
897899
- services/shuttle-rocket
898900
- services/shuttle-salvo

services/shuttle-ntex/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "shuttle-ntex"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "Apache-2.0"
6+
description = "Service implementation to run a Ntex webserver on shuttle"
7+
keywords = ["shuttle-service", "ntex"]
8+
9+
[workspace]
10+
11+
[dependencies]
12+
ntex = { version = "1.2.1"}
13+
shuttle-runtime = { path = "../../runtime", version = "0.42.0", default-features = false }
14+
num_cpus = "1.16.0"
15+
16+
[dev-dependencies]
17+
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] }

services/shuttle-ntex/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Shuttle service integration for the Ntex Web framework
2+
3+
### Example
4+
5+
```rust
6+
use ntex::web::{get, ServiceConfig};
7+
use shuttle_ntex::ShuttleNtexWeb;
8+
9+
#[get("/")]
10+
async fn hello_world() -> &'static str {
11+
"Hello World!"
12+
}
13+
14+
#[shuttle_runtime::main]
15+
async fn ntex_web() -> ShuttleNtexWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
16+
let config = move |cfg: &mut ServiceConfig| {
17+
cfg.service(hello_world);
18+
};
19+
20+
Ok(config.into())
21+
}
22+
```

services/shuttle-ntex/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![doc = include_str!("../README.md")]
2+
use std::net::SocketAddr;
3+
4+
/// A wrapper type for a closure that returns an [ntex::web::ServiceConfig] so we can implement
5+
/// [shuttle_runtime::Service] for it.
6+
#[derive(Clone)]
7+
pub struct NtexWebService<F>(pub F);
8+
9+
#[shuttle_runtime::async_trait]
10+
impl<F> shuttle_runtime::Service for NtexWebService<F>
11+
where
12+
F: FnOnce(&mut ntex::web::ServiceConfig) + Send + Clone + 'static,
13+
{
14+
async fn bind(mut self, addr: SocketAddr) -> Result<(), shuttle_runtime::Error> {
15+
// Start a worker for each cpu, but no more than 4.
16+
let worker_count = num_cpus::get().min(4);
17+
18+
let server =
19+
ntex::web::HttpServer::new(move || ntex::web::App::new().configure(self.0.clone()))
20+
.workers(worker_count)
21+
.bind(addr)?
22+
.run();
23+
24+
server.await.map_err(shuttle_runtime::CustomError::new)?;
25+
26+
Ok(())
27+
}
28+
}
29+
30+
impl<F> From<F> for NtexWebService<F>
31+
where
32+
F: FnOnce(&mut ntex::web::ServiceConfig) + Send + Clone + 'static,
33+
{
34+
fn from(service_config: F) -> Self {
35+
Self(service_config)
36+
}
37+
}
38+
39+
#[doc = include_str!("../README.md")]
40+
pub type ShuttleNtexWeb<F> = Result<NtexWebService<F>, shuttle_runtime::Error>;

0 commit comments

Comments
 (0)