rust-embed v8.13.0
git clone https://git.pyrossh.dev/rust-embed
rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
6351255
— pyros2097
2018-03-17T17:47:26+05:30
add logging in example
Generated files not shown: Cargo.lock
- Cargo.toml +1 -0
- examples/rocket.rs +23 -3
- src/lib.rs +1 -0
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,
|