Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass the root_path as shared data in actix #114

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/server/src/handlers/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use actix_files::NamedFile;
use actix_web::{web::Data, HttpRequest};
use std::{
io::{Error, ErrorKind},
path::Path,
path::PathBuf,
};

/// Find a static HTML file in the `public` folder. This function is used
Expand All @@ -14,7 +14,7 @@ use std::{
///
/// If no file is present, it will try to get a default "public/404.html"
pub async fn handle_assets(req: &HttpRequest) -> Result<NamedFile, Error> {
let root_path = req.app_data::<Data<&Path>>().unwrap();
let root_path = req.app_data::<Data<PathBuf>>().unwrap();
let uri_path = req.path();

// File path. This is required for the wasm_handler as dynamic routes may capture static files
Expand Down
4 changes: 2 additions & 2 deletions crates/server/src/handlers/not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

use actix_files::NamedFile;
use actix_web::{web::Data, HttpRequest, HttpResponse};
use std::path::Path;
use std::path::PathBuf;

/// This method tries to render a custom 404 error file from the static
/// folder. If not, it will render an empty 404
pub async fn handle_not_found(req: &HttpRequest) -> HttpResponse {
let root_path = req.app_data::<Data<&Path>>().unwrap();
let root_path = req.app_data::<Data<PathBuf>>().unwrap();
let public_404_path = root_path.join("public").join("404.html");

if let Ok(file) = NamedFile::open_async(public_404_path).await {
Expand Down
3 changes: 2 additions & 1 deletion crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub async fn serve(
// Clean path before sending it to the service
.wrap(middleware::NormalizePath::trim())
.app_data(Data::clone(&routes))
.app_data(Data::clone(&data));
.app_data(Data::clone(&data))
.app_data(Data::clone(&root_path));

// Append routes to the current service
for route in routes.routes.iter() {
Expand Down