write-only-journal/src/main.rs

168 lines
4.5 KiB
Rust

mod journal;
use chrono::TimeZone;
use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer, Serialize};
use tera::{Context, Tera};
use warp::{Filter, Rejection, Reply, filters::method, http::Response, query, reply::html};
use crate::journal::{Journal, Line};
#[cfg(feature = "hotreload")]
use {tokio::{spawn, sync::RwLock, task::spawn_blocking}, hotwatch::{blocking::Hotwatch, Event}};
#[cfg(feature = "hotreload")]
static TERA: Lazy<RwLock<Tera>> =
Lazy::new(|| RwLock::new(Tera::new("www/*").expect("Failed to compile templates")));
#[cfg(not(feature = "hotreload"))]
static TERA: Lazy<Tera> =
Lazy::new(|| {
let mut tera = Tera::default();
tera.add_raw_template("index.html", include_str!("../www/index.html")).expect("Failed to compile template");
tera.add_raw_template("view.html", include_str!("../www/view.html")).expect("Failed to compile template");
tera
});
async fn get_tera() -> impl std::ops::Deref<Target=Tera> {
#[cfg(feature = "hotreload")]
return TERA.read().await;
#[cfg(not(feature = "hotreload"))]
&(*TERA)
}
async fn serve_index() -> Result<impl Reply, Rejection> {
Ok(html(
get_tera().await.render("index.html", &Context::new())
.unwrap(),
))
}
#[derive(Serialize, Deserialize, Debug)]
struct ViewQuery {
journal: String,
#[serde(default)]
#[serde(deserialize_with = "de_emptystring_none")]
key: Option<String>,
}
fn de_emptystring_none<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<String>, D::Error> {
let s = String::deserialize(deserializer)?;
if s.is_empty() {
Ok(None)
} else {
Ok(Some(s))
}
}
#[derive(Serialize, Deserialize, Debug)]
struct ViewRenderContext {
journal: Journal,
password_matches: bool,
}
async fn serve_view(query: ViewQuery) -> Result<impl Reply, Rejection> {
let ctx = match Journal::load(&query.journal) {
Some(j) => {
let password_matches = query.key.map(|k| k == j.edit_key).unwrap_or(false);
ViewRenderContext {
journal: j,
password_matches,
}
}
None => {
if let Some(k) = query.key {
ViewRenderContext {
journal: Journal::new(query.journal, k),
password_matches: true,
}
} else {
return Err(warp::reject());
}
}
};
Ok(html(
get_tera()
.await
.render("view.html", &Context::from_serialize(ctx).unwrap())
.unwrap(),
))
}
#[derive(Serialize, Deserialize, Debug)]
struct AppendQuery {
journal: String,
key: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct AppendRequest {
line: String,
timezone_offset: i32,
}
async fn append_line(query: AppendQuery, req: AppendRequest) -> Result<impl Reply, Rejection> {
let mut journal = Journal::load_or_create(query.journal.clone(), query.key.clone());
if journal.edit_key == query.key {
let tz = chrono::FixedOffset::west_opt(req.timezone_offset*60).ok_or_else(warp::reject)?;
let time = tz.from_utc_datetime(&chrono::Utc::now().naive_utc());
journal.lines.push(Line {
line: req.line,
uploaded_date: time,
});
journal.save().unwrap();
Ok(
Response::builder()
.header(
"location",
format!(
"/view?journal={}&key={}",
&query.journal,
query.key
),
)
.status(303)
.body(""),
)
} else {
Ok(Response::builder().status(403).body("Invalid password"))
}
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
#[cfg(feature = "hotreload")]
watch_tera();
let index = warp::path::end().and_then(serve_index);
let view = warp::path("view").and(query()).and(method::get()).and_then(serve_view);
let append = warp::path("view").and(query()).and(method::post()).and(warp::filters::body::form()).and_then(append_line);
let routes = index.or(view).or(append);
warp::serve(routes).run(([0, 0, 0, 0], 8080)).await;
}
#[cfg(feature = "hotreload")]
fn watch_tera() {
spawn_blocking(|| {
let mut hotwatch = Hotwatch::new().expect("hotwatch failed to initialize!");
hotwatch
.watch("www", |event: Event| {
tokio::spawn(async {
match TERA.write().await.full_reload() {
Ok(_) => println!("Reloaded tera templates"),
Err(e) => println!("Tera error compiling templates {}", e),
}
});
hotwatch::blocking::Flow::Continue
})
.expect("failed to watch file!");
println!("Starting live tera template reloading!");
hotwatch.run();
});
}