File tree Expand file tree Collapse file tree 4 files changed +81
-0
lines changed
Expand file tree Collapse file tree 4 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -624,6 +624,7 @@ workflows:
624624 - resources/opendal
625625 - services/shuttle-actix-web
626626 - services/shuttle-axum
627+ - services/shuttle-ntex
627628 - services/shuttle-poem
628629 - services/shuttle-rocket
629630 - services/shuttle-salvo
@@ -899,6 +900,7 @@ workflows:
899900 path :
900901 - services/shuttle-actix-web
901902 - services/shuttle-axum
903+ - services/shuttle-ntex
902904 - services/shuttle-poem
903905 - services/shuttle-rocket
904906 - services/shuttle-salvo
Original file line number Diff line number Diff line change 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.43.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" ] }
Original file line number Diff line number Diff line change 1+ ## Shuttle service integration for the Ntex Web framework
2+
3+ ### Example
4+
5+ ``` rust,no_run
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+ ```
Original file line number Diff line number Diff line change 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 > ;
You can’t perform that action at this time.
0 commit comments