From 470ddb818c6129d81ca75899ce69b54ed103acbe Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 30 Jan 2024 12:36:31 +0800 Subject: [PATCH 1/2] feat: add api version as github sha and client timestamp to header --- Cargo.lock | 16 ---------------- Cargo.toml | 5 ----- libs/client-api/src/http.rs | 21 ++++++++++++--------- 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1aa68f6fc..1a8c1e012 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,21 +44,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "actix-cors" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370" -dependencies = [ - "actix-utils", - "actix-web", - "derive_more", - "futures-util", - "log", - "once_cell", - "smallvec", -] - [[package]] name = "actix-http" version = "3.5.1" @@ -513,7 +498,6 @@ name = "appflowy-cloud" version = "0.1.0" dependencies = [ "actix", - "actix-cors", "actix-http", "actix-identity", "actix-router", diff --git a/Cargo.toml b/Cargo.toml index 3db9679a3..3210f6ff5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ actix-rt = "2" actix-web-actors = { version = "4.2.0" } actix-service = "2.0.2" actix-identity = "0.6.0" -actix-cors = "0.6.5" actix-router = "0.5.2" actix-session = { version = "0.8", features = ["redis-rs-tls-session"] } openssl = { version = "0.10.62", features = ["vendored"] } @@ -134,7 +133,6 @@ shared-entity = { path = "libs/shared-entity" } app-error = { path = "libs/app_error" } serde_json = "1.0.111" serde = { version = "1.0.195", features = ["derive"] } -serde_repr = "0.1.18" bytes = "1.5.0" workspace-template = { path = "libs/workspace-template" } uuid = { version = "1.6.1", features = ["v4"] } @@ -160,9 +158,6 @@ codegen-units = 16 debug = true lto = false -[profile.dev.package.sqlx-macros] -opt-level = 3 - [patch.crates-io] collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" } collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" } diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 024cc3354..56fdaf1a0 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -46,7 +46,7 @@ use url::Url; use gotrue_entity::dto::SignUpResponse::{Authenticated, NotAuthenticated}; use gotrue_entity::dto::{GotrueTokenResponse, UpdateGotrueUserParams, User}; -pub const CLIENT_API_VERSION: &str = "0.0.3"; +pub const CLIENT_API_VERSION: Option<&'static str> = std::option_env!("GITHUB_SHA"); pub const X_COMPRESSION_TYPE: &str = "X-Compression-Type"; pub const X_COMPRESSION_BUFFER_SIZE: &str = "X-Compression-Buffer-Size"; pub const X_COMPRESSION_TYPE_BROTLI: &str = "brotli"; @@ -987,7 +987,9 @@ impl Client { } pub async fn ws_url(&self, device_id: &str) -> Result { - self.refresh_if_required().await?; + self + .refresh_if_expired(chrono::Local::now().timestamp()) + .await?; let access_token = self.access_token()?; Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id)) @@ -1151,13 +1153,12 @@ impl Client { .into_data() } - pub async fn refresh_if_required(&self) -> Result<(), AppResponseError> { + // Refresh token if given timestamp is close to the token expiration time + pub async fn refresh_if_expired(&self, ts: i64) -> Result<(), AppResponseError> { let expires_at = self.token_expires_at()?; - // Refresh token if it's about to expire - let time_now_sec = chrono::Local::now().timestamp(); - if time_now_sec + 10 > expires_at { - // Add 10 seconds buffer + if ts + 30 > expires_at { + // Add 30 seconds buffer self.refresh_token().await?; } Ok(()) @@ -1169,14 +1170,16 @@ impl Client { method: Method, url: &str, ) -> Result { - self.refresh_if_required().await?; + let ts_now = chrono::Local::now().timestamp(); + self.refresh_if_expired(ts_now).await?; let access_token = self.access_token()?; trace!("start request: {}, method: {}", url, method); let request_builder = self .cloud_client .request(method, url) - .header("client-version", CLIENT_API_VERSION) + .header("client-version", CLIENT_API_VERSION.unwrap_or("unknown")) + .header("client-timestamp", ts_now.to_string()) .bearer_auth(access_token); Ok(request_builder) } From 719d57936370768f73aee4baaf68902a524f4e2f Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 30 Jan 2024 12:52:18 +0800 Subject: [PATCH 2/2] feat: use semantic versioning instead --- libs/client-api/src/http.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 56fdaf1a0..96109bbf2 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -46,7 +46,7 @@ use url::Url; use gotrue_entity::dto::SignUpResponse::{Authenticated, NotAuthenticated}; use gotrue_entity::dto::{GotrueTokenResponse, UpdateGotrueUserParams, User}; -pub const CLIENT_API_VERSION: Option<&'static str> = std::option_env!("GITHUB_SHA"); +pub const CLIENT_API_VERSION: &str = "0.0.3"; pub const X_COMPRESSION_TYPE: &str = "X-Compression-Type"; pub const X_COMPRESSION_BUFFER_SIZE: &str = "X-Compression-Buffer-Size"; pub const X_COMPRESSION_TYPE_BROTLI: &str = "brotli"; @@ -1178,7 +1178,7 @@ impl Client { let request_builder = self .cloud_client .request(method, url) - .header("client-version", CLIENT_API_VERSION.unwrap_or("unknown")) + .header("client-version", CLIENT_API_VERSION) .header("client-timestamp", ts_now.to_string()) .bearer_auth(access_token); Ok(request_builder)