Change how cleanup trait fn works using a date cutoff
This commit is contained in:
parent
3e770a337b
commit
0304f5955d
20 changed files with 237 additions and 131 deletions
|
|
@ -17,6 +17,7 @@ use utoipa::{
|
|||
routes::person::get_people,
|
||||
routes::person::get_person,
|
||||
routes::person::update_person,
|
||||
routes::tasks::cleanup,
|
||||
),
|
||||
components(schemas(
|
||||
payloads::StatsResponse,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use axum::{http::StatusCode, response::IntoResponse};
|
||||
use common::adaptor::Adaptor;
|
||||
use common::Adaptor;
|
||||
|
||||
pub enum ApiError<A: Adaptor> {
|
||||
AdaptorError(A::Error),
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ async fn main() {
|
|||
"/event/:event_id/people/:person_name",
|
||||
patch(person::update_person),
|
||||
)
|
||||
.route("/tasks/cleanup", patch(tasks::cleanup))
|
||||
.with_state(shared_state)
|
||||
.layer(cors)
|
||||
.layer(rate_limit)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use axum::Json;
|
||||
use common::{event::Event, person::Person, stats::Stats};
|
||||
use common::{Event, Person, Stats};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use axum::{
|
|||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use common::{adaptor::Adaptor, event::Event};
|
||||
use common::{Adaptor, Event};
|
||||
use rand::{seq::SliceRandom, thread_rng, Rng};
|
||||
use regex::Regex;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod event;
|
||||
pub mod person;
|
||||
pub mod stats;
|
||||
pub mod tasks;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use axum::{
|
|||
Json, TypedHeader,
|
||||
};
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use common::{adaptor::Adaptor, person::Person};
|
||||
use common::{Adaptor, Person};
|
||||
|
||||
use crate::{
|
||||
errors::ApiError,
|
||||
|
|
@ -120,6 +120,7 @@ pub async fn get_person<A: Adaptor>(
|
|||
)
|
||||
.await
|
||||
.map_err(ApiError::AdaptorError)?
|
||||
.unwrap()
|
||||
.into(),
|
||||
))
|
||||
}
|
||||
|
|
@ -189,6 +190,7 @@ pub async fn update_person<A: Adaptor>(
|
|||
)
|
||||
.await
|
||||
.map_err(ApiError::AdaptorError)?
|
||||
.unwrap()
|
||||
.into(),
|
||||
))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use axum::{extract, Json};
|
||||
use common::adaptor::Adaptor;
|
||||
use common::Adaptor;
|
||||
|
||||
use crate::{
|
||||
errors::ApiError,
|
||||
|
|
|
|||
40
api/src/routes/tasks.rs
Normal file
40
api/src/routes/tasks.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use std::env;
|
||||
|
||||
use axum::{extract, http::HeaderMap};
|
||||
use common::Adaptor;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{errors::ApiError, State};
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/tasks/cleanup",
|
||||
responses(
|
||||
(status = 200, description = "Cleanup complete"),
|
||||
(status = 401, description = "Missing or incorrect X-Cron-Key header"),
|
||||
(status = 429, description = "Too many requests"),
|
||||
),
|
||||
tag = "tasks",
|
||||
)]
|
||||
/// Delete events older than 3 months
|
||||
pub async fn cleanup<A: Adaptor>(
|
||||
extract::State(state): State<A>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<(), ApiError<A>> {
|
||||
// Check cron key
|
||||
let cron_key = headers.get("X-Cron-Key").ok_or(ApiError::NotAuthorized)?;
|
||||
if let Ok(key) = env::var("CRON_KEY") {
|
||||
if !key.is_empty() && *cron_key != key {
|
||||
return Err(ApiError::NotAuthorized);
|
||||
}
|
||||
}
|
||||
|
||||
info!("Running cleanup task");
|
||||
|
||||
let adaptor = &state.lock().await.adaptor;
|
||||
|
||||
// TODO:
|
||||
//let stats = adaptor.get_stats().await.map_err(ApiError::AdaptorError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue