Now it works correctly with react router

This commit is contained in:
Andrew 2024-06-14 10:43:41 +07:00
parent df2a0f3c9a
commit 1f8531711f

View file

@ -1,5 +1,10 @@
use std::path::Path;
use actix_files; use actix_files;
use actix_web::{App, HttpServer}; use actix_web::{
dev::{ServiceRequest, ServiceResponse},
App, HttpServer,
};
use clap::{arg, command, Parser}; use clap::{arg, command, Parser};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -28,10 +33,29 @@ async fn main() -> std::io::Result<()> {
let bind_addr = format!("{}:{}", args.host, args.port); let bind_addr = format!("{}:{}", args.host, args.port);
println!("Listening on http://{}", bind_addr); println!("Listening on http://{}", bind_addr);
let index_path = Path::new(args.serve_dir.as_str())
.join(args.index_file.as_str())
.display()
.to_string();
let mount_point = args.mount.clone();
HttpServer::new(move || { HttpServer::new(move || {
App::new().default_service( let index_path = index_path.to_owned();
actix_files::Files::new(&args.mount, args.serve_dir.as_str())
.index_file(args.index_file.as_str()), App::new().service(
actix_files::Files::new(&mount_point, args.serve_dir.as_str())
.index_file(args.index_file.as_str())
.default_handler(move |req: ServiceRequest| {
let (http_req, _payload) = req.into_parts();
let index_path = index_path.to_owned();
async {
let response =
actix_files::NamedFile::open(index_path)?.into_response(&http_req);
Ok(ServiceResponse::new(http_req, response))
}
}),
) )
}) })
.bind(bind_addr)? .bind(bind_addr)?