Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
1961
Cargo.lock
generated
Normal file
1961
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "osm-proxy"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8.8"
|
||||
http-body-util = "0.1.3"
|
||||
reqwest = "0.13.2"
|
||||
thiserror = "2.0.18"
|
||||
tokio = { version = "1.50.0", features = [
|
||||
"rt",
|
||||
"rt-multi-thread",
|
||||
"sync",
|
||||
"macros",
|
||||
"net",
|
||||
"signal",
|
||||
] }
|
||||
tower-http = { version = "0.6.8", features = ["fs", "trace", "cors"] }
|
||||
67
src/main.rs
Normal file
67
src/main.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use axum::Router;
|
||||
use axum::extract::Path;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::routing::get;
|
||||
use reqwest::StatusCode;
|
||||
use tokio::fs;
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let app = Router::new()
|
||||
.route("/{*path}", get(get_file))
|
||||
.layer(CorsLayer::permissive());
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
axum::serve(listener, app.into_make_service()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_file(Path(path): Path<String>) -> Result<impl IntoResponse, StatusCode> {
|
||||
let local_path = PathBuf::from("static").join(&path);
|
||||
|
||||
// Check if file exists
|
||||
if fs::metadata(&local_path).await.is_err() {
|
||||
// Build remote URL
|
||||
let remote_url = format!("https://osm.rrze.fau.de/osmhd/{}", path);
|
||||
|
||||
// Download file
|
||||
let response = reqwest::get(&remote_url)
|
||||
.await
|
||||
.map_err(|_| StatusCode::BAD_GATEWAY)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
let bytes = response
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|_| StatusCode::BAD_GATEWAY)?;
|
||||
|
||||
// Ensure directory exists
|
||||
if let Some(parent) = local_path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
}
|
||||
|
||||
// Save file
|
||||
fs::write(&local_path, &bytes)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
}
|
||||
|
||||
// Serve file
|
||||
let bytes = fs::read(&local_path)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
Ok(([(axum::http::header::CONTENT_TYPE, "image/png")], bytes))
|
||||
}
|
||||
BIN
static/10/513/512.png
Normal file
BIN
static/10/513/512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 126 B |
Reference in New Issue
Block a user