diff options
author | metamuffin <metamuffin@disroot.org> | 2025-02-06 16:52:46 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-02-06 16:52:46 +0100 |
commit | 0ce64a50b763d2b19f5ca254233370418f4b7658 (patch) | |
tree | fe1587d38c77180b0c7fa4911d416605e5d6c6e3 /server/src/routes/api/mod.rs | |
parent | 87ebdede17007b626b1275c66dde1e5aefd6cddc (diff) | |
download | jellything-0ce64a50b763d2b19f5ca254233370418f4b7658.tar jellything-0ce64a50b763d2b19f5ca254233370418f4b7658.tar.bz2 jellything-0ce64a50b763d2b19f5ca254233370418f4b7658.tar.zst |
add json capability to most useful endpoints
Diffstat (limited to 'server/src/routes/api/mod.rs')
-rw-r--r-- | server/src/routes/api/mod.rs | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/server/src/routes/api/mod.rs b/server/src/routes/api/mod.rs deleted file mode 100644 index 065c136..0000000 --- a/server/src/routes/api/mod.rs +++ /dev/null @@ -1,90 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2025 metamuffin <metamuffin.org> -*/ -use super::ui::{ - account::{login_logic, session::AdminSession}, - error::MyResult, -}; -use crate::database::Database; -use jellybase::assetfed::AssetInner; -use jellycommon::user::CreateSessionParams; -use rocket::{ - get, - http::MediaType, - outcome::Outcome, - post, - request::{self, FromRequest}, - response::Redirect, - serde::json::Json, - Request, State, -}; -use serde_json::{json, Value}; -use std::ops::Deref; - -#[get("/api")] -pub fn r_api_root() -> Redirect { - Redirect::moved("https://jellything.metamuffin.org/book/api.html#jellything-http-api") -} - -#[get("/api/version")] -pub fn r_api_version() -> &'static str { - env!("CARGO_PKG_VERSION") -} - -#[post("/api/create_session", data = "<data>")] -pub fn r_api_account_login( - database: &State<Database>, - data: Json<CreateSessionParams>, -) -> MyResult<Value> { - let token = login_logic( - database, - &data.username, - &data.password, - data.expire, - data.drop_permissions.clone(), - )?; - Ok(json!(token)) -} - -#[get("/api/asset_token_raw/<token>")] -pub fn r_api_asset_token_raw(admin: AdminSession, token: &str) -> MyResult<Json<AssetInner>> { - drop(admin); - Ok(Json(AssetInner::deser(token)?)) -} - -pub struct AcceptJson(bool); -impl Deref for AcceptJson { - type Target = bool; - fn deref(&self) -> &Self::Target { - &self.0 - } -} -impl<'r> FromRequest<'r> for AcceptJson { - type Error = (); - - fn from_request<'life0, 'async_trait>( - request: &'r Request<'life0>, - ) -> ::core::pin::Pin< - Box< - dyn ::core::future::Future<Output = request::Outcome<Self, Self::Error>> - + ::core::marker::Send - + 'async_trait, - >, - > - where - 'r: 'async_trait, - 'life0: 'async_trait, - Self: 'async_trait, - { - Box::pin(async move { - Outcome::Success(AcceptJson( - request - .accept() - .map(|a| a.preferred().exact_eq(&MediaType::JSON)) - .unwrap_or(false), - )) - }) - } -} |