-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add static-embed * embed * Revert "embed" This reverts commit 15ad319. * move
- Loading branch information
1 parent
3d1e46d
commit 556f667
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "static-embed" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
viz.workspace = true | ||
include_dir = "0.7.3" | ||
http-body-util = "0.1" | ||
|
||
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
h2 { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Document</title> | ||
<link rel="stylesheet" href="assets/style.css" /> | ||
</head> | ||
<body> | ||
<h2>hello word!</h2> | ||
<h3>hello word!</h3> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#![deny(warnings)] | ||
#![allow(clippy::unused_async)] | ||
|
||
use http_body_util::Full; | ||
use include_dir::{include_dir, Dir}; | ||
use std::{net::SocketAddr, sync::Arc}; | ||
use tokio::net::TcpListener; | ||
use viz::{ | ||
serve, IntoResponse, Request, RequestExt, Response, ResponseExt, Result, Router, StatusCode, | ||
Tree, | ||
}; | ||
|
||
const ASSETS: Dir = include_dir!("examples/static-files/static-embed/html"); // frontend dir | ||
|
||
pub async fn index(_: Request) -> Result<Response> { | ||
let file_content = ASSETS | ||
.get_file("index.html") | ||
.unwrap() | ||
.contents_utf8() | ||
.unwrap(); | ||
Ok(Response::html(file_content)) | ||
} | ||
|
||
pub async fn assets(req: Request) -> Result<Response> { | ||
let path = req.path().trim_start_matches('/'); | ||
if path.contains("..") { | ||
return Ok(StatusCode::FORBIDDEN.into_response()); | ||
} | ||
Ok(match ASSETS.get_file(path) { | ||
Some(v) => { | ||
let data = v.contents(); | ||
Response::builder().body(Full::from(data).into())? | ||
} | ||
None => StatusCode::NOT_FOUND.into_response(), | ||
}) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on http://{addr}"); | ||
|
||
let app = Router::new() | ||
.any("/*", |_| async { Ok(StatusCode::NOT_FOUND) }) | ||
.get("/", index) | ||
.get("/*", assets); | ||
let tree = Arc::new(Tree::from(app)); | ||
|
||
loop { | ||
let (stream, addr) = listener.accept().await?; | ||
let tree = tree.clone(); | ||
tokio::task::spawn(serve(stream, tree, Some(addr))); | ||
} | ||
} |