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

"GET /img/menu_logo.76408655.png HTTP/1.1" 404 #177

Closed
qxqxq opened this issue May 12, 2022 · 9 comments
Closed

"GET /img/menu_logo.76408655.png HTTP/1.1" 404 #177

qxqxq opened this issue May 12, 2022 · 9 comments

Comments

@qxqxq
Copy link

qxqxq commented May 12, 2022

"GET /img/menu_logo.76408655.png HTTP/1.1" 404
[2022-05-12T10:29:03Z INFO xdls::controller::statics] img-params===Path("menu_logo.76408655.png")
[2022-05-12T10:29:03Z INFO xdls::controller::statics] handle_embedded_file path====img/menu_logo.76408655.png

"GET /fonts/element-icons.535877f5.woff HTTP/1.1" 404
[2022-05-12T10:30:10Z INFO xdls::controller::statics] fonts-params===Path("element-icons.535877f5.woff")
[2022-05-12T10:30:10Z INFO xdls::controller::statics] handle_embedded_file path====fonts/element-icons.535877f5.woff

"GET /css/menu_logo.76408655.png HTTP/1.1" 200
[2022-05-12T10:27:20Z INFO xdls::controller::statics] handle_embedded_file path====css/menu_logo.76408655.png

"GET /img/logs.fa1e7ddd.css HTTP/1.1" 200
[2022-05-12T10:25:39Z INFO xdls::controller::statics] img-params===Path("logs.fa1e7ddd.css")
[2022-05-12T10:25:39Z INFO xdls::controller::statics] handle_embedded_file path====img/logs.fa1e7ddd.css

Currently all files in the Fonts folder will be 404.
Currently all files in the IMG folder except logs.fa1e7ddd.css are 404.

actix-web = "4.0.0"
rust-embed = "6.4.0"

MacBookPro:static qxq$ tree
.
├── css
│   ├── app.2748f048.css
│   ├── chunk-vendors.4ba11380.css
│   ├── chunk-vendors.4ba11380.css.gz
│   ├── config.39fdc8fc.css
│   ├── confighome.7ae7c0ef.css
│   ├── home.ad65ad08.css
│   ├── login.6d5cd834.css
│   ├── logs.fa1e7ddd.css
│   ├── menu_logo.76408655.png
│   └── system.9e578feb.css
├── favicon.ico
├── fonts
│   ├── element-icons.535877f5.woff
│   └── element-icons.732389de.ttf
├── img
│   ├── detection1.0aea7e16.jpg
│   ├── detection2.b13e79df.jpg
│   ├── icon_car.b91f3b70.png
│   ├── icon_radar.e1631f11.png
│   ├── img_1.8229e084.png
│   ├── logo.734d0fe1.png
│   ├── logs.fa1e7ddd.css
│   └── menu_logo.76408655.png
├── index.html
└── js
├── app.e012053f.js
├── app.e012053f.js.gz
├── chunk-vendors.17c8ec50.js
├── chunk-vendors.17c8ec50.js.gz
├── config.7094d2aa.js
├── config.7094d2aa.js.gz
├── config
home.fe5be4d4.js
├── confighome.fe5be4d4.js.gz
├── config
homeloginlogssystem.5fa4ace5.js
├── config
homeloginlogs~system.5fa4ace5.js.gz
├── home.17d84104.js
├── home.17d84104.js.gz
├── login.f2d94fbe.js
├── logs.d11034de.js
├── system.98ea62e3.js
└── system.98ea62e3.js.gz

4 directories, 38 files

#[derive(RustEmbed)]
#[folder = "static/"]
struct Asset;


fn handle_embedded_file(path: &str) -> HttpResponse {
    log::info!("handle_embedded_file path===={}", path);
    match Asset::get(path) {
        Some(content) => {
            // let body: Vec<u8> = match content.data {
            //     Cow::Borrowed(bytes) => {
            //         bytes.into()
            //     },
            //     Cow::Owned(bytes) => {
            //         bytes.into()
            //     }
            // };
            let body = content.data.into_owned();

            HttpResponse::Ok().content_type(from_path(path).first_or_octet_stream().as_ref()).body(body)
        }
        None => HttpResponse::NotFound().body("404 Not Found"),
    }
}

pub async fn index() -> HttpResponse {
    handle_embedded_file("index.html")
}

