-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58e697e
commit 07754ae
Showing
9 changed files
with
757 additions
and
8 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,19 @@ | ||
// use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
|
||
use super::verify_path; | ||
|
||
#[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: String, | ||
#[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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: &str, port: u16) -> Result<()> { | ||
let addr = SocketAddr::from(([0, 0, 0, 0], port)); | ||
info!("Serving {:?} on port {}", path, addr); | ||
let path = PathBuf::from(path); | ||
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.unwrap(); | ||
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() { | ||
return ( | ||
StatusCode::NOT_FOUND, | ||
format!("File not 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,10 +1,12 @@ | ||
mod b64; | ||
mod csv_convert; | ||
mod gen_pass; | ||
mod http_serve; | ||
mod text; | ||
|
||
pub use csv_convert::process_csv; | ||
pub use gen_pass::process_gen_pass; | ||
|
||
pub use b64::{process_decode, process_encode}; | ||
pub use http_serve::process_http_serve; | ||
pub use text::{process_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/Cargo.toml |