guestbook.rs/src/routes.rs

87 lines
2.8 KiB
Rust
Raw Normal View History

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};
use chrono::Utc;
use md5::{Md5, Digest};
use tokio::sync::RwLock;
use uuid::Uuid;
2023-12-23 04:06:36 +01:00
use crate::{notifications::NotificationProcessor, model::{GuestBookPage, PageOptions, Insertion}, storage::StorageStrategy};
pub fn create_router_with_app_routes(state: Context) -> Router {
Router::new()
.route("/api", get(get_suggestion))
.route("/api", post(send_suggestion_form))
.route("/api", put(send_suggestion_json))
.with_state(Arc::new(RwLock::new(state)))
}
type SafeContext = Arc<RwLock<Context>>;
pub struct Context {
2023-12-23 03:14:18 +01:00
providers: Vec<Box<dyn NotificationProcessor<GuestBookPage>>>,
storage: Box<dyn StorageStrategy<GuestBookPage>>,
}
impl Context {
2023-12-23 03:14:18 +01:00
pub fn new(storage: Box<dyn StorageStrategy<GuestBookPage>>) -> Self {
Context { providers: Vec::new(), storage }
}
2023-12-23 03:14:18 +01:00
pub fn register(mut self, notifier: Box<dyn NotificationProcessor<GuestBookPage>>) -> Self {
self.providers.push(notifier);
self
}
2023-12-23 03:14:18 +01:00
async fn process(&self, x: &GuestBookPage) {
for p in self.providers.iter() {
p.process(x).await;
}
}
}
2023-12-23 04:21:40 +01:00
async fn send_suggestion(unsafe_payload: Insertion, state: SafeContext) -> Result<Redirect, String> {
let payload = unsafe_payload.sanitize();
let mut hasher = Md5::new();
let id = payload.contact.clone().unwrap_or(Uuid::new_v4().to_string());
hasher.update(id.as_bytes());
let avatar = hasher.finalize();
let page = GuestBookPage {
avatar: format!("{:x}", avatar),
2023-12-23 03:24:45 +01:00
author: payload.author.map(|x| x.chars().take(25).collect()), // TODO don't hardcode char limits!
contact: payload.contact.clone().map(|x| x.chars().take(50).collect()),
body: payload.body,
date: Utc::now(),
url: match payload.contact {
None => None,
Some(c) => if c.starts_with("http") {
Some(c)
} else if c.contains('@') {
Some(format!("mailto:{}", c))
} else {
None
}
},
};
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-23 04:06:36 +01:00
async fn send_suggestion_json(State(state): State<SafeContext>, Json(payload): Json<Insertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
async fn send_suggestion_form(State(state): State<SafeContext>, Form(payload): Form<Insertion>) -> Result<Redirect, String> { send_suggestion(payload, state).await }
2023-12-23 03:14:18 +01:00
async fn get_suggestion(State(state): State<SafeContext>, Query(page): Query<PageOptions>) -> Result<Json<Vec<GuestBookPage>>, String> {
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()),
}
}