~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.
3a6139a8
—
AzureMarker 3 years ago
Update Rocket to 0.5.0-rc.1 along with the example
- Cargo.toml +1 -1
- examples/rocket.rs +18 -24
Cargo.toml
CHANGED
|
@@ -57,7 +57,7 @@ mime_guess = { version = "2", optional = true }
|
|
|
57
57
|
hex = { version = "0.4.3", optional = true }
|
|
58
58
|
tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
|
|
59
59
|
warp = { version = "0.3", default-features = false, optional = true }
|
|
60
|
-
rocket = { version = "0.
|
|
60
|
+
rocket = { version = "0.5.0-rc.1", default-features = false, optional = true }
|
|
61
61
|
axum = { version = "0.2.3", default-features = false, optional = true }
|
|
62
62
|
poem = { version = "1.0.8", default-features = false, optional = true }
|
|
63
63
|
|
examples/rocket.rs
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
#![feature(decl_macro, proc_macro_hygiene)]
|
|
2
1
|
#[macro_use]
|
|
3
2
|
extern crate rocket;
|
|
4
3
|
|
|
5
|
-
use rocket::http::
|
|
4
|
+
use rocket::http::ContentType;
|
|
6
|
-
use rocket::response;
|
|
5
|
+
use rocket::response::content::Html;
|
|
7
6
|
use rust_embed::RustEmbed;
|
|
8
7
|
|
|
8
|
+
use std::borrow::Cow;
|
|
9
9
|
use std::ffi::OsStr;
|
|
10
|
-
use std::io::Cursor;
|
|
11
10
|
use std::path::PathBuf;
|
|
12
11
|
|
|
13
12
|
#[derive(RustEmbed)]
|
|
@@ -15,30 +14,25 @@ use std::path::PathBuf;
|
|
|
15
14
|
struct Asset;
|
|
16
15
|
|
|
17
16
|
#[get("/")]
|
|
18
|
-
fn index
|
|
17
|
+
fn index() -> Option<Html<Cow<'static, [u8]>>> {
|
|
19
|
-
Asset::get("index.html")
|
|
18
|
+
let asset = Asset::get("index.html")?;
|
|
20
|
-
|
|
19
|
+
Some(Html(asset.data))
|
|
21
|
-
|d| response::Response::build().header(ContentType::HTML).sized_body(Cursor::new(d.data)).ok(),
|
|
22
|
-
)
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
#[get("/dist/<file..>")]
|
|
26
|
-
fn dist
|
|
23
|
+
fn dist(file: PathBuf) -> Option<(ContentType, Cow<'static, [u8]>)> {
|
|
27
24
|
let filename = file.display().to_string();
|
|
28
|
-
Asset::get(&filename)
|
|
25
|
+
let asset = Asset::get(&filename)?;
|
|
29
|
-
|| Err(Status::NotFound),
|
|
30
|
-
|d| {
|
|
31
|
-
|
|
26
|
+
let content_type = file
|
|
32
|
-
.as_path()
|
|
33
|
-
|
|
27
|
+
.extension()
|
|
34
|
-
|
|
28
|
+
.and_then(OsStr::to_str)
|
|
35
|
-
|
|
29
|
+
.and_then(ContentType::from_extension)
|
|
36
|
-
|
|
30
|
+
.unwrap_or(ContentType::Bytes);
|
|
31
|
+
|
|
37
|
-
|
|
32
|
+
Some((content_type, asset.data))
|
|
38
|
-
},
|
|
39
|
-
)
|
|
40
33
|
}
|
|
41
34
|
|
|
35
|
+
#[rocket::launch]
|
|
42
|
-
fn
|
|
36
|
+
fn rocket() -> _ {
|
|
43
|
-
rocket::
|
|
37
|
+
rocket::build().mount("/", routes![index, dist])
|
|
44
38
|
}
|