~repos /rust-embed
git clone https://pyrossh.dev/repos/rust-embed.git
rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
9059a4a0
—
Luka Dornhecker 7 years ago
format project using cargo fmt
- examples/rocket.rs +6 -20
examples/rocket.rs
CHANGED
|
@@ -5,11 +5,11 @@ extern crate rocket_contrib;
|
|
|
5
5
|
#[macro_use]
|
|
6
6
|
extern crate rust_embed;
|
|
7
7
|
|
|
8
|
+
use rocket::http::{ContentType, Status};
|
|
8
|
-
use
|
|
9
|
+
use rocket::response;
|
|
9
10
|
use std::ffi::OsStr;
|
|
10
11
|
use std::io::Cursor;
|
|
11
|
-
use
|
|
12
|
+
use std::path::PathBuf;
|
|
12
|
-
use rocket::http::{ContentType, Status};
|
|
13
13
|
|
|
14
14
|
#[derive(RustEmbed)]
|
|
15
15
|
#[folder = "examples/public/"]
|
|
@@ -19,32 +19,18 @@ struct Asset;
|
|
|
19
19
|
fn index<'r>() -> response::Result<'r> {
|
|
20
20
|
Asset::get("index.html").map_or_else(
|
|
21
21
|
|| Err(Status::NotFound),
|
|
22
|
-
|d| {
|
|
23
|
-
|
|
22
|
+
|d| response::Response::build().header(ContentType::HTML).sized_body(Cursor::new(d)).ok(),
|
|
24
|
-
.header(ContentType::HTML)
|
|
25
|
-
.sized_body(Cursor::new(d))
|
|
26
|
-
.ok()
|
|
27
|
-
},
|
|
28
23
|
)
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
#[get("/dist/<file..>")]
|
|
32
27
|
fn dist<'r>(file: PathBuf) -> response::Result<'r> {
|
|
33
28
|
let filename = file.display().to_string();
|
|
34
|
-
let ext = file
|
|
35
|
-
.as_path()
|
|
36
|
-
.extension()
|
|
37
|
-
.and_then(OsStr::to_str)
|
|
38
|
-
|
|
29
|
+
let ext = file.as_path().extension().and_then(OsStr::to_str).expect("Could not get file extension");
|
|
39
30
|
let content_type = ContentType::from_extension(ext).expect("Could not get file content type");
|
|
40
31
|
Asset::get(&filename.clone()).map_or_else(
|
|
41
32
|
|| Err(Status::NotFound),
|
|
42
|
-
|d| {
|
|
43
|
-
|
|
33
|
+
|d| response::Response::build().header(content_type).sized_body(Cursor::new(d)).ok(),
|
|
44
|
-
.header(content_type)
|
|
45
|
-
.sized_body(Cursor::new(d))
|
|
46
|
-
.ok()
|
|
47
|
-
},
|
|
48
34
|
)
|
|
49
35
|
}
|
|
50
36
|
|