feat: add favicon.png route

This commit is contained in:
2025-07-10 19:09:00 -05:00
parent bab7f916d3
commit 5af4e3b9b7
2 changed files with 24 additions and 2 deletions

View File

@@ -1,8 +1,8 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use crate::routes::{ use crate::routes::{
absolute_handler, fallback_handler, favicon_handler, implicit_handler, index_handler, absolute_handler, fallback_handler, favicon_handler, favicon_png_handler, implicit_handler,
relative_handler, index_handler, relative_handler,
}; };
use axum::{http::HeaderValue, response::Response, routing::get, Router}; use axum::{http::HeaderValue, response::Response, routing::get, Router};
use config::Configuration; use config::Configuration;
@@ -36,6 +36,7 @@ async fn main() {
let app = Router::new() let app = Router::new()
.route("/", get(index_handler)) .route("/", get(index_handler))
.route("/favicon.ico", get(favicon_handler)) .route("/favicon.ico", get(favicon_handler))
.route("/favicon.png", get(favicon_png_handler))
.route("/{path}", get(implicit_handler)) .route("/{path}", get(implicit_handler))
.route("/rel/{path}", get(relative_handler)) .route("/rel/{path}", get(relative_handler))
.route("/relative/{path}", get(relative_handler)) .route("/relative/{path}", get(relative_handler))

View File

@@ -83,6 +83,27 @@ pub async fn favicon_handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> impl
} }
} }
/// Handles `/favicon.png` - generates a dynamic clock favicon showing the current time.
///
/// Logs the client IP address and returns a PNG image of an analog clock.
pub async fn favicon_png_handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> impl IntoResponse {
let now = chrono::Utc::now();
// Log the IP address for the favicon request
tracing::info!("Favicon PNG request from IP: {}", addr.ip());
// Generate PNG bytes directly
match generate_favicon_png_bytes(now) {
Ok(png_bytes) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "image/png")],
png_bytes,
)
.into_response(),
Err(e) => get_error_response(e).into_response(),
}
}
/// Fallback handler for unmatched routes. /// Fallback handler for unmatched routes.
pub async fn fallback_handler() -> impl IntoResponse { pub async fn fallback_handler() -> impl IntoResponse {
get_error_response(TimeBannerError::NotFound).into_response() get_error_response(TimeBannerError::NotFound).into_response()