~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.
63512550
—
pyros2097 7 years ago
add logging in example
- Cargo.lock +10 -0
- Cargo.toml +1 -0
- examples/rocket.rs +23 -3
- src/lib.rs +1 -0
Cargo.lock
CHANGED
|
@@ -234,6 +234,14 @@ dependencies = [
|
|
|
234
234
|
"num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
235
235
|
]
|
|
236
236
|
|
|
237
|
+
[[package]]
|
|
238
|
+
name = "fern"
|
|
239
|
+
version = "0.5.4"
|
|
240
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
241
|
+
dependencies = [
|
|
242
|
+
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
243
|
+
]
|
|
244
|
+
|
|
237
245
|
[[package]]
|
|
238
246
|
name = "filetime"
|
|
239
247
|
version = "0.1.15"
|
|
@@ -703,6 +711,7 @@ dependencies = [
|
|
|
703
711
|
name = "rust-embed"
|
|
704
712
|
version = "0.5.2"
|
|
705
713
|
dependencies = [
|
|
714
|
+
"fern 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
706
715
|
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
707
716
|
"rocket 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
708
717
|
"rocket_codegen 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
@@ -1024,6 +1033,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
|
1024
1033
|
"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
|
|
1025
1034
|
"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
|
|
1026
1035
|
"checksum enum_primitive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180"
|
|
1036
|
+
"checksum fern 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "50475651fccc56343c766e4d1889428ea753308a977e1315db358ada28cc8c9d"
|
|
1027
1037
|
"checksum filetime 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "714653f3e34871534de23771ac7b26e999651a0a228f47beb324dfdf1dd4b10f"
|
|
1028
1038
|
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
|
1029
1039
|
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
Cargo.toml
CHANGED
|
@@ -19,6 +19,7 @@ rocket = "0.3.6"
|
|
|
19
19
|
rocket_codegen = "0.3.6"
|
|
20
20
|
rocket_contrib = "0.3.6"
|
|
21
21
|
rouille = "2.1.0"
|
|
22
|
+
fern = "0.5"
|
|
22
23
|
|
|
23
24
|
[badges]
|
|
24
25
|
appveyor = { repository = "pyros2097/rust-embed" }
|
examples/rocket.rs
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
extern crate rocket;
|
|
4
4
|
extern crate rocket_contrib;
|
|
5
5
|
extern crate rust_embed;
|
|
6
|
+
extern crate fern;
|
|
7
|
+
extern crate log;
|
|
6
8
|
|
|
7
9
|
use std::path::PathBuf;
|
|
8
10
|
use std::ffi::OsStr;
|
|
@@ -28,7 +30,11 @@ fn index<'r>(asset: State<Asset>) -> response::Result<'r> {
|
|
|
28
30
|
#[get("/dist/<file..>")]
|
|
29
31
|
fn dist<'r>(asset: State<Asset>, file: PathBuf) -> response::Result<'r> {
|
|
30
32
|
let filename = file.display().to_string();
|
|
33
|
+
let ext = file
|
|
34
|
+
.as_path()
|
|
35
|
+
.extension()
|
|
36
|
+
.and_then(OsStr::to_str)
|
|
31
|
-
|
|
37
|
+
.expect("Could not get file extension");
|
|
32
38
|
let content_type = ContentType::from_extension(ext).expect("Could not get file content type");
|
|
33
39
|
asset(filename.clone()).map_or_else(
|
|
34
40
|
|| Err(Status::NotFound),
|
|
@@ -42,8 +48,22 @@ fn dist<'r>(asset: State<Asset>, file: PathBuf) -> response::Result<'r> {
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
fn main() {
|
|
51
|
+
fern::Dispatch::new()
|
|
52
|
+
.format(move |out, message, record| {
|
|
53
|
+
out.finish(format_args!(
|
|
54
|
+
"[{}][{}] {}",
|
|
55
|
+
record.target(),
|
|
56
|
+
record.level(),
|
|
57
|
+
message
|
|
58
|
+
))
|
|
59
|
+
})
|
|
60
|
+
.level(log::LevelFilter::Info)
|
|
61
|
+
.chain(std::io::stdout())
|
|
62
|
+
.apply()
|
|
63
|
+
.expect("Could not initialize logger");
|
|
45
64
|
let asset = embed!("examples/public/".to_owned());
|
|
46
65
|
rocket::ignite()
|
|
47
|
-
|
|
66
|
+
.manage(asset)
|
|
48
|
-
|
|
67
|
+
.mount("/", routes![index, dist])
|
|
68
|
+
.launch();
|
|
49
69
|
}
|
src/lib.rs
CHANGED
|
@@ -57,6 +57,7 @@ pub fn generate_assets<'a>(parent_path: String) -> Asset {
|
|
|
57
57
|
map.insert(key, data);
|
|
58
58
|
}
|
|
59
59
|
Box::new(move |file_path| {
|
|
60
|
+
info!("asset from cache -> {}", file_path);
|
|
60
61
|
match map.get(&file_path) {
|
|
61
62
|
Some(s) => Some(s.to_vec()),
|
|
62
63
|
None => None,
|