-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support http static file server
- Loading branch information
Showing
10 changed files
with
826 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,17 @@ | ||
use super::verify_path; | ||
use clap::Parser; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Parser)] | ||
pub enum HttpSubCommand { | ||
#[command(about = "Serve a directory over HTTP")] | ||
Serve(HttpServeOpts), | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct HttpServeOpts { | ||
#[arg(short, long, value_parser = verify_path, default_value = ".")] | ||
pub dir: PathBuf, | ||
#[arg(short, long, default_value_t = 8080)] | ||
pub port: u16, | ||
} |
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
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
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,54 @@ | ||
use anyhow::Result; | ||
use axum::{ | ||
extract::{Path, State}, | ||
http::StatusCode, | ||
routing::get, | ||
Router, | ||
}; | ||
use std::{net::SocketAddr, path::PathBuf, sync::Arc}; | ||
use tracing::{info, warn}; | ||
|
||
#[derive(Debug)] | ||
struct HttpServeState { | ||
path: PathBuf, | ||
} | ||
|
||
pub async fn process_http_serve(path: PathBuf, port: u16) -> Result<()> { | ||
let addr = SocketAddr::from(([0, 0, 0, 0], port)); | ||
info!("Serving {:?} on {}", path, addr); | ||
|
||
let state = HttpServeState { path }; | ||
// axum router | ||
let router = Router::new() | ||
.route("/*path", get(file_handler)) | ||
.with_state(Arc::new(state)); | ||
|
||
let listener = tokio::net::TcpListener::bind(addr).await?; | ||
axum::serve(listener, router).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn file_handler( | ||
State(state): State<Arc<HttpServeState>>, | ||
Path(path): Path<String>, | ||
) -> (StatusCode, String) { | ||
let p = std::path::Path::new(&state.path).join(path); | ||
info!("Reading file {:?}", p); | ||
if !p.exists() { | ||
( | ||
StatusCode::NOT_FOUND, | ||
format!("File {} note found", p.display()), | ||
) | ||
} else { | ||
match tokio::fs::read_to_string(p).await { | ||
Ok(content) => { | ||
info!("Read {} bytes", content.len()); | ||
(StatusCode::OK, content) | ||
} | ||
Err(e) => { | ||
warn!("Error reading file: {:?}", e); | ||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod b64; | ||
mod csv_convert; | ||
mod gen_pass; | ||
mod http_serve; | ||
mod text; | ||
|
||
pub use b64::{process_decode, process_encode}; | ||
pub use csv_convert::process_csv; | ||
pub use gen_pass::process_genpass; | ||
pub use http_serve::process_http_serve; | ||
pub use text::{process_text_key_generate, process_text_sign, process_text_verify}; |
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 @@ | ||
### Test index page | ||
|
||
GET http://localhost:8080/fixtures/ed25519.pk |