tmtd/src/web.rs
2022-04-16 23:54:13 +02:00

209 lines
7 KiB
Rust

/*
* tmtd - Suckless To Do list
* Copyright (C) 2022 C4TG1RL5
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
use async_sqlx_session::PostgresSessionStore;
use chrono::Duration;
use sqlx::types::chrono::Utc;
use tera::{Context, Tera};
use tokio::sync::broadcast;
use tracing::info;
use warp::{Filter, Reply};
use warp_sessions::{CookieOptions, SameSiteCookieOption, SessionWithStore};
use std::time::SystemTime;
use crate::{task, Config, Database};
macro_rules! warp_try {
($expr:expr) => {
match $expr {
Ok(o) => o,
Err(e) => {
use warp::Reply;
tracing::warn!("Error in a request handler: {}", e);
return warp::reply::with_status(
format!("Error: {}", e),
warp::http::StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response();
}
}
};
($store:expr, $expr:expr) => {
match $expr {
Ok(o) => o,
Err(e) => {
use warp::Reply;
tracing::warn!("Error in a request handler: {}", e);
return (
warp::reply::with_status(
format!("Error: {}", e),
warp::http::StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response(),
$store,
);
}
}
};
}
macro_rules! warp_session {
($filter:expr, $store:expr, $then:expr) => {
$filter
.and(
warp_sessions::request::with_session(
$store.clone(),
Some(CookieOptions {
cookie_name: "tmtd_sid",
cookie_value: None,
max_age: Some(86400 * 7),
domain: None,
path: None,
secure: true,
http_only: true,
same_site: Some(SameSiteCookieOption::Strict),
}),
)
.map(|mut s: SessionWithStore<PostgresSessionStore>| {
s.session.set_expiry(Utc::now() + Duration::days(7));
s
}),
)
.then($then)
.untuple_one()
.and_then(warp_sessions::reply::with_session)
};
}
pub struct App {
config: Arc<Config>,
db: Arc<Database>,
session_store: PostgresSessionStore,
tera: Arc<Tera>,
}
impl App {
pub fn new(
config: Arc<Config>,
db: Arc<Database>,
session_store: PostgresSessionStore,
) -> Result<Self, tera::Error> {
Ok(Self {
config,
db,
session_store,
tera: Arc::new(Tera::new("templates/**/*.html")?),
})
}
pub async fn run(self, mut cancel: broadcast::Receiver<()>) {
let cloned_db = self.db.clone(); // Borrow checker moment
let db = warp::any().map(move || cloned_db.clone());
let route = warp_session!(
warp::path::end(),
self.session_store,
move |mut s: SessionWithStore<_>| async move {
s.session.insert_raw("test", "something".into());
(
warp::reply::html(format!("session id: {}", s.session.id())),
s,
)
}
)
.or(warp::path("task").map({
let tera = self.tera.clone();
let db = self.db.clone();
move || {
let mut ctx = Context::new();
// TODO: Replace the for loop with an SQL request to select only the tasks you can
// see after you logged in and the selected category which is stored in a cookie
// previously (see `post_task_sort` for more info)
// TODO: figure out what we need to pass to the get_issues function
let tasks = db.get_tasks();
ctx.insert("tasks", &tasks);
warp::reply::html(tera.render("task/task_page.html", &ctx).unwrap())
}
}))
.or(warp::path("task")
.and(warp::path("create"))
.map({
let tera = self.tera.clone();
move || {
let ctx = Context::new();
warp::reply::html(tera.render("task/create_task.html", &ctx).unwrap())
}
}))
.or(warp::post()
.and(warp::path("api"))
.and(warp::path("task"))
.and(warp::path("move"))
.and(warp::body::form::<task::MoveRequest>())
.and(db.clone())
.then(post_task_move)
)
.or(warp::post()
.and(warp::path("api"))
.and(warp::path("task"))
.and(warp::path("sort"))
.and(warp::body::form::<task::Request>())
.and(db.clone())
.then(post_task_sort)
)
.or(warp::post()
.and(warp::path("api"))
.and(warp::path("task"))
.and(warp::path("create"))
.and(warp::body::form::<task::Create>())
.and(db.clone())
.then(post_task_create)
);
let (_, server) =
warp::serve(route).bind_with_graceful_shutdown(self.config.listen_addr, async move {
cancel.recv().await.unwrap();
});
server.await;
info!("Web app has been shut down");
}
}
pub async fn post_task_move(form: task::MoveRequest, db: Arc<Database>) -> impl warp::Reply {
tracing::debug!("Got POST on /api/task/move: {:?}", form);
db.move_task(form).await;
warp::redirect(warp::http::Uri::from_static("/task"))
}
pub async fn post_task_sort(form: task::Request, db: Arc<Database>) -> impl warp::Reply {
tracing::debug!("Got POST on /api/task/move: {}", form.request);
// TODO: Store the information of the form in a cookie for the selection of the tasks in the
// website building
warp::redirect(warp::http::Uri::from_static("/task"))
}
pub async fn post_task_create(form: task::Create, db: Arc<Database>)-> impl warp::Reply {
let date = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
tracing::debug!("Got POST on /api/task/create: {:?}, date: {:?}", form, date);
db.create_task(form);
warp::redirect(warp::http::Uri::from_static("/task"))
}