use axum::{
extract::Path,
response::{Html, IntoResponse, Redirect},
routing, Router,
};
use ramhorns::{Content, Template};
use tracing::info;
#[derive(Content)]
struct HomeContent {
cargo_version: String,
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", routing::get(home))
.route("/:url", routing::get(redirect));
tracing_subscriber::fmt().init();
info!("Starting server at http://localhost:3000");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
async fn home() -> impl IntoResponse {
let home_template = include_str!("../templates/home.html");
let template = Template::new(home_template).unwrap();
let content = HomeContent {
cargo_version: env!("CARGO_PKG_VERSION").to_string(),
};
Html(template.render(&content))
}
async fn redirect(Path(url): Path) -> impl IntoResponse {
Redirect::permanent(&format!("https://crates.io/crates/{url}"))
}