@@ -32,11 +32,19 @@ pub struct ResponseParts {
32
32
33
33
impl ResponseParts {
34
34
/// Insert a header, overwriting any previous value with the same key
35
- pub fn insert_header ( & mut self , key : header:: HeaderName , value : header:: HeaderValue ) {
35
+ pub fn insert_header (
36
+ & mut self ,
37
+ key : header:: HeaderName ,
38
+ value : header:: HeaderValue ,
39
+ ) {
36
40
self . headers . insert ( key, value) ;
37
41
}
38
42
/// Append a header, leaving any header with the same key intact
39
- pub fn append_header ( & mut self , key : header:: HeaderName , value : header:: HeaderValue ) {
43
+ pub fn append_header (
44
+ & mut self ,
45
+ key : header:: HeaderName ,
46
+ value : header:: HeaderValue ,
47
+ ) {
40
48
self . headers . append ( key, value) ;
41
49
}
42
50
}
@@ -60,13 +68,21 @@ impl ResponseOptions {
60
68
res_parts. status = Some ( status) ;
61
69
}
62
70
/// Insert a header, overwriting any previous value with the same key
63
- pub fn insert_header ( & self , key : header:: HeaderName , value : header:: HeaderValue ) {
71
+ pub fn insert_header (
72
+ & self ,
73
+ key : header:: HeaderName ,
74
+ value : header:: HeaderValue ,
75
+ ) {
64
76
let mut writeable = self . 0 . write ( ) ;
65
77
let res_parts = & mut * writeable;
66
78
res_parts. headers . insert ( key, value) ;
67
79
}
68
80
/// Append a header, leaving any header with the same key intact
69
- pub fn append_header ( & self , key : header:: HeaderName , value : header:: HeaderValue ) {
81
+ pub fn append_header (
82
+ & self ,
83
+ key : header:: HeaderName ,
84
+ value : header:: HeaderValue ,
85
+ ) {
70
86
let mut writeable = self . 0 . write ( ) ;
71
87
let res_parts = & mut * writeable;
72
88
res_parts. headers . append ( key, value) ;
@@ -81,7 +97,8 @@ pub fn redirect(cx: leptos::Scope, path: &str) {
81
97
response_options. set_status ( StatusCode :: FOUND ) ;
82
98
response_options. insert_header (
83
99
header:: LOCATION ,
84
- header:: HeaderValue :: from_str ( path) . expect ( "Failed to create HeaderValue" ) ,
100
+ header:: HeaderValue :: from_str ( path)
101
+ . expect ( "Failed to create HeaderValue" ) ,
85
102
) ;
86
103
}
87
104
@@ -173,7 +190,8 @@ pub fn handle_server_fns_with_context(
173
190
174
191
match server_fn ( cx, body) . await {
175
192
Ok ( serialized) => {
176
- let res_options = use_context :: < ResponseOptions > ( cx) . unwrap ( ) ;
193
+ let res_options =
194
+ use_context :: < ResponseOptions > ( cx) . unwrap ( ) ;
177
195
178
196
// clean up the scope, which we only needed to run the server fn
179
197
disposer. dispose ( ) ;
@@ -183,7 +201,8 @@ pub fn handle_server_fns_with_context(
183
201
let mut res_parts = res_options. 0 . write ( ) ;
184
202
185
203
if accept_header == Some ( "application/json" )
186
- || accept_header == Some ( "application/x-www-form-urlencoded" )
204
+ || accept_header
205
+ == Some ( "application/x-www-form-urlencoded" )
187
206
|| accept_header == Some ( "application/cbor" )
188
207
{
189
208
res = HttpResponse :: Ok ( ) ;
@@ -221,7 +240,9 @@ pub fn handle_server_fns_with_context(
221
240
res. body ( Bytes :: from ( data) )
222
241
}
223
242
Payload :: Url ( data) => {
224
- res. content_type ( "application/x-www-form-urlencoded" ) ;
243
+ res. content_type (
244
+ "application/x-www-form-urlencoded" ,
245
+ ) ;
225
246
res. body ( data)
226
247
}
227
248
Payload :: Json ( data) => {
@@ -230,13 +251,15 @@ pub fn handle_server_fns_with_context(
230
251
}
231
252
}
232
253
}
233
- Err ( e) => HttpResponse :: InternalServerError ( ) . body ( e. to_string ( ) ) ,
254
+ Err ( e) => HttpResponse :: InternalServerError ( )
255
+ . body ( e. to_string ( ) ) ,
234
256
}
235
257
} else {
236
258
HttpResponse :: BadRequest ( ) . body ( format ! (
237
259
"Could not find a server function at the route {:?}. \
238
- \n \n It's likely that you need to call ServerFn::register() on the \
239
- server function type, somewhere in your `main` function.",
260
+ \n \n It's likely that you need to call \
261
+ ServerFn::register() on the server function type, \
262
+ somewhere in your `main` function.",
240
263
req. path( )
241
264
) )
242
265
}
@@ -256,13 +279,13 @@ pub fn handle_server_fns_with_context(
256
279
///
257
280
/// This can then be set up at an appropriate route in your application:
258
281
/// ```
259
- /// use actix_web::{HttpServer, App };
282
+ /// use actix_web::{App, HttpServer };
260
283
/// use leptos::*;
261
- /// use std::{env,net::SocketAddr};
284
+ /// use std::{env, net::SocketAddr};
262
285
///
263
286
/// #[component]
264
287
/// fn MyApp(cx: Scope) -> impl IntoView {
265
- /// view! { cx, <main>"Hello, world!"</main> }
288
+ /// view! { cx, <main>"Hello, world!"</main> }
266
289
/// }
267
290
///
268
291
/// # if false { // don't actually try to run a server in a doctest...
@@ -272,11 +295,17 @@ pub fn handle_server_fns_with_context(
272
295
/// let addr = conf.leptos_options.site_addr.clone();
273
296
/// HttpServer::new(move || {
274
297
/// let leptos_options = &conf.leptos_options;
275
- ///
298
+ ///
276
299
/// App::new()
277
300
/// // {tail:.*} passes the remainder of the URL as the route
278
301
/// // the actual routing will be handled by `leptos_router`
279
- /// .route("/{tail:.*}", leptos_actix::render_app_to_stream(leptos_options.to_owned(), |cx| view! { cx, <MyApp/> }))
302
+ /// .route(
303
+ /// "/{tail:.*}",
304
+ /// leptos_actix::render_app_to_stream(
305
+ /// leptos_options.to_owned(),
306
+ /// |cx| view! { cx, <MyApp/> },
307
+ /// ),
308
+ /// )
280
309
/// })
281
310
/// .bind(&addr)?
282
311
/// .run()
@@ -353,14 +382,14 @@ where
353
382
///
354
383
/// This can then be set up at an appropriate route in your application:
355
384
/// ```
356
- /// use actix_web::{HttpServer, App };
385
+ /// use actix_web::{App, HttpServer };
357
386
/// use leptos::*;
358
- /// use std::{env,net::SocketAddr};
359
387
/// use leptos_actix::DataResponse;
388
+ /// use std::{env, net::SocketAddr};
360
389
///
361
390
/// #[component]
362
391
/// fn MyApp(cx: Scope, data: &'static str) -> impl IntoView {
363
- /// view! { cx, <main>"Hello, world!"</main> }
392
+ /// view! { cx, <main>"Hello, world!"</main> }
364
393
/// }
365
394
///
366
395
/// # if false { // don't actually try to run a server in a doctest...
@@ -370,14 +399,21 @@ where
370
399
/// let addr = conf.leptos_options.site_addr.clone();
371
400
/// HttpServer::new(move || {
372
401
/// let leptos_options = &conf.leptos_options;
373
- ///
402
+ ///
374
403
/// App::new()
375
404
/// // {tail:.*} passes the remainder of the URL as the route
376
405
/// // the actual routing will be handled by `leptos_router`
377
- /// .route("/{tail:.*}", leptos_actix::render_preloaded_data_app(
378
- /// leptos_options.to_owned(),
379
- /// |req| async move { Ok(DataResponse::Data("async func that can preload data")) },
380
- /// |cx, data| view! { cx, <MyApp data/> })
406
+ /// .route(
407
+ /// "/{tail:.*}",
408
+ /// leptos_actix::render_preloaded_data_app(
409
+ /// leptos_options.to_owned(),
410
+ /// |req| async move {
411
+ /// Ok(DataResponse::Data(
412
+ /// "async func that can preload data",
413
+ /// ))
414
+ /// },
415
+ /// |cx, data| view! { cx, <MyApp data/> },
416
+ /// ),
381
417
/// )
382
418
/// })
383
419
/// .bind(&addr)?
@@ -430,7 +466,11 @@ where
430
466
} )
431
467
}
432
468
433
- fn provide_contexts ( cx : leptos:: Scope , req : & HttpRequest , res_options : ResponseOptions ) {
469
+ fn provide_contexts (
470
+ cx : leptos:: Scope ,
471
+ req : & HttpRequest ,
472
+ res_options : ResponseOptions ,
473
+ ) {
434
474
let path = leptos_corrected_path ( req) ;
435
475
436
476
let integration = ServerIntegration { path } ;
@@ -457,25 +497,27 @@ async fn stream_app(
457
497
res_options : ResponseOptions ,
458
498
additional_context : impl Fn ( leptos:: Scope ) + ' static + Clone + Send ,
459
499
) -> HttpResponse < BoxBody > {
460
- let ( stream, runtime, scope) = render_to_stream_with_prefix_undisposed_with_context (
461
- app,
462
- move |cx| {
463
- let meta = use_context :: < MetaContext > ( cx) ;
464
- let head = meta
465
- . as_ref ( )
466
- . map ( |meta| meta. dehydrate ( ) )
467
- . unwrap_or_default ( ) ;
468
- let body_meta = meta
469
- . as_ref ( )
470
- . and_then ( |meta| meta. body . as_string ( ) )
471
- . unwrap_or_default ( ) ;
472
- format ! ( "{head}</head><body{body_meta}>" ) . into ( )
473
- } ,
474
- additional_context,
475
- ) ;
500
+ let ( stream, runtime, scope) =
501
+ render_to_stream_with_prefix_undisposed_with_context (
502
+ app,
503
+ move |cx| {
504
+ let meta = use_context :: < MetaContext > ( cx) ;
505
+ let head = meta
506
+ . as_ref ( )
507
+ . map ( |meta| meta. dehydrate ( ) )
508
+ . unwrap_or_default ( ) ;
509
+ let body_meta = meta
510
+ . as_ref ( )
511
+ . and_then ( |meta| meta. body . as_string ( ) )
512
+ . unwrap_or_default ( ) ;
513
+ format ! ( "{head}</head><body{body_meta}>" ) . into ( )
514
+ } ,
515
+ additional_context,
516
+ ) ;
476
517
477
518
let cx = leptos:: Scope { runtime, id : scope } ;
478
- let ( head, tail) = html_parts ( options, use_context :: < MetaContext > ( cx) . as_ref ( ) ) ;
519
+ let ( head, tail) =
520
+ html_parts ( options, use_context :: < MetaContext > ( cx) . as_ref ( ) ) ;
479
521
480
522
let mut stream = Box :: pin (
481
523
futures:: stream:: once ( async move { head. clone ( ) } )
@@ -493,11 +535,13 @@ async fn stream_app(
493
535
494
536
let res_options = res_options. 0 . read ( ) ;
495
537
496
- let ( status, mut headers) = ( res_options. status , res_options. headers . clone ( ) ) ;
538
+ let ( status, mut headers) =
539
+ ( res_options. status , res_options. headers . clone ( ) ) ;
497
540
let status = status. unwrap_or_default ( ) ;
498
541
499
542
let complete_stream =
500
- futures:: stream:: iter ( [ first_chunk. unwrap ( ) , second_chunk. unwrap ( ) ] ) . chain ( stream) ;
543
+ futures:: stream:: iter ( [ first_chunk. unwrap ( ) , second_chunk. unwrap ( ) ] )
544
+ . chain ( stream) ;
501
545
let mut res = HttpResponse :: Ok ( )
502
546
. content_type ( "text/html" )
503
547
. streaming ( complete_stream) ;
@@ -514,7 +558,10 @@ async fn stream_app(
514
558
res
515
559
}
516
560
517
- fn html_parts ( options : & LeptosOptions , meta_context : Option < & MetaContext > ) -> ( String , String ) {
561
+ fn html_parts (
562
+ options : & LeptosOptions ,
563
+ meta_context : Option < & MetaContext > ,
564
+ ) -> ( String , String ) {
518
565
// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
519
566
// we add _bg to the wasm files if cargo-leptos doesn't set the env var LEPTOS_OUTPUT_NAME
520
567
// Otherwise we need to add _bg because wasm_pack always does. This is not the same as options.output_name, which is set regardless
@@ -578,7 +625,9 @@ fn html_parts(options: &LeptosOptions, meta_context: Option<&MetaContext>) -> (S
578
625
/// Generates a list of all routes defined in Leptos's Router in your app. We can then use this to automatically
579
626
/// create routes in Actix's App without having to use wildcard matching or fallbacks. Takes in your root app Element
580
627
/// as an argument so it can walk you app tree. This version is tailored to generated Actix compatible paths.
581
- pub fn generate_route_list < IV > ( app_fn : impl FnOnce ( leptos:: Scope ) -> IV + ' static ) -> Vec < String >
628
+ pub fn generate_route_list < IV > (
629
+ app_fn : impl FnOnce ( leptos:: Scope ) -> IV + ' static ,
630
+ ) -> Vec < String >
582
631
where
583
632
IV : IntoView + ' static ,
584
633
{
@@ -658,7 +707,12 @@ pub trait LeptosRoutes {
658
707
/// to those paths to Leptos's renderer.
659
708
impl < T > LeptosRoutes for actix_web:: App < T >
660
709
where
661
- T : ServiceFactory < ServiceRequest , Config = ( ) , Error = Error , InitError = ( ) > ,
710
+ T : ServiceFactory <
711
+ ServiceRequest ,
712
+ Config = ( ) ,
713
+ Error = Error ,
714
+ InitError = ( ) ,
715
+ > ,
662
716
{
663
717
fn leptos_routes < IV > (
664
718
self ,
@@ -671,7 +725,10 @@ where
671
725
{
672
726
let mut router = self ;
673
727
for path in paths. iter ( ) {
674
- router = router. route ( path, render_app_to_stream ( options. clone ( ) , app_fn. clone ( ) ) ) ;
728
+ router = router. route (
729
+ path,
730
+ render_app_to_stream ( options. clone ( ) , app_fn. clone ( ) ) ,
731
+ ) ;
675
732
}
676
733
router
677
734
}
@@ -693,7 +750,11 @@ where
693
750
for path in paths. iter ( ) {
694
751
router = router. route (
695
752
path,
696
- render_preloaded_data_app ( options. clone ( ) , data_fn. clone ( ) , app_fn. clone ( ) ) ,
753
+ render_preloaded_data_app (
754
+ options. clone ( ) ,
755
+ data_fn. clone ( ) ,
756
+ app_fn. clone ( ) ,
757
+ ) ,
697
758
) ;
698
759
}
699
760
router
0 commit comments