serve proofs through http interface

This commit is contained in:
Giacomo Pasini 2025-04-24 10:30:12 +02:00
parent ddade529a6
commit 5b0a5a11cf
No known key found for this signature in database
GPG Key ID: FC08489D2D895D4B
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,39 @@
use axum::{
extract::Query,
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Deserialize;
use std::path::PathBuf;
use tokio::fs;
#[derive(Deserialize)]
pub struct ProofRequest {
block_start: u64,
block_count: u64
}
/// Handler for GET /
pub async fn serve_proof(Query(query): Query<ProofRequest>) -> Response {
let file_name = format!("{}-{}.zkp", query.block_start, query.block_count + query.block_start);
let path = PathBuf::from(&file_name);
// Read file contents
match fs::read(&path).await {
Ok(bytes) => (
StatusCode::OK,
bytes,
).into_response(),
Err(err) => {
let status = if err.kind() == std::io::ErrorKind::NotFound {
StatusCode::NOT_FOUND
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
(status, format!("Error reading file: {}", err)).into_response()
}
}
}

View File

@ -117,6 +117,12 @@ fn get_latest_block(client: &Client, rpc: &str) -> Result<u64, String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
std::thread::spawn(move || {
if let Err(e) = run_server() {
error!("Error running server: {}", e);
}
});
let filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&args.log_level));
@ -166,3 +172,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
}
#[tokio::main]
async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
// Initialize tracing
tracing_subscriber::fmt::init();
// Build our application with a route
let app = Router::new()
.route("/", get(http::serve_proof));
let addr = SocketAddr::from(([127, 0, 0, 1], 8070));
// Run it on localhost:8070
tracing::info!("Serving files on http://{}", addr);
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
Ok(())
}