refactor sync server into modules

This commit is contained in:
Dustin J. Mitchell
2020-11-25 23:16:05 -05:00
parent d0bfbbb7f0
commit 087333a227
9 changed files with 1504 additions and 102 deletions

View File

@@ -0,0 +1,25 @@
use crate::server::SyncServer;
use crate::types::{ClientId, HistorySegment, VersionId};
use actix_web::{error, http::StatusCode, post, web, HttpResponse, Responder, Result};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
/// Request body to add_version
#[derive(Serialize, Deserialize)]
pub(crate) struct AddVersionRequest {
// TODO: temporary!
#[serde(default)]
history_segment: HistorySegment,
}
#[post("/client/{client_id}/add-version/{parent_version_id}")]
pub(crate) async fn service(
data: web::Data<Arc<SyncServer>>,
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
body: web::Json<AddVersionRequest>,
) -> Result<impl Responder> {
let result = data
.add_version(client_id, parent_version_id, &body.history_segment)
.map_err(|e| error::InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))?;
Ok(HttpResponse::Ok().json(result))
}

View File

@@ -0,0 +1,19 @@
use crate::server::SyncServer;
use crate::types::{ClientId, VersionId};
use actix_web::{error, get, http::StatusCode, web, HttpResponse, Result};
use std::sync::Arc;
#[get("/client/{client_id}/get-child-version/{parent_version_id}")]
pub(crate) async fn service(
data: web::Data<Arc<SyncServer>>,
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
) -> Result<HttpResponse> {
let result = data
.get_child_version(client_id, parent_version_id)
.map_err(|e| error::InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))?;
if let Some(result) = result {
Ok(HttpResponse::Ok().json(result))
} else {
Err(error::ErrorNotFound("no such version"))
}
}

View File

@@ -0,0 +1,2 @@
pub(crate) mod add_version;
pub(crate) mod get_child_version;