~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.
fb2b653e
—
Bernardo 7 years ago
rustfmt
- examples/actix.rs +4 -13
- src/lib.rs +6 -2
examples/actix.rs
CHANGED
|
@@ -3,8 +3,8 @@ extern crate actix_web;
|
|
|
3
3
|
extern crate rust_embed;
|
|
4
4
|
extern crate mime_guess;
|
|
5
5
|
|
|
6
|
-
use actix_web::{App, HttpRequest, HttpResponse, server};
|
|
7
6
|
use actix_web::http::Method;
|
|
7
|
+
use actix_web::{server, App, HttpRequest, HttpResponse};
|
|
8
8
|
use mime_guess::guess_mime_type;
|
|
9
9
|
|
|
10
10
|
#[derive(RustEmbed)]
|
|
@@ -13,11 +13,7 @@ struct Asset;
|
|
|
13
13
|
|
|
14
14
|
fn handle_embedded_file(path: &str) -> HttpResponse {
|
|
15
15
|
match Asset::get(path) {
|
|
16
|
-
Some(content) => {
|
|
17
|
-
HttpResponse::Ok()
|
|
18
|
-
|
|
16
|
+
Some(content) => HttpResponse::Ok().content_type(guess_mime_type(path).as_ref()).body(content),
|
|
19
|
-
.body(content)
|
|
20
|
-
}
|
|
21
17
|
None => HttpResponse::NotFound().body("404 Not Found"),
|
|
22
18
|
}
|
|
23
19
|
}
|
|
@@ -32,13 +28,8 @@ fn dist(req: HttpRequest) -> HttpResponse {
|
|
|
32
28
|
}
|
|
33
29
|
|
|
34
30
|
fn main() {
|
|
35
|
-
server::new(|| {
|
|
36
|
-
|
|
31
|
+
server::new(|| App::new().route("/", Method::GET, index).route("/dist/{_:.*}", Method::GET, dist))
|
|
37
|
-
"/dist/{_:.*}",
|
|
38
|
-
Method::GET,
|
|
39
|
-
dist,
|
|
40
|
-
)
|
|
41
|
-
|
|
32
|
+
.bind("127.0.0.1:8000")
|
|
42
33
|
.unwrap()
|
|
43
34
|
.run();
|
|
44
35
|
}
|
src/lib.rs
CHANGED
|
@@ -7,9 +7,9 @@ extern crate syn;
|
|
|
7
7
|
extern crate walkdir;
|
|
8
8
|
|
|
9
9
|
use proc_macro::TokenStream;
|
|
10
|
-
use syn::*;
|
|
11
10
|
use quote::Tokens;
|
|
12
11
|
use std::path::Path;
|
|
12
|
+
use syn::*;
|
|
13
13
|
|
|
14
14
|
#[cfg(debug_assertions)]
|
|
15
15
|
fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
|
|
@@ -115,7 +115,11 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
|
|
|
115
115
|
}
|
|
116
116
|
};
|
|
117
117
|
if !Path::new(&folder_path).exists() {
|
|
118
|
+
panic!(
|
|
118
|
-
|
|
119
|
+
"#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",
|
|
120
|
+
folder_path,
|
|
121
|
+
std::env::current_dir().unwrap().to_str().unwrap()
|
|
122
|
+
);
|
|
119
123
|
};
|
|
120
124
|
generate_assets(ident, folder_path)
|
|
121
125
|
}
|