-
Notifications
You must be signed in to change notification settings - Fork 84
/
actix.rs
31 lines (26 loc) · 870 Bytes
/
actix.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use mime_guess::from_path;
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;
fn handle_embedded_file(path: &str) -> HttpResponse {
match Asset::get(path) {
Some(content) => HttpResponse::Ok()
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
#[actix_web::get("/")]
async fn index() -> impl Responder {
handle_embedded_file("index.html")
}
#[actix_web::get("/dist/{_:.*}")]
async fn dist(path: web::Path<String>) -> impl Responder {
handle_embedded_file(path.as_str())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index).service(dist)).bind("127.0.0.1:8000")?.run().await
}