@@ -2,12 +2,12 @@ use brotli::enc::BrotliEncoderParams;
22use brotli:: BrotliCompress ;
33use std:: collections:: HashMap ;
44use std:: net:: SocketAddr ;
5- use std:: path:: { Path , PathBuf } ;
5+ use std:: path:: Path ;
66use std:: str:: FromStr ;
77use std:: sync:: atomic:: { AtomicBool , Ordering as AtomicOrdering } ;
88use std:: sync:: Arc ;
99use std:: time:: Instant ;
10- use std:: { fmt, fs , str} ;
10+ use std:: { fmt, str} ;
1111
1212use futures:: { future:: FutureExt , stream:: StreamExt } ;
1313use headers:: { Authorization , CacheControl , ContentType , ETag , Header , HeaderMapExt , IfNoneMatch } ;
@@ -28,7 +28,7 @@ pub use crate::api::{
2828use crate :: db:: { self , ArtifactId } ;
2929use crate :: load:: { Config , SiteCtxt } ;
3030use crate :: request_handlers;
31- use crate :: templates :: TemplateRenderer ;
31+ use crate :: resources :: ResourceResolver ;
3232
3333pub type Request = http:: Request < hyper:: Body > ;
3434pub type Response = http:: Response < hyper:: Body > ;
@@ -566,23 +566,12 @@ where
566566
567567lazy_static:: lazy_static! {
568568 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" ) ;
573570}
574571
575572/// Handle the case where the path is to a static file
576573async 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 ( "../" ) {
586575 return Some ( not_found ( ) ) ;
587576 }
588577
@@ -598,31 +587,26 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
598587 }
599588 }
600589
601- async fn render_page ( path : & str ) -> Vec < u8 > {
590+ async fn resolve_template ( path : & str ) -> Vec < u8 > {
602591 TEMPLATES
603- . render ( & format ! ( "pages/{}" , path) )
592+ . get_template ( & format ! ( "pages/{}" , path) )
604593 . await
605594 . unwrap ( )
606- . into_bytes ( )
607595 }
608596
597+ let relative_path = path. trim_start_matches ( "/" ) ;
609598 let source = match path {
610- "" | "/" | "/index.html" => render_page ( "graphs.html" ) . await ,
599+ "" | "/" | "/index.html" => resolve_template ( "graphs.html" ) . await ,
611600 "/bootstrap.html"
612601 | "/compare.html"
613602 | "/dashboard.html"
614603 | "/detailed-query.html"
615604 | "/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) ?,
623607 } ;
624608
625- let p = Path :: new ( & fs_path ) ;
609+ let p = Path :: new ( & path ) ;
626610 match p. extension ( ) . and_then ( |x| x. to_str ( ) ) {
627611 Some ( "html" ) => response = response. header_typed ( ContentType :: html ( ) ) ,
628612 Some ( "png" ) => response = response. header_typed ( ContentType :: png ( ) ) ,
0 commit comments