static assets caching

This commit is contained in:
realaravinth
2021-05-04 23:27:58 +05:30
parent 98719670df
commit f0254b3b77
5 changed files with 23 additions and 21 deletions

View File

@@ -14,14 +14,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::borrow::Cow;
use actix_web::body::Body;
use actix_web::{get, web, HttpResponse, Responder};
use actix_web::{get, http::header, web, HttpResponse, Responder};
use cache_buster::Files;
use mime_guess::from_path;
use rust_embed::RustEmbed;
use std::borrow::Cow;
use crate::CACHE_AGE;
#[derive(RustEmbed)]
#[folder = "static/"]
@@ -34,7 +35,11 @@ pub fn handle_embedded_file(path: &str) -> HttpResponse {
Cow::Borrowed(bytes) => bytes.into(),
Cow::Owned(bytes) => bytes.into(),
};
HttpResponse::Ok()
.set(header::CacheControl(vec![header::CacheDirective::MaxAge(
CACHE_AGE,
)]))
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(body)
}