-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Shuttle service integration for the Ntex Web framework | ||
|
||
### Example | ||
|
||
```rust | ||
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()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |