Skip to content

Commit

Permalink
feat: add http server support
Browse files Browse the repository at this point in the history
  • Loading branch information
will-we committed Jan 12, 2025
1 parent 12d3eed commit 607557e
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 1 deletion.
187 changes: 187 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio = { version = "1.41.0", features = ["rt", "rt-multi-thread", "net", "fs",
axum = { version = "0.7.7", features = ["http2", "query", "tracing"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"
tower-http = { version = "0.6.2", features = ["compression-full", "cors", "trace", "fs"] }
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>hello world</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::io::stdin;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use tower_http::services::ServeDir;
use tracing::info;
use tracing::log::warn;

Expand Down Expand Up @@ -121,9 +122,16 @@ async fn process_http_requests(port: u16, path: PathBuf) -> anyhow::Result<()> {
port,
path.display()
);
let state = HttpServeState { path };
let state = HttpServeState { path: path.clone() };
let dir = ServeDir::new(path)
.append_index_html_on_directories(true)
.precompressed_gzip()
.precompressed_br()
.precompressed_deflate()
.precompressed_zstd();
let app = Router::new()
.route("/*path", get(file_handler))
.nest_service("/tower", dir)
.with_state(Arc::new(state));
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
let listener = tokio::net::TcpListener::bind(addr).await?;
Expand Down

0 comments on commit 607557e

Please sign in to comment.