use actix_web::{ get, http::header::{HeaderValue, ETAG, IF_NONE_MATCH}, web, HttpRequest, HttpResponse, Responder, }; use lazy_static::lazy_static; use md5; static STYLE_ASSET: &'static str = include_str!(concat!(env!("OUT_DIR"), "/style.css")); lazy_static! { static ref STYLE_HASH: String = format!("{:?}", md5::compute(STYLE_ASSET)); static ref STYLE_HASH_HEADER: HeaderValue = HeaderValue::from_str(&STYLE_HASH).unwrap(); } #[get("/static/{asset}")] pub(crate) async fn get_static_asset(path: web::Path<String>, req: HttpRequest) -> impl Responder { let asset_name = path.into_inner(); match asset_name.as_str() { "style.css" => { if let Some(val) = req.headers().get(IF_NONE_MATCH) { if let Ok(etag) = val.to_str() { if etag == &*STYLE_HASH { return HttpResponse::NotModified().body(()); } } } let mut response = HttpResponse::Ok().body(STYLE_ASSET); response .headers_mut() .insert(ETAG, STYLE_HASH_HEADER.clone()); return response; } unknown_asset => HttpResponse::NotFound().body(format!("could not find {}", unknown_asset)), } }