mirror of
https://github.com/flibusta-apps/batch_downloader.git
synced 2025-12-06 06:15:37 +01:00
Add pre-commit config
This commit is contained in:
39
src/views.rs
39
src/views.rs
@@ -1,15 +1,25 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::{Router, routing::{get, post}, middleware::{self, Next}, http::{Request, StatusCode, self}, response::{Response, IntoResponse}, extract::Path, Json};
|
||||
use axum::{
|
||||
extract::Path,
|
||||
http::{self, Request, StatusCode},
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use axum_prometheus::PrometheusMetricLayer;
|
||||
use moka::future::Cache;
|
||||
use once_cell::sync::Lazy;
|
||||
use tower_http::trace::{TraceLayer, self};
|
||||
use tower_http::trace::{self, TraceLayer};
|
||||
|
||||
use tracing::Level;
|
||||
|
||||
use crate::{config::CONFIG, structures::{Task, CreateTask, TaskStatus}, services::{task_creator::create_task, utils::get_key}};
|
||||
|
||||
use crate::{
|
||||
config::CONFIG,
|
||||
services::{task_creator::create_task, utils::get_key},
|
||||
structures::{CreateTask, Task, TaskStatus},
|
||||
};
|
||||
|
||||
pub static TASK_RESULTS: Lazy<Cache<String, Task>> = Lazy::new(|| {
|
||||
Cache::builder()
|
||||
@@ -18,37 +28,30 @@ pub static TASK_RESULTS: Lazy<Cache<String, Task>> = Lazy::new(|| {
|
||||
.build()
|
||||
});
|
||||
|
||||
|
||||
async fn create_archive_task(
|
||||
Json(data): Json<CreateTask>
|
||||
) -> impl IntoResponse {
|
||||
async fn create_archive_task(Json(data): Json<CreateTask>) -> impl IntoResponse {
|
||||
let key = get_key(data.clone());
|
||||
|
||||
let result = match TASK_RESULTS.get(&key).await {
|
||||
Some(result) => {
|
||||
if result.status == TaskStatus::Failed {
|
||||
create_task(data).await
|
||||
create_task(data).await
|
||||
} else {
|
||||
result
|
||||
}
|
||||
},
|
||||
}
|
||||
None => create_task(data).await,
|
||||
};
|
||||
|
||||
Json::<Task>(result).into_response()
|
||||
}
|
||||
|
||||
|
||||
async fn check_archive_task_status(
|
||||
Path(task_id): Path<String>
|
||||
) -> impl IntoResponse {
|
||||
async fn check_archive_task_status(Path(task_id): Path<String>) -> impl IntoResponse {
|
||||
match TASK_RESULTS.get(&task_id).await {
|
||||
Some(result) => Json::<Task>(result).into_response(),
|
||||
None => StatusCode::NOT_FOUND.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async fn auth<B>(req: Request<B>, next: Next<B>) -> Result<Response, StatusCode> {
|
||||
let auth_header = req
|
||||
.headers()
|
||||
@@ -68,13 +71,15 @@ async fn auth<B>(req: Request<B>, next: Next<B>) -> Result<Response, StatusCode>
|
||||
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/", post(create_archive_task))
|
||||
.route("/api/check_archive/:task_id", get(check_archive_task_status))
|
||||
.route(
|
||||
"/api/check_archive/:task_id",
|
||||
get(check_archive_task_status),
|
||||
)
|
||||
.layer(middleware::from_fn(auth))
|
||||
.layer(prometheus_layer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user