Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rust-embed-spa example #1317

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/rust-embed-spa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
12 changes: 12 additions & 0 deletions examples/rust-embed-spa/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rust_embed_spa"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.5.15"
mime_guess = "2.0.4"
rust-embed = "6.4.0"
tokio = { version = "1.20.1", features = ["full"] }
1 change: 1 addition & 0 deletions examples/rust-embed-spa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A small example for hosting single page application (SPA) files with [axum](https://github.com/tokio-rs/axum) and [rust-embed](https://github.com/pyrossh/rust-embed).
15 changes: 15 additions & 0 deletions examples/rust-embed-spa/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Single Page Application</title>
</head>

<body>
I'm the body!
<script src="./js/script.js"></script>
</body>

</html>
3 changes: 3 additions & 0 deletions examples/rust-embed-spa/assets/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var elem = document.createElement("div");
elem.innerHTML = "I'm the JS script!<br>" + new Date();
document.body.appendChild(elem);
73 changes: 73 additions & 0 deletions examples/rust-embed-spa/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use axum::{
body::{boxed, Full},
handler::Handler,
http::{header, StatusCode, Uri},
response::{IntoResponse, Response},
routing::Router,
};
use mime_guess;
use rust_embed::RustEmbed;
use std::net::SocketAddr;

static INDEX_HTML: &str = "index.html";

#[derive(RustEmbed)]
#[folder = "assets/"]
struct Assets;

#[tokio::main]
async fn main() {
let app = Router::new().fallback(static_handler.into_service());

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}

async fn static_handler(uri: Uri) -> impl IntoResponse {
let path = uri.path().trim_start_matches('/').to_string();

if path.is_empty() || path == INDEX_HTML {
return index_html().await.into_response();
}

match Assets::get(path.as_str()) {
Some(content) => {
let body = boxed(Full::from(content.data));
let mime = mime_guess::from_path(path).first_or_octet_stream();

Response::builder()
.header(header::CONTENT_TYPE, mime.as_ref())
.body(body)
.unwrap()
}
None => {
if path.contains('.') {
return not_found().await.into_response();
}

index_html().await.into_response()
}
}
}

async fn index_html() -> impl IntoResponse {
match Assets::get(INDEX_HTML) {
Some(content) => {
let body = boxed(Full::from(content.data));

Response::builder()
.header(header::CONTENT_TYPE, "text/html")
.body(body)
.unwrap()
}
None => not_found().await.into_response(),
}
}

async fn not_found() -> impl IntoResponse {
(StatusCode::NOT_FOUND, "404")
}