~repos /rust-embed

#rust#proc-macro#http

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.


71d66a06 Mark Drobnak

4 years ago
Merge pull request #149 from lazywalker/master
Files changed (3) hide show
  1. Cargo.toml +10 -2
  2. examples/axum.rs +74 -0
  3. readme.md +4 -0
Cargo.toml CHANGED
@@ -26,6 +26,11 @@ name = "rocket"
26
26
  path = "examples/rocket.rs"
27
27
  required-features = ["rocket"]
28
28
 
29
+ [[example]]
30
+ name = "axum"
31
+ path = "examples/axum.rs"
32
+ required-features = ["axum-ex"]
33
+
29
34
  [[test]]
30
35
  name = "interpolated_path"
31
36
  path = "tests/interpolated_path.rs"
@@ -44,9 +49,10 @@ rust-embed-utils = { version = "7.0.0", path = "utils"}
44
49
  include-flate = { version = "0.1", optional = true, features = ["stable"] }
45
50
  actix-web = { version = "3", default-features = false, optional = true }
46
51
  mime_guess = { version = "2", optional = true }
47
- tokio = { version = "0.2", optional = true, features = ["macros"] }
52
+ tokio = { version = "1.0", optional = true, features = ["macros", "rt-multi-thread"] }
48
- warp = { version = "0.2", default-features = false, optional = true }
53
+ warp = { version = "0.3", default-features = false, optional = true }
49
54
  rocket = { version = "0.4.5", default-features = false, optional = true }
55
+ axum = { version = "0.2.3", default-features = false, optional = true }
50
56
 
51
57
  [dev-dependencies]
52
58
  sha2 = "0.9"
@@ -59,6 +65,8 @@ include-exclude = ["rust-embed-impl/include-exclude", "rust-embed-utils/include-
59
65
  nightly = ["rocket"]
60
66
  actix = ["actix-web", "mime_guess"]
61
67
  warp-ex = ["warp", "tokio", "mime_guess"]
68
+ axum-ex = ["axum", "tokio", "mime_guess"]
69
+
62
70
 
63
71
  [badges]
64
72
  appveyor = { repository = "pyros2097/rust-embed" }
examples/axum.rs ADDED
@@ -0,0 +1,74 @@
1
+ use std::convert::Infallible;
2
+
3
+ use axum::{
4
+ body::{Bytes, Full},
5
+ handler::get,
6
+ http::{header, Response, StatusCode, Uri},
7
+ response::{Html, IntoResponse},
8
+ routing::Router,
9
+ };
10
+
11
+ use axum::handler::Handler;
12
+ use mime_guess;
13
+ use rust_embed::RustEmbed;
14
+ use std::net::SocketAddr;
15
+
16
+ #[tokio::main]
17
+ async fn main() {
18
+ // build our application with a route
19
+ let app = Router::new()
20
+ .route("/hello", get(helloworld))
21
+ // handle static files with rust_embed
22
+ .route("/", get(index_handler))
23
+ .route("/index.html", get(index_handler))
24
+ .route("/dist/", static_handler.into_service())
25
+ .or(static_handler.into_service());
26
+
27
+ // run it
28
+ let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
29
+ println!("listening on {}", addr);
30
+ axum::Server::bind(&addr).serve(app.into_make_service()).await.unwrap();
31
+ }
32
+
33
+ async fn helloworld() -> Html<&'static str> {
34
+ Html("<h1>Hello, World!</h1>")
35
+ }
36
+
37
+ // serve index.html from examples/public/index.html
38
+ async fn index_handler() -> impl IntoResponse {
39
+ static_handler("/index.html".parse::<Uri>().unwrap()).await
40
+ }
41
+
42
+ // static_handler is a handler that serves static files from the
43
+ async fn static_handler(uri: Uri) -> impl IntoResponse {
44
+ let mut path = uri.path().trim_start_matches('/').to_string();
45
+ if path.starts_with("dist/") {
46
+ path = path.replace("dist/", "");
47
+ }
48
+ StaticFile(path)
49
+ }
50
+
51
+ #[derive(RustEmbed)]
52
+ #[folder = "examples/public/"]
53
+ struct Asset;
54
+ pub struct StaticFile<T>(pub T);
55
+
56
+ impl<T> IntoResponse for StaticFile<T>
57
+ where
58
+ T: Into<String>,
59
+ {
60
+ type Body = Full<Bytes>;
61
+ type BodyError = Infallible;
62
+
63
+ fn into_response(self) -> Response<Self::Body> {
64
+ let path = self.0.into();
65
+ match Asset::get(path.as_str()) {
66
+ Some(content) => {
67
+ let body = content.data.into();
68
+ let mime = mime_guess::from_path(path).first_or_octet_stream();
69
+ Response::builder().header(header::CONTENT_TYPE, mime.as_ref()).body(body).unwrap()
70
+ }
71
+ None => Response::builder().status(StatusCode::NOT_FOUND).body(Full::from("404")).unwrap(),
72
+ }
73
+ }
74
+ }
readme.md CHANGED
@@ -159,6 +159,10 @@ Note: To run the `warp` example:
159
159
 
160
160
  `cargo run --example warp --features warp-ex`
161
161
 
162
+ Note: To run the `axum` example:
163
+
164
+ `cargo run --example axum --features axum-ex`
165
+
162
166
  ## Testing
163
167
 
164
168
  debug: `cargo test --test lib`