Skip to content

Commit 6de38b3

Browse files
committed
新增静态资源处理实例
1 parent 11c3ccb commit 6de38b3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

examples/file_server/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "file_server"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
silent = { path = "../../silent" }
10+
async-trait = "0.1.68"
11+
tokio = { version = "1", features = ["full"] }

examples/file_server/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use silent::prelude::*;
2+
use std::sync::Arc;
3+
4+
fn main() {
5+
logger::fmt().init();
6+
if !std::path::Path::new("static").is_dir() {
7+
std::fs::create_dir("static").unwrap();
8+
std::fs::write(
9+
"./static/index.html",
10+
r#"<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<meta charset="utf-8">
14+
<title>Silent</title>
15+
</head>
16+
<body>
17+
18+
<h1>我的第一个标题</h1>
19+
20+
<p>我的第一个段落。</p>
21+
22+
</body>
23+
</html>"#,
24+
)
25+
.unwrap();
26+
}
27+
let mut route = Route::new("<path:**>");
28+
route
29+
.get_handler_mut()
30+
.insert(Method::GET, Arc::new(static_handler("static")));
31+
Server::new().bind_route(route).run();
32+
}

0 commit comments

Comments
 (0)