init
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 18 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | index.html | 11 | ||||
| -rw-r--r-- | src/main.rs | 36 |
5 files changed, 70 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e560953 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.wrangler +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..119f6e5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "svgviewer" +version = "0.1.0" +edition = "2021" +authors = ["bendn <[email protected]>"] + +[package.metadata.release] +release = false + + +[dependencies] +axum = { version = "0.8", features = ["macros"] } +reqwest = { version = "0.12.22", features = [ + "rustls-tls", + "http2", +], default-features = false } +anyhow = "1.0.98" +tokio = { version = "1.46.1", features = ["macros", "rt-multi-thread"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..9dd231c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# svgviewer + +requests a svg and returns it inside a html document diff --git a/index.html b/index.html new file mode 100644 index 0000000..b9d6e3a --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>SVGViewer</title> + </head> + <body> + @svg + </body> +</html> diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fedece7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +#![feature(try_blocks)] +use axum::{ + extract::Path, + http::Uri, + response::{Html, IntoResponse, Response}, + routing::get, + Router, +}; +use reqwest::StatusCode; + +fn router() -> Router { + Router::new().route("/{*k}", get(root)) +} + +pub async fn root(uri: Uri) -> Response { + let url = [ + &uri.path()[1..], + &uri.query().map_or("".to_string(), |x| "?".to_string() + x), + ] + .concat(); + let Ok::<String, anyhow::Error>(svg) = (try { reqwest::get(&url).await?.text().await? }) else { + return (StatusCode::IM_A_TEAPOT, url).into_response(); + }; + + Html(include_str!("../index.html").replace("@svg", &(svg + &url))).into_response() +} + +#[tokio::main] +async fn main() { + axum::serve( + tokio::net::TcpListener::bind("0.0.0.0:3333").await.unwrap(), + router().into_make_service(), + ) + .await + .unwrap(); +} |