82 lines
3.1 KiB
Rust
82 lines
3.1 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 crate::{task, templates, database::Database};
|
|
use actix_web::{http::StatusCode, web, HttpResponse, HttpResponseBuilder, HttpServer, Responder};
|
|
use askama::Template;
|
|
use std::net::SocketAddr;
|
|
use std::sync::Arc;
|
|
|
|
pub struct App {
|
|
pub server: actix_web::dev::Server,
|
|
}
|
|
|
|
impl App {
|
|
pub async fn new(addr: SocketAddr, db: Arc<Database>) -> anyhow::Result<Self> {
|
|
let db = db.clone();
|
|
let server = HttpServer::new(move || {
|
|
actix_web::App::new().service((
|
|
web::resource("/task")
|
|
.app_data(web::Data::new(db.clone()))
|
|
.route(web::get().to(task)),
|
|
web::resource("/create")
|
|
.route(web::get().to(create)),
|
|
web::resource("/api/task/create")
|
|
.app_data(web::Data::new(db.clone()))
|
|
.route(web::post().to(create_task)),
|
|
web::resource("/api/task/move")
|
|
.app_data(web::Data::new(db.clone()))
|
|
.route(web::post().to(move_task)),
|
|
web::resource("/api/task/sort")
|
|
.app_data(web::Data::new(db.clone()))
|
|
.route(web::post().to(sort_task)),
|
|
))
|
|
})
|
|
.bind(addr)?
|
|
.run();
|
|
Ok(App { server })
|
|
}
|
|
}
|
|
|
|
async fn task(db: web::Data<Arc<Database>>) -> impl Responder {
|
|
let tasks = db.get_tasks();
|
|
let html = tasks.render().unwrap();
|
|
HttpResponseBuilder::new(StatusCode::OK).body(html)
|
|
}
|
|
|
|
async fn create() -> impl Responder {
|
|
let create = templates::CreateTask{};
|
|
let html = create.render().unwrap();
|
|
HttpResponseBuilder::new(StatusCode::OK).body(html)
|
|
}
|
|
|
|
async fn create_task(req: web::Form<task::CreateTask>, db: web::Data<Arc<Database>>) -> impl Responder {
|
|
tracing::debug!("Got POST request on /api/task/create: {:#?}", req);
|
|
HttpResponse::SeeOther().insert_header(("Location", "/task")).finish()
|
|
}
|
|
|
|
async fn move_task(req: web::Form<task::MoveTask>, db: web::Data<Arc<Database>>) -> impl Responder {
|
|
tracing::debug!("Got POST request on /api/task/move: {:#?}", req);
|
|
HttpResponse::SeeOther().insert_header(("Location", "/task")).finish()
|
|
}
|
|
|
|
async fn sort_task(req: web::Form<task::SortTask>, db: web::Data<Arc<Database>>) -> impl Responder {
|
|
tracing::debug!("Got POST request on /api/task/sort: {:#?}", req);
|
|
HttpResponse::SeeOther().insert_header(("Location", "/task")).finish()
|
|
}
|