|
| 1 | +use anyhow::Result; |
| 2 | +use axum::extract::{Path, State}; |
| 3 | +use axum::http::StatusCode; |
| 4 | +use axum::routing::get; |
| 5 | +use axum::Router; |
| 6 | +use std::net::SocketAddr; |
| 7 | +use std::path::PathBuf; |
| 8 | +use std::sync::Arc; |
| 9 | +use tracing::{info, warn}; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +struct HttpServeState { |
| 13 | + path: PathBuf, |
| 14 | +} |
| 15 | + |
| 16 | +pub async fn process_http_serve(path: PathBuf, port: u16) -> Result<()> { |
| 17 | + let addr = SocketAddr::from(([0, 0, 0, 0], port)); |
| 18 | + info!("Serving: {:?} on {}", path.as_path(), addr); |
| 19 | + let state = HttpServeState { path }; |
| 20 | + // axum router |
| 21 | + let router = Router::new() |
| 22 | + .route("/*path", get(file_handler)) |
| 23 | + .with_state(Arc::new(state)); |
| 24 | + |
| 25 | + // run our app with hyper, listening globally on port 3000 |
| 26 | + let listener = tokio::net::TcpListener::bind(addr).await?; |
| 27 | + |
| 28 | + axum::serve(listener, router).await?; |
| 29 | + Ok(()) |
| 30 | +} |
| 31 | + |
| 32 | +async fn file_handler( |
| 33 | + State(state): State<Arc<HttpServeState>>, |
| 34 | + Path(path): Path<String>, |
| 35 | +) -> (StatusCode, String) { |
| 36 | + let p = std::path::Path::new(&state.path).join(path); |
| 37 | + info!("Reading file {:?}", p); |
| 38 | + if !p.exists() { |
| 39 | + ( |
| 40 | + StatusCode::NOT_FOUND, |
| 41 | + format!("File {} Not Found", p.display()), |
| 42 | + ) |
| 43 | + } else { |
| 44 | + match tokio::fs::read_to_string(p).await { |
| 45 | + Ok(content) => { |
| 46 | + info!("Read {} bytes", content.len()); |
| 47 | + (StatusCode::OK, content) |
| 48 | + } |
| 49 | + Err(e) => { |
| 50 | + warn!("Error reading file: {:?}", e); |
| 51 | + (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments