Add /health endpoint and tidy views.rs

This commit is contained in:
2026-01-14 10:37:43 +01:00
parent b79701d847
commit f8dc2332b6

View File

@@ -11,8 +11,8 @@ use axum_prometheus::PrometheusMetricLayer;
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
use tokio_util::io::ReaderStream;
use tower_http::trace::{self, TraceLayer};
use tracing::Level;
use tracing::log;
use tracing::Level;
use crate::config::CONFIG;
@@ -20,7 +20,6 @@ use super::file_utils::{download_file, upload_file};
const BODY_LIMIT: usize = 4 * (2 << 30); // bytes: 4GB
async fn auth(req: Request<axum::body::Body>, next: Next) -> Result<Response, StatusCode> {
let auth_header = req
.headers()
@@ -40,13 +39,15 @@ async fn auth(req: Request<axum::body::Body>, next: Next) -> Result<Response, St
Ok(next.run(req).await)
}
pub async fn get_router() -> Router {
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
let app_router = Router::new()
.route("/api/v1/files/upload/", post(upload))
.route("/api/v1/files/download_by_message/{chat_id}/{message_id}", get(download))
.route(
"/api/v1/files/download_by_message/{chat_id}/{message_id}",
get(download),
)
.layer(DefaultBodyLimit::max(BODY_LIMIT))
.layer(middleware::from_fn(auth))
.layer(prometheus_layer);
@@ -54,9 +55,12 @@ pub async fn get_router() -> Router {
let metric_router =
Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
let health_router = Router::new().route("/health", get(health));
Router::new()
.merge(app_router)
.merge(metric_router)
.merge(health_router)
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
@@ -64,7 +68,6 @@ pub async fn get_router() -> Router {
)
}
#[derive(TryFromMultipart)]
pub struct UploadFileRequest {
#[form_data(limit = "unlimited")]
@@ -73,7 +76,6 @@ pub struct UploadFileRequest {
caption: Option<String>,
}
async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
let result = match upload_file(
data.file.clone(),
@@ -89,17 +91,19 @@ async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
result.unwrap()
}
async fn health() -> impl IntoResponse {
StatusCode::OK
}
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
let file = match download_file(chat_id, message_id).await {
Ok(v) => {
match v {
Some(v) => v,
None => return StatusCode::NO_CONTENT.into_response(),
}
Ok(v) => match v {
Some(v) => v,
None => return StatusCode::NO_CONTENT.into_response(),
},
Err(err) => {
log::error!("{}", err);
return StatusCode::INTERNAL_SERVER_ERROR.into_response()
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}
};