rust-embed v8.13.0

#rust#proc-macro#http

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.


44533ec — pyros2097 2018-03-13T19:55:42+05:30
add rocket example

Generated files not shown: Cargo.lock

Files changed (4) hide show
  1. Cargo.toml +5 -0
  2. examples/public/index.html +3 -3
  3. examples/rocket.rs +46 -0
  4. readme.md +1 -1
Cargo.toml CHANGED
@@ -16,3 +16,8 @@ plugin = true
16
16
  [dependencies]
17
17
  log = "0.4"
18
18
  walkdir = "2.1.4"
19
+
20
+ [dev-dependencies]
21
+ rocket = "0.3.6"
22
+ rocket_codegen = "0.3.6"
23
+ rocket_contrib = "0.3.6"
examples/public/index.html CHANGED
@@ -1,14 +1,14 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <link rel="stylesheet" type="text/css" href="examples/public/main.css" />
4
+ <link rel="stylesheet" type="text/css" href="dist/main.css" />
5
- <script src="examples/public/main.js"></script>
5
+ <script src="dist/main.js"></script>
6
6
  </head>
7
7
  <body>
8
8
  <div class="bor">
9
9
  <table bgcolor=#f0f0f0 cellspacing=0 class="hh">
10
10
  <tr>
11
- <td><img src="examples/public/images/llama.png" align="left"></td>
11
+ <td><img src="dist/images/llama.png" align="left"></td>
12
12
  <td>
13
13
  <table cellspacing=0>
14
14
  <tr><td><b>pyros2097</b></td></tr>
examples/rocket.rs ADDED
@@ -0,0 +1,46 @@
1
+ #![feature(test, plugin, decl_macro)]
2
+ #![plugin(rocket_codegen)]
3
+ extern crate rocket;
4
+ extern crate rocket_contrib;
5
+ extern crate rust_embed;
6
+
7
+ use std::path::PathBuf;
8
+ use std::io::Cursor;
9
+ use rocket::response;
10
+ use rocket::http::{ContentType, Status};
11
+ use rust_embed::*;
12
+
13
+ #[get("/")]
14
+ fn index<'r>() -> response::Result<'r> {
15
+ let asset = embed!("examples/public".to_owned());
16
+ asset("/index.html".to_owned()).map_or_else(
17
+ || Err(Status::NotFound),
18
+ |d| {
19
+ response::Response::build()
20
+ .header(ContentType::HTML)
21
+ .sized_body(Cursor::new(d))
22
+ .ok()
23
+ },
24
+ )
25
+ }
26
+
27
+ #[get("/dist/<file..>")]
28
+ fn dist<'r>(file: PathBuf) -> response::Result<'r> {
29
+ let filename = file.display().to_string();
30
+ let asset = embed!("examples/public/".to_owned());
31
+ // let ext = Path::new(&filename).extension().and_then(OsStr::to_str).expect("Could not get file extension");
32
+ // let content_type = ContentType::from_extension(ext).expect("Could not get file content type");
33
+ asset(filename.clone()).map_or_else(
34
+ || Err(Status::NotFound),
35
+ |d| {
36
+ response::Response::build()
37
+ .header(ContentType::HTML)
38
+ .sized_body(Cursor::new(d))
39
+ .ok()
40
+ },
41
+ )
42
+ }
43
+
44
+ fn main() {
45
+ rocket::ignite().mount("/", routes![index, dist]).launch();
46
+ }
readme.md CHANGED
@@ -44,7 +44,7 @@ fn main() {
44
44
 
45
45
  ## Examples
46
46
  To run the examples,
47
- `cargo run --example hyper`
47
+ `cargo run --example rocket`
48
48
 
49
49
  ## Testing
50
50
  debug: `cargo test --lib