Package the folder into the program body?

I wrote a rust back-end program by Rocket and a vue front-end program.
How can I pack my front-end program into rust executable file?
Only file is more elegant.
Can anyone help me?

1 Like

WOW!Thanks!
I will try it.

But if I want to use this font-end folder in rocket, I have to rewrite a new file server function.
The thing is, Ican not use include_dir's type Dir as a state.

For example
This doesn't work:

use rocket;
use std::path::PathBuf};
use rocket::fs::NamedFile;
use rocket::State;
use include_dir::{include_dir, Dir};

#[get("/<path..>")]
async fn index(file: &State<webfile>, path: PathBuf) -> Option<NamedFile> {
    NamedFile::open(file.get_file(path).unwrap()).await.ok()
}
#[launch]
fn rocket() -> _ {
    let webfile: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/static");
    rocket::build().manage(webfile)
}

rust says:

cannot find type `webfile` in this scope
not found in this scoperustcClick for full compiler diagnostic
main.rs: you might be missing a type parameter: `<webfile>`

It should be &State<Dir<'_>>, not &State<webfile>, which is a variable name from a different function, and not a type.

1 Like

Thanks! :laughing: :laughing: :laughing:

Emmmmm,I have a question again.

After use get_file(), i can get &File type file in embedding directory.
But how can I convert it init rocket::fs::NamedFile type so that I can return in my rocket server?

Just from looking at the API (I haven't used it): include_dir::File has a path method, and the path can be passed to NamedFile::open.

Using NamedFile at all would defeat the original goal, because NamedFile will always read from the file system (it's a wrapper around std::fs::File). You'd want to return the &[u8] from .contents() instead.

emmm
But rocket not provide api to send &[u8].....
Please help me :sob:

I haven't used Rocket myself, but the documentation says that &[u8] implements Responder. You can use that where you'd use the NamedFile.

You'll also want to set the Content-Type appropriately, which is usually taken from the file extension for static files, so actually use (ContentType, &[u8]) which is handled by this implementation.

1 Like

I'm trying my best :sob:.But it still not work
Thank you very match and i will try other way later
Before that I will read Rocket document again carefully :slightly_smiling_face:

1 Like