From 1f8531711f28edfc2abfc54e9bc138b4a663d393 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Fri, 14 Jun 2024 10:43:41 +0700 Subject: [PATCH] Now it works correctly with react router --- src/main.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 504d819..b473b8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,10 @@ +use std::path::Path; + use actix_files; -use actix_web::{App, HttpServer}; +use actix_web::{ + dev::{ServiceRequest, ServiceResponse}, + App, HttpServer, +}; use clap::{arg, command, Parser}; #[derive(Parser, Debug)] @@ -28,10 +33,29 @@ async fn main() -> std::io::Result<()> { let bind_addr = format!("{}:{}", args.host, args.port); 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 || { - App::new().default_service( - actix_files::Files::new(&args.mount, args.serve_dir.as_str()) - .index_file(args.index_file.as_str()), + let index_path = index_path.to_owned(); + + 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)?