pub async fn css(params: web::Path<String>) -> HttpResponse {
    let path = format!("css/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn js(params: web::Path<String>) -> HttpResponse {
    let path = format!("js/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn fonts(params: web::Path<String>) -> HttpResponse {
    log::info!("fonts-params==={:?}", params);
    let path = format!("fonts/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn img(params: web::Path<String>) -> HttpResponse {
    log::info!("img-params==={:?}", params);
    let path = format!("img/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn ico() -> HttpResponse {
    handle_embedded_file("favicon.ico")
}

.service(
            web::resource("/")
                .route(web::get().to(statics::index)),
        )
        .route("/css/{path}", web::get().to(statics::css))
        .route("/js/{path}", web::get().to(statics::js))
        .route("/fonts/{path}", web::get().to(statics::fonts))
        .route("/img/{path}", web::get().to(statics::img))
        .route("/favicon.ico", web::get().to(statics::ico))
@pyrossh
Copy link
Owner

pyrossh commented May 12, 2022

@qxqxq have you taken a look at the example, You should be able to handle it with the single route handler. It seems to me that you could be missing the /static prefix in the path when calling handle_embedded_file.

@qxqxq
Copy link
Author

qxqxq commented May 12, 2022

@qxqxq have you taken a look at the example, You should be able to handle it with the single route handler. It seems to me that you could be missing the /static prefix in the path when calling handle_embedded_file.

How to handle it using a single routing handler, I don't know. In my opinion, the current code is just not very elegant. I don't think it's the absence of the /static prefix in the path. You can see my log in handle_Embedded_FILE. You can't explain why only a few files failed.

"GET /img/menu_logo.76408655.png HTTP/1.1" 404
[2022-05-12T10:29:03Z INFO xdls::controller::statics] img-params===Path("menu_logo.76408655.png")
[2022-05-12T10:29:03Z INFO xdls::controller::statics] handle_embedded_file path====img/menu_logo.76408655.png

"GET /img/logs.fa1e7ddd.css HTTP/1.1" 200
[2022-05-12T10:25:39Z INFO xdls::controller::statics] img-params===Path("logs.fa1e7ddd.css")
[2022-05-12T10:25:39Z INFO xdls::controller::statics] handle_embedded_file path====img/logs.fa1e7ddd.css

Take a look at the file in the static directory. Why one can succeed and the other can't.

@qxqxq
Copy link
Author

qxqxq commented May 12, 2022

@pyrossh There are 4 directories and 38 files in the static folder. There are 8 files in the IMG folder. Why only 7 files in img folder and 2 files in fonts folder will fail while the other 29 files are successful?

@qxqxq
Copy link
Author

qxqxq commented May 12, 2022

@pyrossh The example route prefix is dist, while my route prefix is JS and CSS and IMG and fonts.So I don't know how to handle it with the single route handle.

@pyrossh
Copy link
Owner

pyrossh commented May 13, 2022

In this case you would use /static instead of /dist and also you would need to prefix the html/webpack to use /static/js instead of just js a simpler approach. Maybe that would be easier to figure out this bug.
But anyways those files and images which are missing could have an issue in the path/name I'm guessing. A simple example repo to reproduce this will help debugging it.

@qxqxq
Copy link
Author

qxqxq commented May 13, 2022

@pyrossh All files were successful in Cargo Build. An error occurs only when cargo Build --release is used.

@qxqxq
Copy link
Author

qxqxq commented May 13, 2022

In this case you would use /static instead of /dist and also you would need to prefix the html/webpack to use /static/js instead of just js a simpler approach. Maybe that would be easier to figure out this bug. But anyways those files and images which are missing could have an issue in the path/name I'm guessing. A simple example repo to reproduce this will help debugging it.

I tried to work with HTML/Webpack in the same way as the example.

@pyrossh
Copy link
Owner

pyrossh commented May 13, 2022

Ahh ok it seems to fail in release only. Also it seems .css file in img directory works. Can you try with another css file in that directory as well as in the fonts directory and see if that works.
Seems to me maybe in release maybe it's failing to load other content types because css is a text format whereas images are binary format.

Also do disable your cache while testing maybe that would help in finding out.

@pyrossh
Copy link
Owner

pyrossh commented Nov 23, 2022

Closing this due to inactivity.

@pyrossh pyrossh closed this as completed Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants