diff options
Diffstat (limited to 'server/src/main.rs')
-rw-r--r-- | server/src/main.rs | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/server/src/main.rs b/server/src/main.rs index 3a1fd52..88eb903 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,10 +2,6 @@ // - Have a task that cleans up expired logins once in a while (once every day?) use tokio; -use tokio::sync::Mutex; -use std::{ - sync::Arc, -}; use warp::{Filter, Reply, Rejection}; use argon2::{ password_hash::{ @@ -14,11 +10,12 @@ use argon2::{ }, Argon2 }; -use serde::Deserialize; +use serde::{Serialize, Deserialize}; mod constants; use constants::*; mod util; mod db; use db::DB; +mod path; use path::Path; macro_rules! mk_bad_request { ($res:expr) => { Ok(Box::new(warp::reply::with_status($res, warp::http::StatusCode::BAD_REQUEST))) } @@ -28,6 +25,10 @@ macro_rules! mk_not_found { ($res:expr) => { Ok(Box::new(warp::reply::with_status($res, warp::http::StatusCode::NOT_FOUND))) } } +macro_rules! mk_unauthorized { + ($msg:expr) => { Ok(Box::new(warp::reply::with_status($msg, warp::http::StatusCode::UNAUTHORIZED))) } +} + macro_rules! mk_server_err { () => { Ok(Box::new(warp::reply::with_status("Internal server error", warp::http::StatusCode::INTERNAL_SERVER_ERROR))) } } @@ -75,7 +76,7 @@ async fn handle_login(db: DB, req: RegisterReq) -> Response { } }; if let Err(_) = Argon2::default().verify_password(req.password.as_bytes(), &parsed_hash) { - return Ok(Box::new(warp::reply::with_status("Incorrect password", warp::http::StatusCode::UNAUTHORIZED))); + return mk_unauthorized!("Incorrect password"); } match db::create_login_token(db, &req.username).await { Ok(token) => Ok(Box::new(token)), @@ -91,13 +92,50 @@ async fn handle_logout(db: DB, token: String) -> Response { Ok(Box::new("Logged out")) } +macro_rules! check_login { + ($db:expr, $token:expr) => { + match db::check_login($db.clone(), $token).await { + Ok(user) => user, + Err(()) => return mk_unauthorized!("Not logged in"), + } + } +} + +#[derive(Deserialize)] +struct FileCreateReq { + path: String, +} + +#[derive(Serialize)] +struct FileCreateRes { + id: i64, +} + +async fn handle_file_create(db: DB, token: String, req: FileCreateReq) -> Response { + let user = check_login!(db, &token); + + let path = match Path::split(&req.path) { + Some(path) => path, + None => return mk_bad_request!("Invalid path"), + }; + + match db::file_create_empty(db.clone(), user, &path).await { + Ok(id) => Ok(Box::new(warp::reply::json(&FileCreateRes { id }))), + Err(err) => mk_bad_request!(err), + } +} + macro_rules! db_handler1 { ($db:expr, $handler:ident) => { { let db2 = $db.clone(); move |a| $handler(db2.clone(), a) } } } +macro_rules! db_handler2 { + ($db:expr, $handler:ident) => { { let db2 = $db.clone(); move |a,b| $handler(db2.clone(), a, b) } } +} + #[tokio::main] async fn main() { - let db: DB = Arc::new(Mutex::new(db::open().await)); + let db: DB = db::open().await; println!("Opened database at {DB_FILE_NAME}."); let use_login_token = warp::header::<String>("x-kaasnoot-token"); @@ -115,7 +153,11 @@ async fn main() { .and_then(db_handler1!(db, handle_login))) .or(warp::post().and(warp::path!("logout")) .and(use_login_token) - .and_then(db_handler1!(db, handle_logout))); + .and_then(db_handler1!(db, handle_logout))) + .or(warp::put().and(warp::path!("file")) + .and(use_login_token) + .and(warp::body::json()) + .and_then(db_handler2!(db, handle_file_create))); warp::serve(router) .run(([0, 0, 0, 0], 8775)) |