Rename user-agent header to device-id
This commit is contained in:
parent
5448c1cf99
commit
70fe45a09d
8 changed files with 94 additions and 21 deletions
|
|
@ -1,6 +1,7 @@
|
|||
mod auth;
|
||||
mod create_document;
|
||||
mod delete_document;
|
||||
mod device_id_header;
|
||||
mod fetch_document_version;
|
||||
mod fetch_document_version_content;
|
||||
mod fetch_latest_document_version;
|
||||
|
|
@ -32,6 +33,7 @@ use axum::{
|
|||
response::IntoResponse,
|
||||
routing::IntoMakeService,
|
||||
};
|
||||
use device_id_header::DEVICE_ID_HEADER_NAME;
|
||||
use log::{error, info};
|
||||
use tokio::signal;
|
||||
use tower_http::{
|
||||
|
|
@ -79,7 +81,11 @@ pub async fn create_server(config_path: Option<OsString>) -> Result<()> {
|
|||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin("*".parse::<HeaderValue>().expect("Failed to parse origin"))
|
||||
.allow_headers([http::header::CONTENT_TYPE, http::header::AUTHORIZATION])
|
||||
.allow_headers([
|
||||
http::header::CONTENT_TYPE,
|
||||
http::header::AUTHORIZATION,
|
||||
DEVICE_ID_HEADER_NAME.clone(),
|
||||
])
|
||||
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE]),
|
||||
)
|
||||
.layer(
|
||||
|
|
|
|||
|
|
@ -4,13 +4,16 @@ use axum::{
|
|||
Extension,
|
||||
extract::{Path, State},
|
||||
};
|
||||
use axum_extra::{TypedHeader, headers::UserAgent};
|
||||
use axum_extra::TypedHeader;
|
||||
use axum_jsonschema::Json;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use sync_lib::base64_to_bytes;
|
||||
|
||||
use super::requests::{CreateDocumentVersion, CreateDocumentVersionMultipart};
|
||||
use super::{
|
||||
device_id_header::DeviceIdHeader,
|
||||
requests::{CreateDocumentVersion, CreateDocumentVersionMultipart},
|
||||
};
|
||||
use crate::{
|
||||
app_state::{
|
||||
AppState,
|
||||
|
|
@ -38,7 +41,7 @@ pub struct CreateDocumentPathParams {
|
|||
pub async fn create_document_multipart(
|
||||
Path(CreateDocumentPathParams { vault_id }): Path<CreateDocumentPathParams>,
|
||||
Extension(user): Extension<User>,
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
TypedHeader(user_agent): TypedHeader<DeviceIdHeader>,
|
||||
State(state): State<AppState>,
|
||||
TypedMultipart(axum_typed_multipart::TypedMultipart(request)): TypedMultipart<
|
||||
CreateDocumentVersionMultipart,
|
||||
|
|
@ -64,7 +67,7 @@ pub async fn create_document_multipart(
|
|||
pub async fn create_document_json(
|
||||
Path(CreateDocumentPathParams { vault_id }): Path<CreateDocumentPathParams>,
|
||||
Extension(user): Extension<User>,
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
TypedHeader(user_agent): TypedHeader<DeviceIdHeader>,
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<CreateDocumentVersion>,
|
||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||
|
|
@ -88,7 +91,7 @@ pub async fn create_document_json(
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
async fn internal_create_document(
|
||||
user: User,
|
||||
user_agent: UserAgent,
|
||||
user_agent: DeviceIdHeader,
|
||||
state: AppState,
|
||||
vault_id: VaultId,
|
||||
document_id: Option<DocumentId>,
|
||||
|
|
@ -137,7 +140,7 @@ async fn internal_create_document(
|
|||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: false,
|
||||
user_id: user.name,
|
||||
device_id: user_agent.to_string(),
|
||||
device_id: user_agent.0,
|
||||
};
|
||||
|
||||
state
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ use axum::{
|
|||
Extension,
|
||||
extract::{Path, State},
|
||||
};
|
||||
use axum_extra::{TypedHeader, headers::UserAgent};
|
||||
use axum_extra::TypedHeader;
|
||||
use axum_jsonschema::Json;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::requests::DeleteDocumentVersion;
|
||||
use super::{device_id_header::DeviceIdHeader, requests::DeleteDocumentVersion};
|
||||
use crate::{
|
||||
app_state::{
|
||||
AppState,
|
||||
|
|
@ -38,7 +38,7 @@ pub async fn delete_document(
|
|||
document_id,
|
||||
}): Path<DeleteDocumentPathParams>,
|
||||
Extension(user): Extension<User>,
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
TypedHeader(user_agent): TypedHeader<DeviceIdHeader>,
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<DeleteDocumentVersion>,
|
||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||
|
|
@ -76,7 +76,7 @@ pub async fn delete_document(
|
|||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: true,
|
||||
user_id: user.name,
|
||||
device_id: user_agent.to_string(),
|
||||
device_id: user_agent.0,
|
||||
};
|
||||
|
||||
state
|
||||
|
|
|
|||
33
backend/sync_server/src/server/device_id_header.rs
Normal file
33
backend/sync_server/src/server/device_id_header.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use axum_extra::headers;
|
||||
use headers::{Header, HeaderName, HeaderValue};
|
||||
|
||||
pub struct DeviceIdHeader(pub String);
|
||||
|
||||
pub static DEVICE_ID_HEADER_NAME: HeaderName = HeaderName::from_static("device-id");
|
||||
|
||||
impl Header for DeviceIdHeader {
|
||||
fn name() -> &'static HeaderName { &DEVICE_ID_HEADER_NAME }
|
||||
|
||||
fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
|
||||
where
|
||||
I: Iterator<Item = &'i HeaderValue>,
|
||||
{
|
||||
let value = values.next().ok_or_else(headers::Error::invalid)?;
|
||||
|
||||
Ok(DeviceIdHeader(
|
||||
value
|
||||
.to_str()
|
||||
.map_err(|_| headers::Error::invalid())?
|
||||
.to_owned(),
|
||||
))
|
||||
}
|
||||
|
||||
fn encode<E>(&self, values: &mut E)
|
||||
where
|
||||
E: Extend<HeaderValue>,
|
||||
{
|
||||
let value = HeaderValue::from_static(Box::leak(self.0.to_string().into_boxed_str()));
|
||||
|
||||
values.extend(std::iter::once(value));
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ use axum::{
|
|||
Extension,
|
||||
extract::{Path, State},
|
||||
};
|
||||
use axum_extra::{TypedHeader, headers::UserAgent};
|
||||
use axum_extra::TypedHeader;
|
||||
use axum_jsonschema::Json;
|
||||
use log::info;
|
||||
use schemars::JsonSchema;
|
||||
|
|
@ -12,6 +12,7 @@ use serde::Deserialize;
|
|||
use sync_lib::{base64_to_bytes, is_file_type_mergable, merge};
|
||||
|
||||
use super::{
|
||||
device_id_header::DeviceIdHeader,
|
||||
requests::{UpdateDocumentVersion, UpdateDocumentVersionMultipart},
|
||||
responses::DocumentUpdateResponse,
|
||||
};
|
||||
|
|
@ -42,7 +43,7 @@ pub async fn update_document_multipart(
|
|||
document_id,
|
||||
}): Path<UpdateDocumentPathParams>,
|
||||
Extension(user): Extension<User>,
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
TypedHeader(user_agent): TypedHeader<DeviceIdHeader>,
|
||||
State(state): State<AppState>,
|
||||
TypedMultipart(axum_typed_multipart::TypedMultipart(request)): TypedMultipart<
|
||||
UpdateDocumentVersionMultipart,
|
||||
|
|
@ -69,7 +70,7 @@ pub async fn update_document_json(
|
|||
document_id,
|
||||
}): Path<UpdateDocumentPathParams>,
|
||||
Extension(user): Extension<User>,
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
TypedHeader(user_agent): TypedHeader<DeviceIdHeader>,
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<UpdateDocumentVersion>,
|
||||
) -> Result<Json<DocumentUpdateResponse>, SyncServerError> {
|
||||
|
|
@ -94,7 +95,7 @@ pub async fn update_document_json(
|
|||
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
|
||||
async fn internal_update_document(
|
||||
user: User,
|
||||
user_agent: UserAgent,
|
||||
user_agent: DeviceIdHeader,
|
||||
state: AppState,
|
||||
vault_id: VaultId,
|
||||
document_id: DocumentId,
|
||||
|
|
@ -214,7 +215,7 @@ async fn internal_update_document(
|
|||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: false,
|
||||
user_id: user.name,
|
||||
device_id: user_agent.to_string(),
|
||||
device_id: user_agent.0,
|
||||
};
|
||||
|
||||
state
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue