Skip to content

Commit

Permalink
feat: add server info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
khorshuheng committed Sep 20, 2024
1 parent ece8243 commit e8d5b29
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
23 changes: 23 additions & 0 deletions libs/client-api/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::notify::{ClientToken, TokenStateReceiver};
use app_error::AppError;
use app_error::ErrorCode;
use client_api_entity::auth_dto::DeleteUserQuery;
use client_api_entity::server_info_dto::ServerInfoResponseItem;
use client_api_entity::workspace_dto::FolderView;
use client_api_entity::workspace_dto::QueryWorkspaceFolder;
use client_api_entity::workspace_dto::QueryWorkspaceParam;
Expand All @@ -11,6 +13,7 @@ use gotrue::grant::PasswordGrant;
use gotrue::grant::{Grant, RefreshTokenGrant};
use gotrue::params::MagicLinkParams;
use gotrue::params::{AdminUserParams, GenerateLinkParams};
use reqwest::StatusCode;
use shared_entity::dto::workspace_dto::{CreateWorkspaceParam, PatchWorkspaceParam};
use std::fmt::{Display, Formatter};
#[cfg(feature = "enable_brotli")]
Expand Down Expand Up @@ -967,6 +970,26 @@ impl Client {
.into_data()
}

#[instrument(level = "info", skip_all)]
pub async fn get_server_info(&self) -> Result<ServerInfoResponseItem, AppResponseError> {
let url = format!("{}/api/server", self.base_url);
let resp = self
.http_client_with_auth(Method::GET, &url)
.await?
.send()
.await?;
if resp.status() == StatusCode::NOT_FOUND {
Err(AppResponseError::new(
ErrorCode::Unhandled,
"server info not implemented",
))
} else {
AppResponse::<ServerInfoResponseItem>::from_response(resp)
.await?
.into_data()
}
}

// Refresh token if given timestamp is close to the token expiration time
pub async fn refresh_if_expired(&self, ts: i64, reason: &str) -> Result<(), AppResponseError> {
let expires_at = self.token_expires_at()?;
Expand Down
10 changes: 9 additions & 1 deletion libs/shared-entity/src/dto/server_info_dto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum SupportedClientFeatures {
// Supports Collab Params serialization using Bincode
CollabParamsBincode,
// Supports Collab Params serialization using Protobuf
CollabParamsProtobuf,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerInfoResponseItem {
pub version: String,
pub supported_client_features: Vec<SupportedClientFeatures>,
}
4 changes: 2 additions & 2 deletions src/api/server_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{web, Scope};
use shared_entity::dto::server_info_dto::ServerInfoResponseItem;
use shared_entity::dto::server_info_dto::{ServerInfoResponseItem, SupportedClientFeatures};
use shared_entity::response::{AppResponse, JsonAppResponse};

pub fn server_info_scope() -> Scope {
Expand All @@ -10,7 +10,7 @@ async fn server_info_handler() -> actix_web::Result<JsonAppResponse<ServerInfoRe
Ok(
AppResponse::Ok()
.with_data(ServerInfoResponseItem {
version: env!("CARGO_PKG_VERSION").to_string(),
supported_client_features: vec![SupportedClientFeatures::CollabParamsBincode],
})
.into(),
)
Expand Down
2 changes: 2 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::api::file_storage::file_storage_scope;
use crate::api::history::history_scope;
use crate::api::metrics::metrics_scope;
use crate::api::search::search_scope;
use crate::api::server_info::server_info_scope;
use crate::api::template::template_scope;
use crate::api::user::user_scope;
use crate::api::workspace::{collab_scope, workspace_scope};
Expand Down Expand Up @@ -158,6 +159,7 @@ pub async fn run_actix_server(
// .wrap(DecryptPayloadMiddleware)
.wrap(access_control.clone())
.wrap(RequestIdMiddleware)
.service(server_info_scope())
.service(user_scope())
.service(workspace_scope())
.service(collab_scope())
Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod collab_history;
mod file_test;
mod gotrue;
mod search;
mod server_info;
mod sql_test;
mod user;
mod websocket;
Expand Down
19 changes: 19 additions & 0 deletions tests/server_info/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use client_api_test::generate_unique_registered_user_client;
use shared_entity::dto::server_info_dto::SupportedClientFeatures;

#[tokio::test]
async fn test_get_server_info() {
let (c, _) = generate_unique_registered_user_client().await;
let resp = c
.get_server_info()
.await
.expect("Failed to get server info");
let expected_features = vec![SupportedClientFeatures::CollabParamsBincode];
assert_eq!(
resp.supported_client_features.len(),
expected_features.len()
);
for i in 0..resp.supported_client_features.len() {
assert_eq!(resp.supported_client_features[i], expected_features[i]);
}
}
1 change: 1 addition & 0 deletions tests/server_info/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod info;

0 comments on commit e8d5b29

Please sign in to comment.