mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-05 23:16:35 -06:00
feat: add favicon.png route
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use crate::routes::{
|
||||
absolute_handler, fallback_handler, favicon_handler, implicit_handler, index_handler,
|
||||
relative_handler,
|
||||
absolute_handler, fallback_handler, favicon_handler, favicon_png_handler, implicit_handler,
|
||||
index_handler, relative_handler,
|
||||
};
|
||||
use axum::{http::HeaderValue, response::Response, routing::get, Router};
|
||||
use config::Configuration;
|
||||
@@ -36,6 +36,7 @@ async fn main() {
|
||||
let app = Router::new()
|
||||
.route("/", get(index_handler))
|
||||
.route("/favicon.ico", get(favicon_handler))
|
||||
.route("/favicon.png", get(favicon_png_handler))
|
||||
.route("/{path}", get(implicit_handler))
|
||||
.route("/rel/{path}", get(relative_handler))
|
||||
.route("/relative/{path}", get(relative_handler))
|
||||
|
||||
@@ -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.
|
||||
pub async fn fallback_handler() -> impl IntoResponse {
|
||||
get_error_response(TimeBannerError::NotFound).into_response()
|
||||
|
||||
Reference in New Issue
Block a user