mirror of
https://github.com/flibusta-apps/telegram_files_server.git
synced 2026-03-02 22:55:25 +01:00
Add /health endpoint and tidy views.rs
This commit is contained in:
@@ -11,8 +11,8 @@ use axum_prometheus::PrometheusMetricLayer;
|
|||||||
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
|
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
|
||||||
use tokio_util::io::ReaderStream;
|
use tokio_util::io::ReaderStream;
|
||||||
use tower_http::trace::{self, TraceLayer};
|
use tower_http::trace::{self, TraceLayer};
|
||||||
use tracing::Level;
|
|
||||||
use tracing::log;
|
use tracing::log;
|
||||||
|
use tracing::Level;
|
||||||
|
|
||||||
use crate::config::CONFIG;
|
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
|
const BODY_LIMIT: usize = 4 * (2 << 30); // bytes: 4GB
|
||||||
|
|
||||||
|
|
||||||
async fn auth(req: Request<axum::body::Body>, next: Next) -> Result<Response, StatusCode> {
|
async fn auth(req: Request<axum::body::Body>, next: Next) -> Result<Response, StatusCode> {
|
||||||
let auth_header = req
|
let auth_header = req
|
||||||
.headers()
|
.headers()
|
||||||
@@ -40,13 +39,15 @@ async fn auth(req: Request<axum::body::Body>, next: Next) -> Result<Response, St
|
|||||||
Ok(next.run(req).await)
|
Ok(next.run(req).await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub async fn get_router() -> Router {
|
pub async fn get_router() -> Router {
|
||||||
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
|
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
|
||||||
|
|
||||||
let app_router = Router::new()
|
let app_router = Router::new()
|
||||||
.route("/api/v1/files/upload/", post(upload))
|
.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(DefaultBodyLimit::max(BODY_LIMIT))
|
||||||
.layer(middleware::from_fn(auth))
|
.layer(middleware::from_fn(auth))
|
||||||
.layer(prometheus_layer);
|
.layer(prometheus_layer);
|
||||||
@@ -54,9 +55,12 @@ pub async fn get_router() -> Router {
|
|||||||
let metric_router =
|
let metric_router =
|
||||||
Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
||||||
|
|
||||||
|
let health_router = Router::new().route("/health", get(health));
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(app_router)
|
.merge(app_router)
|
||||||
.merge(metric_router)
|
.merge(metric_router)
|
||||||
|
.merge(health_router)
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
|
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
|
||||||
@@ -64,7 +68,6 @@ pub async fn get_router() -> Router {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(TryFromMultipart)]
|
#[derive(TryFromMultipart)]
|
||||||
pub struct UploadFileRequest {
|
pub struct UploadFileRequest {
|
||||||
#[form_data(limit = "unlimited")]
|
#[form_data(limit = "unlimited")]
|
||||||
@@ -73,7 +76,6 @@ pub struct UploadFileRequest {
|
|||||||
caption: Option<String>,
|
caption: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
|
async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
|
||||||
let result = match upload_file(
|
let result = match upload_file(
|
||||||
data.file.clone(),
|
data.file.clone(),
|
||||||
@@ -89,17 +91,19 @@ async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
|
|||||||
result.unwrap()
|
result.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn health() -> impl IntoResponse {
|
||||||
|
StatusCode::OK
|
||||||
|
}
|
||||||
|
|
||||||
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
|
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
|
||||||
let file = match download_file(chat_id, message_id).await {
|
let file = match download_file(chat_id, message_id).await {
|
||||||
Ok(v) => {
|
Ok(v) => match v {
|
||||||
match v {
|
Some(v) => v,
|
||||||
Some(v) => v,
|
None => return StatusCode::NO_CONTENT.into_response(),
|
||||||
None => return StatusCode::NO_CONTENT.into_response(),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("{}", err);
|
log::error!("{}", err);
|
||||||
return StatusCode::INTERNAL_SERVER_ERROR.into_response()
|
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user