@@ -2,12 +2,12 @@ use brotli::enc::BrotliEncoderParams;
2
2
use brotli:: BrotliCompress ;
3
3
use std:: collections:: HashMap ;
4
4
use std:: net:: SocketAddr ;
5
- use std:: path:: { Path , PathBuf } ;
5
+ use std:: path:: Path ;
6
6
use std:: str:: FromStr ;
7
7
use std:: sync:: atomic:: { AtomicBool , Ordering as AtomicOrdering } ;
8
8
use std:: sync:: Arc ;
9
9
use std:: time:: Instant ;
10
- use std:: { fmt, fs , str} ;
10
+ use std:: { fmt, str} ;
11
11
12
12
use futures:: { future:: FutureExt , stream:: StreamExt } ;
13
13
use headers:: { Authorization , CacheControl , ContentType , ETag , Header , HeaderMapExt , IfNoneMatch } ;
@@ -28,7 +28,7 @@ pub use crate::api::{
28
28
use crate :: db:: { self , ArtifactId } ;
29
29
use crate :: load:: { Config , SiteCtxt } ;
30
30
use crate :: request_handlers;
31
- use crate :: templates :: TemplateRenderer ;
31
+ use crate :: resources :: ResourceResolver ;
32
32
33
33
pub type Request = http:: Request < hyper:: Body > ;
34
34
pub type Response = http:: Response < hyper:: Body > ;
@@ -566,23 +566,12 @@ where
566
566
567
567
lazy_static:: lazy_static! {
568
568
static ref VERSION_UUID : Uuid = Uuid :: new_v4( ) ; // random UUID used as ETag for cache revalidation
569
- static ref TEMPLATES : TemplateRenderer = {
570
- let livereload = std:: env:: var( "LIVERELOAD" ) . is_ok( ) ;
571
- TemplateRenderer :: new( PathBuf :: from( "site/templates" ) , livereload) . unwrap( )
572
- } ;
569
+ static ref TEMPLATES : ResourceResolver = ResourceResolver :: new( ) . expect( "Cannot load resources" ) ;
573
570
}
574
571
575
572
/// Handle the case where the path is to a static file
576
573
async fn handle_fs_path ( req : & Request , path : & str ) -> Option < http:: Response < hyper:: Body > > {
577
- let fs_path = format ! (
578
- "site/static{}" ,
579
- match path {
580
- "" | "/" => "/index.html" ,
581
- _ => path,
582
- }
583
- ) ;
584
-
585
- if fs_path. contains ( "./" ) | fs_path. contains ( "../" ) {
574
+ if path. contains ( "./" ) | path. contains ( "../" ) {
586
575
return Some ( not_found ( ) ) ;
587
576
}
588
577
@@ -598,31 +587,26 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
598
587
}
599
588
}
600
589
601
- async fn render_page ( path : & str ) -> Vec < u8 > {
590
+ async fn resolve_template ( path : & str ) -> Vec < u8 > {
602
591
TEMPLATES
603
- . render ( & format ! ( "pages/{}" , path) )
592
+ . get_template ( & format ! ( "pages/{}" , path) )
604
593
. await
605
594
. unwrap ( )
606
- . into_bytes ( )
607
595
}
608
596
597
+ let relative_path = path. trim_start_matches ( "/" ) ;
609
598
let source = match path {
610
- "" | "/" | "/index.html" => render_page ( "graphs.html" ) . await ,
599
+ "" | "/" | "/index.html" => resolve_template ( "graphs.html" ) . await ,
611
600
"/bootstrap.html"
612
601
| "/compare.html"
613
602
| "/dashboard.html"
614
603
| "/detailed-query.html"
615
604
| "/help.html"
616
- | "/status.html" => render_page ( path. trim_start_matches ( "/" ) ) . await ,
617
- _ => {
618
- if !Path :: new ( & fs_path) . is_file ( ) {
619
- return None ;
620
- }
621
- fs:: read ( & fs_path) . unwrap ( )
622
- }
605
+ | "/status.html" => resolve_template ( relative_path) . await ,
606
+ _ => TEMPLATES . get_static_asset ( relative_path) ?,
623
607
} ;
624
608
625
- let p = Path :: new ( & fs_path ) ;
609
+ let p = Path :: new ( & path ) ;
626
610
match p. extension ( ) . and_then ( |x| x. to_str ( ) ) {
627
611
Some ( "html" ) => response = response. header_typed ( ContentType :: html ( ) ) ,
628
612
Some ( "png" ) => response = response. header_typed ( ContentType :: png ( ) ) ,
0 commit comments