2023-12-22 23:37:23 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-12-23 04:06:36 +01:00
|
|
|
use axum::{Json, Form, Router, routing::{put, post, get}, extract::{State, Query}, response::Redirect};
|
2023-12-23 03:15:06 +01:00
|
|
|
use chrono::Utc;
|
|
|
|
use md5::{Md5, Digest};
|
2023-12-22 23:37:23 +01:00
|
|
|
use tokio::sync::RwLock;
|
2023-12-23 03:15:06 +01:00
|
|
|
use uuid::Uuid;
|
2023-12-22 23:37:23 +01:00
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
use crate::{notifications::NotificationProcessor, model::{Page, PageOptions, PageInsertion}, storage::StorageStrategy};
|
2023-12-22 23:37:23 +01:00
|
|
|
|
|
|
|
pub fn create_router_with_app_routes(state: Context) -> Router {
|
|
|
|
Router::new()
|
2023-12-23 01:26:31 +01:00
|
|
|
.route("/api", get(get_suggestion))
|
|
|
|
.route("/api", post(send_suggestion_form))
|
|
|
|
.route("/api", put(send_suggestion_json))
|
2023-12-22 23:37:23 +01:00
|
|
|
.with_state(Arc::new(RwLock::new(state)))
|
|
|
|
}
|
|
|
|
|
|
|
|
type SafeContext = Arc<RwLock<Context>>;
|
|
|
|
|
|
|
|
pub struct Context {
|
2023-12-28 18:19:16 +01:00
|
|
|
providers: Vec<Box<dyn NotificationProcessor<Page>>>,
|
|
|
|
storage: Box<dyn StorageStrategy<Page>>,
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2023-12-28 18:19:16 +01:00
|
|
|
pub fn new(storage: Box<dyn StorageStrategy<Page>>) -> Self {
|
2023-12-22 23:37:23 +01:00
|
|
|
Context { providers: Vec::new(), storage }
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
pub fn register(mut self, notifier: Box<dyn NotificationProcessor<Page>>) -> Self {
|
2023-12-22 23:37:23 +01:00
|
|
|
self.providers.push(notifier);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
async fn process(&self, x: &Page) {
|
2023-12-22 23:37:23 +01:00
|
|
|
for p in self.providers.iter() {
|
|
|
|
p.process(x).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
async fn send_suggestion(payload: PageInsertion, state: SafeContext) -> Result<Redirect, String> {
|
|
|
|
let page = Page::from(payload);
|
2023-12-23 16:54:42 +01:00
|
|
|
// lock state, process and archive new page
|
2023-12-22 23:37:23 +01:00
|
|
|
let mut lock = state.write().await;
|
2023-12-23 03:14:18 +01:00
|
|
|
lock.process(&page).await;
|
|
|
|
match lock.storage.archive(page).await {
|
2023-12-23 04:06:36 +01:00
|
|
|
Ok(()) => Ok(Redirect::to("/")),
|
|
|
|
Err(e) => Err(e.to_string()),
|
2023-12-22 23:37:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
async fn send_suggestion_json(State(state): State<SafeContext>, Json(payload): Json<PageInsertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
|
|
|
|
async fn send_suggestion_form(State(state): State<SafeContext>, Form(payload): Form<PageInsertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
|
2023-12-22 23:37:23 +01:00
|
|
|
|
|
|
|
|
2023-12-28 18:19:16 +01:00
|
|
|
async fn get_suggestion(State(state): State<SafeContext>, Query(page): Query<PageOptions>) -> Result<Json<Vec<Page>>, String> {
|
2023-12-22 23:37:23 +01:00
|
|
|
let offset = page.offset.unwrap_or(0);
|
|
|
|
let limit = std::cmp::min(page.limit.unwrap_or(20), 20);
|
|
|
|
|
|
|
|
match state.read().await.storage.extract(offset, limit).await {
|
|
|
|
Ok(x) => Ok(Json(x)),
|
|
|
|
Err(e) => Err(e.to_string()),
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 16:54:42 +01:00
|
|
|
|
|
|
|
fn limit_string(s: &str, l: usize) -> String {
|
|
|
|
// TODO is there a better way? slicing doesn't work when l > s.len
|
|
|
|
s.chars().take(l).collect()
|
|
|
|
}
|