From 4ffc45d5f7de359948d09bf0b39e40af2105a2ae Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 26 Mar 2024 11:43:57 -0700 Subject: [PATCH] rewrite GET /account and add GET /myimages --- docker-compose.yml | 2 ++ src/main.rs | 28 ++++++++++++++++++++-------- web/myimages.html | 28 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 web/myimages.html diff --git a/docker-compose.yml b/docker-compose.yml index 65d8e24..bc96c42 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,8 @@ services: environment: - ROCKET_DATABASES={fossil_postgres={url="postgres://postgres:$POSTGRES_PASSWORD@db/$POSTGRES_DB", max_connections=10, connect_timeout=10}} + - ROCKET_LIMITS={file=1GiB} + - ROCKET_ADDRESS=0.0.0.0 - ROCKET_PORT=$PORT - RUST_LOG=debug diff --git a/src/main.rs b/src/main.rs index 0f7900d..93368d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,11 @@ fn uploadimage() -> RawHtml { RawHtml(fs::read_to_string("/srv/web/uploadimage.html").unwrap()) } +#[get("/myimages")] +fn myimages() -> RawHtml { + RawHtml(fs::read_to_string("/srv/web/myimages.html").unwrap()) +} + #[derive(Deserialize)] #[serde(crate = "rocket::serde")] struct LoginInfo { @@ -95,21 +100,27 @@ async fn createuser( } } +#[derive(Serialize)] +#[serde(crate = "rocket::serde")] +struct GetUser { + username: String, + admin: bool, + make_posts: bool, + comment: bool +} + #[get("/account")] -async fn account(mut db: Connection, cookies: &CookieJar<'_>) -> status::Custom { +async fn account(mut db: Connection, cookies: &CookieJar<'_>) -> status::Custom, &'static str>> { let token = cookies.get_private("token"); match token { Some(t) => match User::get_by_token(&mut db, t).await { Some(user) => status::Custom( Status::Ok, - format!( - "Username: {}\nAdmin: {}\nMake Posts: {}\nComment: {}", - user.username, user.admin, user.make_posts, user.comment - ), + Ok(Json(GetUser {username: user.username, admin: user.admin, make_posts: user.make_posts, comment: user.comment})), ), - None => status::Custom(Status::NotFound, "User doesn't exist.".to_string()), + None => status::Custom(Status::NotFound, Err("User doesn't exist.")), }, - None => status::Custom(Status::Unauthorized, "Not logged in".to_string()), + None => status::Custom(Status::Unauthorized, Err("Not logged in")), } } @@ -629,7 +640,8 @@ async fn main() { upload, uploadimage, delete_image, - get_images_by_username + get_images_by_username, + myimages ], ) .mount("/api", routes![api_perms]) diff --git a/web/myimages.html b/web/myimages.html new file mode 100644 index 0000000..59cdb93 --- /dev/null +++ b/web/myimages.html @@ -0,0 +1,28 @@ + + + + My Images + + + + + + + + + +