File tree 4 files changed +81
-0
lines changed 4 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -620,6 +620,7 @@ workflows:
620
620
- resources/opendal
621
621
- services/shuttle-actix-web
622
622
- services/shuttle-axum
623
+ - services/shuttle-ntex
623
624
- services/shuttle-poem
624
625
- services/shuttle-rocket
625
626
- services/shuttle-salvo
@@ -893,6 +894,7 @@ workflows:
893
894
path :
894
895
- services/shuttle-actix-web
895
896
- services/shuttle-axum
897
+ - services/shuttle-ntex
896
898
- services/shuttle-poem
897
899
- services/shuttle-rocket
898
900
- 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.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" ] }
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
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