Migrate to actix

This commit is contained in:
2022-03-30 10:14:39 +02:00
parent d75242d213
commit 70df96f286
7 changed files with 613 additions and 1392 deletions

View File

@ -1,22 +1,20 @@
use std::path::PathBuf;
use std::path::Path;
use actix_web::{HttpResponse, web};
use include_dir::{Dir, include_dir};
use rocket::http::{ContentType, Status};
/// Assets directory
static ASSETS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/assets");
#[get("/<file..>")]
pub fn assets_route(file: PathBuf) -> (Status, (ContentType, &'static [u8])) {
match ASSETS_DIR.get_file(file) {
None =>
(Status::NotFound, (ContentType::Text, "404 Not found".as_bytes())),
pub async fn assets_route(path: web::Path<String>) -> HttpResponse {
let path: &Path = path.as_ref().as_ref();
match ASSETS_DIR.get_file(path) {
None => HttpResponse::NotFound().body("404 Not found"),
Some(file) => {
(Status::Ok, (
ContentType::from_extension(file.path().extension().unwrap_or_default()
.to_string_lossy().as_ref())
.unwrap_or(ContentType::Binary),
file.contents()))
let res = mime_guess::from_path(path).first_or_octet_stream();
HttpResponse::Ok()
.content_type(res.to_string())
.body(file.contents())
}
}
}