From 66c3aef6090087d5a3e64cc322027a29f4c6ad0c Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:11:50 +0200 Subject: [PATCH 1/7] style: move channels to /channels --- src/api/v1/channels/mod.rs | 11 +++ .../uuid => }/channels/uuid/messages.rs | 19 ++--- src/api/v1/channels/uuid/mod.rs | 60 +++++++++++++++ .../uuid => }/channels/uuid/socket.rs | 24 ++---- src/api/v1/mod.rs | 2 + .../uuid/{channels/mod.rs => channels.rs} | 2 - src/api/v1/servers/uuid/channels/uuid/mod.rs | 77 ------------------- src/api/v1/servers/uuid/mod.rs | 4 - src/structs.rs | 33 ++++++-- 9 files changed, 111 insertions(+), 121 deletions(-) create mode 100644 src/api/v1/channels/mod.rs rename src/api/v1/{servers/uuid => }/channels/uuid/messages.rs (76%) create mode 100644 src/api/v1/channels/uuid/mod.rs rename src/api/v1/{servers/uuid => }/channels/uuid/socket.rs (82%) rename src/api/v1/servers/uuid/{channels/mod.rs => channels.rs} (99%) delete mode 100644 src/api/v1/servers/uuid/channels/uuid/mod.rs diff --git a/src/api/v1/channels/mod.rs b/src/api/v1/channels/mod.rs new file mode 100644 index 0000000..d3d5d23 --- /dev/null +++ b/src/api/v1/channels/mod.rs @@ -0,0 +1,11 @@ +use actix_web::{web, Scope}; + +mod uuid; + +pub fn web() -> Scope { + web::scope("/channels") + .service(uuid::get) + .service(uuid::delete) + .service(uuid::messages::get) + .service(uuid::socket::ws) +} diff --git a/src/api/v1/servers/uuid/channels/uuid/messages.rs b/src/api/v1/channels/uuid/messages.rs similarity index 76% rename from src/api/v1/servers/uuid/channels/uuid/messages.rs rename to src/api/v1/channels/uuid/messages.rs index 27bf0cf..25134d2 100644 --- a/src/api/v1/servers/uuid/channels/uuid/messages.rs +++ b/src/api/v1/channels/uuid/messages.rs @@ -43,10 +43,10 @@ struct MessageRequest { /// }); /// ``` /// -#[get("{uuid}/channels/{channel_uuid}/messages")] +#[get("/{uuid}/messages")] pub async fn get( req: HttpRequest, - path: web::Path<(Uuid, Uuid)>, + path: web::Path<(Uuid,)>, message_request: web::Query, data: web::Data, ) -> Result { @@ -54,7 +54,7 @@ pub async fn get( let auth_header = get_auth_header(headers)?; - let (guild_uuid, channel_uuid) = path.into_inner(); + let channel_uuid = path.into_inner().0; let mut conn = data.pool.get().await?; @@ -62,18 +62,9 @@ pub async fn get( global_checks(&data, uuid).await?; - Member::fetch_one(&mut conn, uuid, guild_uuid).await?; + let channel = Channel::fetch_one(&data, channel_uuid).await?; - let channel: Channel; - - if let Ok(cache_hit) = data.get_cache_key(format!("{}", channel_uuid)).await { - channel = serde_json::from_str(&cache_hit)? - } else { - channel = Channel::fetch_one(&mut conn, channel_uuid).await?; - - data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60) - .await?; - } + Member::fetch_one(&mut conn, uuid, channel.guild_uuid).await?; let messages = channel .fetch_messages(&data, message_request.amount, message_request.offset) diff --git a/src/api/v1/channels/uuid/mod.rs b/src/api/v1/channels/uuid/mod.rs new file mode 100644 index 0000000..f6c93fe --- /dev/null +++ b/src/api/v1/channels/uuid/mod.rs @@ -0,0 +1,60 @@ +pub mod messages; +pub mod socket; + +use crate::{ + api::v1::auth::check_access_token, error::Error, structs::{Channel, Member}, utils::{get_auth_header, global_checks}, Data +}; +use actix_web::{HttpRequest, HttpResponse, delete, get, web}; +use uuid::Uuid; + +#[get("/{uuid}")] +pub async fn get( + req: HttpRequest, + path: web::Path<(Uuid,)>, + data: web::Data, +) -> Result { + let headers = req.headers(); + + let auth_header = get_auth_header(headers)?; + + let channel_uuid = path.into_inner().0; + + let mut conn = data.pool.get().await?; + + let uuid = check_access_token(auth_header, &mut conn).await?; + + global_checks(&data, uuid).await?; + + let channel = Channel::fetch_one(&data, channel_uuid).await?; + + Member::fetch_one(&mut conn, uuid, channel.guild_uuid).await?; + + Ok(HttpResponse::Ok().json(channel)) +} + +#[delete("/{uuid}")] +pub async fn delete( + req: HttpRequest, + path: web::Path<(Uuid,)>, + data: web::Data, +) -> Result { + let headers = req.headers(); + + let auth_header = get_auth_header(headers)?; + + let channel_uuid = path.into_inner().0; + + let mut conn = data.pool.get().await?; + + let uuid = check_access_token(auth_header, &mut conn).await?; + + global_checks(&data, uuid).await?; + + let channel = Channel::fetch_one(&data, channel_uuid).await?; + + Member::fetch_one(&mut conn, uuid, channel.guild_uuid).await?; + + channel.delete(&data).await?; + + Ok(HttpResponse::Ok().finish()) +} diff --git a/src/api/v1/servers/uuid/channels/uuid/socket.rs b/src/api/v1/channels/uuid/socket.rs similarity index 82% rename from src/api/v1/servers/uuid/channels/uuid/socket.rs rename to src/api/v1/channels/uuid/socket.rs index 3cbdb8f..c7ca1e8 100644 --- a/src/api/v1/servers/uuid/channels/uuid/socket.rs +++ b/src/api/v1/channels/uuid/socket.rs @@ -11,10 +11,10 @@ use crate::{ api::v1::auth::check_access_token, structs::{Channel, Member}, utils::{get_ws_protocol_header, global_checks}, Data }; -#[get("{uuid}/channels/{channel_uuid}/socket")] +#[get("/{uuid}/socket")] pub async fn ws( req: HttpRequest, - path: web::Path<(Uuid, Uuid)>, + path: web::Path<(Uuid,)>, stream: web::Payload, data: web::Data, ) -> Result { @@ -24,8 +24,8 @@ pub async fn ws( // Retrieve auth header let auth_header = get_ws_protocol_header(headers)?; - // Get uuids from path - let (guild_uuid, channel_uuid) = path.into_inner(); + // Get uuid from path + let channel_uuid = path.into_inner().0; let mut conn = data.pool.get().await.map_err(crate::error::Error::from)?; @@ -34,20 +34,10 @@ pub async fn ws( global_checks(&data, uuid).await?; + let channel = Channel::fetch_one(&data, channel_uuid).await?; + // Get server member from psql - Member::fetch_one(&mut conn, uuid, guild_uuid).await?; - - let channel: Channel; - - // Return channel cache or result from psql as `channel` variable - if let Ok(cache_hit) = data.get_cache_key(format!("{}", channel_uuid)).await { - channel = serde_json::from_str(&cache_hit)? - } else { - channel = Channel::fetch_one(&mut conn, channel_uuid).await?; - - data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60) - .await?; - } + Member::fetch_one(&mut conn, uuid, channel.guild_uuid).await?; let (mut res, mut session_1, stream) = actix_ws::handle(&req, stream)?; diff --git a/src/api/v1/mod.rs b/src/api/v1/mod.rs index f30ad58..c08e1e3 100644 --- a/src/api/v1/mod.rs +++ b/src/api/v1/mod.rs @@ -3,6 +3,7 @@ use actix_web::{Scope, web}; mod auth; +mod channels; mod invites; mod servers; mod stats; @@ -14,6 +15,7 @@ pub fn web() -> Scope { .service(stats::res) .service(auth::web()) .service(users::web()) + .service(channels::web()) .service(servers::web()) .service(invites::web()) .service(me::web()) diff --git a/src/api/v1/servers/uuid/channels/mod.rs b/src/api/v1/servers/uuid/channels.rs similarity index 99% rename from src/api/v1/servers/uuid/channels/mod.rs rename to src/api/v1/servers/uuid/channels.rs index 6327e33..813de13 100644 --- a/src/api/v1/servers/uuid/channels/mod.rs +++ b/src/api/v1/servers/uuid/channels.rs @@ -5,8 +5,6 @@ use ::uuid::Uuid; use actix_web::{HttpRequest, HttpResponse, get, post, web}; use serde::Deserialize; -pub mod uuid; - #[derive(Deserialize)] struct ChannelInfo { name: String, diff --git a/src/api/v1/servers/uuid/channels/uuid/mod.rs b/src/api/v1/servers/uuid/channels/uuid/mod.rs deleted file mode 100644 index 946adf3..0000000 --- a/src/api/v1/servers/uuid/channels/uuid/mod.rs +++ /dev/null @@ -1,77 +0,0 @@ -pub mod messages; -pub mod socket; - -use crate::{ - api::v1::auth::check_access_token, error::Error, structs::{Channel, Member}, utils::{get_auth_header, global_checks}, Data -}; -use actix_web::{HttpRequest, HttpResponse, delete, get, web}; -use uuid::Uuid; - -#[get("{uuid}/channels/{channel_uuid}")] -pub async fn get( - req: HttpRequest, - path: web::Path<(Uuid, Uuid)>, - data: web::Data, -) -> Result { - let headers = req.headers(); - - let auth_header = get_auth_header(headers)?; - - let (guild_uuid, channel_uuid) = path.into_inner(); - - let mut conn = data.pool.get().await?; - - let uuid = check_access_token(auth_header, &mut conn).await?; - - global_checks(&data, uuid).await?; - - Member::fetch_one(&mut conn, uuid, guild_uuid).await?; - - if let Ok(cache_hit) = data.get_cache_key(format!("{}", channel_uuid)).await { - return Ok(HttpResponse::Ok() - .content_type("application/json") - .body(cache_hit)); - } - - let channel = Channel::fetch_one(&mut conn, channel_uuid).await?; - - data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60) - .await?; - - Ok(HttpResponse::Ok().json(channel)) -} - -#[delete("{uuid}/channels/{channel_uuid}")] -pub async fn delete( - req: HttpRequest, - path: web::Path<(Uuid, Uuid)>, - data: web::Data, -) -> Result { - let headers = req.headers(); - - let auth_header = get_auth_header(headers)?; - - let (guild_uuid, channel_uuid) = path.into_inner(); - - let mut conn = data.pool.get().await?; - - let uuid = check_access_token(auth_header, &mut conn).await?; - - global_checks(&data, uuid).await?; - - Member::fetch_one(&mut conn, uuid, guild_uuid).await?; - - let channel: Channel; - - if let Ok(cache_hit) = data.get_cache_key(format!("{}", channel_uuid)).await { - channel = serde_json::from_str(&cache_hit)?; - - data.del_cache_key(format!("{}", channel_uuid)).await?; - } else { - channel = Channel::fetch_one(&mut conn, channel_uuid).await?; - } - - channel.delete(&mut conn).await?; - - Ok(HttpResponse::Ok().finish()) -} diff --git a/src/api/v1/servers/uuid/mod.rs b/src/api/v1/servers/uuid/mod.rs index 39b2925..f874b91 100644 --- a/src/api/v1/servers/uuid/mod.rs +++ b/src/api/v1/servers/uuid/mod.rs @@ -19,10 +19,6 @@ pub fn web() -> Scope { // Channels .service(channels::get) .service(channels::create) - .service(channels::uuid::get) - .service(channels::uuid::delete) - .service(channels::uuid::messages::get) - .service(channels::uuid::socket::ws) // Roles .service(roles::get) .service(roles::create) diff --git a/src/structs.rs b/src/structs.rs index 500396f..8d1d367 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -183,15 +183,26 @@ impl Channel { futures::future::try_join_all(channel_futures).await } - pub async fn fetch_one(conn: &mut Conn, channel_uuid: Uuid) -> Result { + pub async fn fetch_one(data: &Data, channel_uuid: Uuid) -> Result { + if let Ok(cache_hit) = data.get_cache_key(channel_uuid.to_string()).await { + return Ok(serde_json::from_str(&cache_hit)?); + } + + let mut conn = data.pool.get().await?; + use channels::dsl; let channel_builder: ChannelBuilder = dsl::channels .filter(dsl::uuid.eq(channel_uuid)) .select(ChannelBuilder::as_select()) - .get_result(conn) + .get_result(&mut conn) .await?; - channel_builder.build(conn).await + let channel = channel_builder.build(&mut conn).await?; + + data.set_cache_key(channel_uuid.to_string(), channel.clone(), 60) + .await?; + + Ok(channel) } pub async fn new( @@ -245,19 +256,27 @@ impl Channel { data.set_cache_key(channel_uuid.to_string(), channel.clone(), 1800) .await?; - data.del_cache_key(format!("{}_channels", guild_uuid)) - .await?; + if let Ok(_) = data.get_cache_key(format!("{}_channels", guild_uuid)).await { + data.del_cache_key(format!("{}_channels", guild_uuid)) + .await?; + } Ok(channel) } - pub async fn delete(self, conn: &mut Conn) -> Result<(), Error> { + pub async fn delete(self, data: &Data) -> Result<(), Error> { + let mut conn = data.pool.get().await?; + use channels::dsl; delete(channels::table) .filter(dsl::uuid.eq(self.uuid)) - .execute(conn) + .execute(&mut conn) .await?; + if let Ok(_) = data.get_cache_key(self.uuid.to_string()).await { + data.del_cache_key(self.uuid.to_string()).await?; + } + Ok(()) } From 1543a2f4857d0977dd6eb274f730e8675bf55ec7 Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:13:01 +0200 Subject: [PATCH 2/7] docs: change path in comments --- src/api/v1/channels/uuid/messages.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/v1/channels/uuid/messages.rs b/src/api/v1/channels/uuid/messages.rs index 25134d2..90055c8 100644 --- a/src/api/v1/channels/uuid/messages.rs +++ b/src/api/v1/channels/uuid/messages.rs @@ -1,4 +1,4 @@ -//! `/api/v1/servers/{uuid}/channels/{uuid}/messages` Endpoints related to channel messages +//! `/api/v1/channels/{uuid}/messages` Endpoints related to channel messages use crate::{ api::v1::auth::check_access_token, error::Error, structs::{Channel, Member}, utils::{get_auth_header, global_checks}, Data @@ -13,7 +13,7 @@ struct MessageRequest { offset: i64, } -/// `GET /api/v1/servers/{uuid}/channels/{uuid}/messages` Returns user with the given UUID +/// `GET /api/v1/channels/{uuid}/messages` Returns user with the given UUID /// /// requires auth: yes /// From e4d9a1b5afe07c8e5d6593acd5c34068c5c43bce Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:15:27 +0200 Subject: [PATCH 3/7] style: move servers to guilds --- src/api/v1/{servers => guilds}/mod.rs | 2 +- src/api/v1/{servers => guilds}/uuid/channels.rs | 0 src/api/v1/{servers => guilds}/uuid/icon.rs | 0 src/api/v1/{servers => guilds}/uuid/invites/id.rs | 0 src/api/v1/{servers => guilds}/uuid/invites/mod.rs | 0 src/api/v1/{servers => guilds}/uuid/mod.rs | 0 src/api/v1/{servers => guilds}/uuid/roles/mod.rs | 0 src/api/v1/{servers => guilds}/uuid/roles/uuid.rs | 0 src/api/v1/mod.rs | 4 ++-- 9 files changed, 3 insertions(+), 3 deletions(-) rename src/api/v1/{servers => guilds}/mod.rs (99%) rename src/api/v1/{servers => guilds}/uuid/channels.rs (100%) rename src/api/v1/{servers => guilds}/uuid/icon.rs (100%) rename src/api/v1/{servers => guilds}/uuid/invites/id.rs (100%) rename src/api/v1/{servers => guilds}/uuid/invites/mod.rs (100%) rename src/api/v1/{servers => guilds}/uuid/mod.rs (100%) rename src/api/v1/{servers => guilds}/uuid/roles/mod.rs (100%) rename src/api/v1/{servers => guilds}/uuid/roles/uuid.rs (100%) diff --git a/src/api/v1/servers/mod.rs b/src/api/v1/guilds/mod.rs similarity index 99% rename from src/api/v1/servers/mod.rs rename to src/api/v1/guilds/mod.rs index f0d27a8..237abce 100644 --- a/src/api/v1/servers/mod.rs +++ b/src/api/v1/guilds/mod.rs @@ -15,7 +15,7 @@ struct GuildInfo { } pub fn web() -> Scope { - web::scope("/servers") + web::scope("/guilds") .service(post) .service(get) .service(uuid::web()) diff --git a/src/api/v1/servers/uuid/channels.rs b/src/api/v1/guilds/uuid/channels.rs similarity index 100% rename from src/api/v1/servers/uuid/channels.rs rename to src/api/v1/guilds/uuid/channels.rs diff --git a/src/api/v1/servers/uuid/icon.rs b/src/api/v1/guilds/uuid/icon.rs similarity index 100% rename from src/api/v1/servers/uuid/icon.rs rename to src/api/v1/guilds/uuid/icon.rs diff --git a/src/api/v1/servers/uuid/invites/id.rs b/src/api/v1/guilds/uuid/invites/id.rs similarity index 100% rename from src/api/v1/servers/uuid/invites/id.rs rename to src/api/v1/guilds/uuid/invites/id.rs diff --git a/src/api/v1/servers/uuid/invites/mod.rs b/src/api/v1/guilds/uuid/invites/mod.rs similarity index 100% rename from src/api/v1/servers/uuid/invites/mod.rs rename to src/api/v1/guilds/uuid/invites/mod.rs diff --git a/src/api/v1/servers/uuid/mod.rs b/src/api/v1/guilds/uuid/mod.rs similarity index 100% rename from src/api/v1/servers/uuid/mod.rs rename to src/api/v1/guilds/uuid/mod.rs diff --git a/src/api/v1/servers/uuid/roles/mod.rs b/src/api/v1/guilds/uuid/roles/mod.rs similarity index 100% rename from src/api/v1/servers/uuid/roles/mod.rs rename to src/api/v1/guilds/uuid/roles/mod.rs diff --git a/src/api/v1/servers/uuid/roles/uuid.rs b/src/api/v1/guilds/uuid/roles/uuid.rs similarity index 100% rename from src/api/v1/servers/uuid/roles/uuid.rs rename to src/api/v1/guilds/uuid/roles/uuid.rs diff --git a/src/api/v1/mod.rs b/src/api/v1/mod.rs index c08e1e3..421c10d 100644 --- a/src/api/v1/mod.rs +++ b/src/api/v1/mod.rs @@ -5,7 +5,7 @@ use actix_web::{Scope, web}; mod auth; mod channels; mod invites; -mod servers; +mod guilds; mod stats; mod users; mod me; @@ -16,7 +16,7 @@ pub fn web() -> Scope { .service(auth::web()) .service(users::web()) .service(channels::web()) - .service(servers::web()) + .service(guilds::web()) .service(invites::web()) .service(me::web()) } From 556337aa4e13617839e70d24e51ba46fbf9db6b2 Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:16:29 +0200 Subject: [PATCH 4/7] docs: fix paths in guild comments --- src/api/v1/guilds/mod.rs | 4 ++-- src/api/v1/guilds/uuid/icon.rs | 4 ++-- src/api/v1/guilds/uuid/mod.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/v1/guilds/mod.rs b/src/api/v1/guilds/mod.rs index 237abce..4c2dbf1 100644 --- a/src/api/v1/guilds/mod.rs +++ b/src/api/v1/guilds/mod.rs @@ -1,4 +1,4 @@ -//! `/api/v1/servers` Guild related endpoints +//! `/api/v1/guilds` Guild related endpoints use actix_web::{HttpRequest, HttpResponse, Scope, get, post, web}; use serde::Deserialize; @@ -21,7 +21,7 @@ pub fn web() -> Scope { .service(uuid::web()) } -/// `POST /api/v1/servers` Creates a new guild +/// `POST /api/v1/guilds` Creates a new guild /// /// requires auth: yes /// diff --git a/src/api/v1/guilds/uuid/icon.rs b/src/api/v1/guilds/uuid/icon.rs index 876aec4..0ac4470 100644 --- a/src/api/v1/guilds/uuid/icon.rs +++ b/src/api/v1/guilds/uuid/icon.rs @@ -1,4 +1,4 @@ -//! `/api/v1/servers/{uuid}/icon` icon related endpoints, will probably be replaced by a multipart post to above endpoint +//! `/api/v1/guilds/{uuid}/icon` icon related endpoints, will probably be replaced by a multipart post to above endpoint use actix_web::{HttpRequest, HttpResponse, put, web}; use futures_util::StreamExt as _; @@ -8,7 +8,7 @@ use crate::{ api::v1::auth::check_access_token, error::Error, structs::{Guild, Member}, utils::{get_auth_header, global_checks}, Data }; -/// `PUT /api/v1/servers/{uuid}/icon` Icon upload +/// `PUT /api/v1/guilds/{uuid}/icon` Icon upload /// /// requires auth: no /// diff --git a/src/api/v1/guilds/uuid/mod.rs b/src/api/v1/guilds/uuid/mod.rs index f874b91..7ab719d 100644 --- a/src/api/v1/guilds/uuid/mod.rs +++ b/src/api/v1/guilds/uuid/mod.rs @@ -1,4 +1,4 @@ -//! `/api/v1/servers/{uuid}` Specific server endpoints +//! `/api/v1/guilds/{uuid}` Specific server endpoints use actix_web::{HttpRequest, HttpResponse, Scope, get, web}; use uuid::Uuid; @@ -30,7 +30,7 @@ pub fn web() -> Scope { .service(icon::upload) } -/// `GET /api/v1/servers/{uuid}` DESCRIPTION +/// `GET /api/v1/guilds/{uuid}` DESCRIPTION /// /// requires auth: yes /// From 3c5f3fd6544ec418b04dcadec4181b255c1362e9 Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:29:45 +0200 Subject: [PATCH 5/7] style: rename url to frontend_url --- Dockerfile | 3 ++- compose.dev.yml | 2 +- compose.yml | 2 +- entrypoint.sh | 3 ++- src/config.rs | 6 +++--- src/structs.rs | 6 +++--- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index a8e8dc6..d7209ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,8 @@ RUN useradd --create-home --home-dir /gorb gorb USER gorb -ENV WEB_URL=https://gorb.app/web/ \ +ENV WEB_FRONTEND_URL=https://gorb.app/web/ \ +WEB_BASE_PATH=/api \ DATABASE_USERNAME=gorb \ DATABASE_PASSWORD=gorb \ DATABASE=gorb \ diff --git a/compose.dev.yml b/compose.dev.yml index e80f2a7..93a1a85 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -18,7 +18,7 @@ services: - gorb-backend:/gorb environment: #- RUST_LOG=debug - - WEB_URL=https://gorb.app/web/ + - WEB_FRONTEND_URL=https://gorb.app/web/ - DATABASE_USERNAME=gorb - DATABASE_PASSWORD=gorb - DATABASE=gorb diff --git a/compose.yml b/compose.yml index 2bc7339..b1dc07d 100644 --- a/compose.yml +++ b/compose.yml @@ -16,7 +16,7 @@ services: - gorb-backend:/gorb environment: #- RUST_LOG=debug - - WEB_URL=https://gorb.app/web/ + - WEB_FRONTEND_URL=https://gorb.app/web/ - DATABASE_USERNAME=gorb - DATABASE_PASSWORD=gorb - DATABASE=gorb diff --git a/entrypoint.sh b/entrypoint.sh index 9c7a401..38ba890 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -11,7 +11,8 @@ fi if [ ! -f "/gorb/config/config.toml" ]; then cat > /gorb/config/config.toml <, port: Option, - url: Url, + frontend_url: Url, _ssl: Option, } @@ -85,7 +85,7 @@ impl ConfigBuilder { let web = Web { ip: self.web.ip.unwrap_or(String::from("0.0.0.0")), port: self.web.port.unwrap_or(8080), - url: self.web.url, + frontend_url: self.web.frontend_url, }; let endpoint = match &*self.bunny.endpoint { @@ -146,7 +146,7 @@ pub struct Config { pub struct Web { pub ip: String, pub port: u16, - pub url: Url, + pub frontend_url: Url, } #[derive(Debug, Clone)] diff --git a/src/structs.rs b/src/structs.rs index 8d1d367..50615ff 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1014,7 +1014,7 @@ impl EmailToken { .execute(&mut conn) .await?; - let mut verify_endpoint = data.config.web.url.join("verify-email")?; + let mut verify_endpoint = data.config.web.frontend_url.join("verify-email")?; verify_endpoint.set_query(Some(&format!("token={}", token))); @@ -1104,7 +1104,7 @@ impl PasswordResetToken { .execute(&mut conn) .await?; - let mut reset_endpoint = data.config.web.url.join("reset-password")?; + let mut reset_endpoint = data.config.web.frontend_url.join("reset-password")?; reset_endpoint.set_query(Some(&format!("token={}", token))); @@ -1153,7 +1153,7 @@ impl PasswordResetToken { .get_result(&mut conn) .await?; - let login_page = data.config.web.url.join("login")?; + let login_page = data.config.web.frontend_url.join("login")?; let email = data .mail_client From 94c4428bb0d0eca3e3da1a9ac29148017df6c3ba Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 29 May 2025 20:41:50 +0200 Subject: [PATCH 6/7] feat: add base_path to api Lets you replace /api with whatever you want! --- src/api/mod.rs | 4 ++-- src/config.rs | 3 +++ src/main.rs | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 87c1c14..4cad2fa 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -6,6 +6,6 @@ use actix_web::web; mod v1; mod versions; -pub fn web() -> Scope { - web::scope("/api").service(v1::web()).service(versions::get) +pub fn web(path: &str) -> Scope { + web::scope(path.trim_end_matches('/')).service(v1::web()).service(versions::get) } diff --git a/src/config.rs b/src/config.rs index 8a274b4..464c98d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -38,6 +38,7 @@ pub struct CacheDatabase { struct WebBuilder { ip: Option, port: Option, + base_path: Option, frontend_url: Url, _ssl: Option, } @@ -85,6 +86,7 @@ impl ConfigBuilder { let web = Web { ip: self.web.ip.unwrap_or(String::from("0.0.0.0")), port: self.web.port.unwrap_or(8080), + base_path: self.web.base_path.unwrap_or("".to_string()), frontend_url: self.web.frontend_url, }; @@ -146,6 +148,7 @@ pub struct Config { pub struct Web { pub ip: String, pub port: u16, + pub base_path: String, pub frontend_url: Url, } diff --git a/src/main.rs b/src/main.rs index 0f94be8..5670e7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,7 +150,7 @@ async fn main() -> Result<(), Error> { App::new() .app_data(web::Data::new(data.clone())) .wrap(cors) - .service(api::web()) + .service(api::web(&data.config.web.base_path)) }) .bind((web.ip, web.port))? .run() From 55e343507e20e2ca5d88915e164249df324e51d1 Mon Sep 17 00:00:00 2001 From: Radical Date: Fri, 30 May 2025 08:37:45 +0000 Subject: [PATCH 7/7] style: move /me/servers to /me/guilds --- src/api/v1/me/{servers.rs => guilds.rs} | 6 +++--- src/api/v1/me/mod.rs | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) rename src/api/v1/me/{servers.rs => guilds.rs} (89%) diff --git a/src/api/v1/me/servers.rs b/src/api/v1/me/guilds.rs similarity index 89% rename from src/api/v1/me/servers.rs rename to src/api/v1/me/guilds.rs index ae026a7..1e92a34 100644 --- a/src/api/v1/me/servers.rs +++ b/src/api/v1/me/guilds.rs @@ -1,11 +1,11 @@ -//! `/api/v1/me/servers` Contains endpoint related to guild memberships +//! `/api/v1/me/guilds` Contains endpoint related to guild memberships use actix_web::{get, web, HttpRequest, HttpResponse}; use crate::{api::v1::auth::check_access_token, error::Error, structs::Me, utils::{get_auth_header, global_checks}, Data}; -/// `GET /api/v1/me/servers` Returns all guild memberships in a list +/// `GET /api/v1/me/guilds` Returns all guild memberships in a list /// /// requires auth: yes /// @@ -27,7 +27,7 @@ use crate::{api::v1::auth::check_access_token, error::Error, structs::Me, utils: /// ]); /// ``` /// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps -#[get("/servers")] +#[get("/guilds")] pub async fn get(req: HttpRequest, data: web::Data) -> Result { let headers = req.headers(); diff --git a/src/api/v1/me/mod.rs b/src/api/v1/me/mod.rs index cd86e43..ac24342 100644 --- a/src/api/v1/me/mod.rs +++ b/src/api/v1/me/mod.rs @@ -6,10 +6,13 @@ use crate::{ api::v1::auth::check_access_token, error::Error, structs::Me, utils::{get_auth_header, global_checks}, Data }; -mod servers; +mod guilds; pub fn web() -> Scope { - web::scope("/me").service(get).service(update) + web::scope("/me") + .service(get) + .service(update) + .service(guilds::get) } #[get("")]