use a trait object for the server, for dynamic dispatch
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
use crate::server::SyncServer;
|
use crate::api::ServerState;
|
||||||
use crate::types::{ClientId, HistorySegment, VersionId};
|
use crate::types::{ClientId, HistorySegment, VersionId};
|
||||||
use actix_web::{error, http::StatusCode, post, web, HttpResponse, Responder, Result};
|
use actix_web::{error, http::StatusCode, post, web, HttpResponse, Responder, Result};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
/// Request body to add_version
|
/// Request body to add_version
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@@ -14,7 +13,7 @@ pub(crate) struct AddVersionRequest {
|
|||||||
|
|
||||||
#[post("/client/{client_id}/add-version/{parent_version_id}")]
|
#[post("/client/{client_id}/add-version/{parent_version_id}")]
|
||||||
pub(crate) async fn service(
|
pub(crate) async fn service(
|
||||||
data: web::Data<Arc<SyncServer>>,
|
data: web::Data<ServerState>,
|
||||||
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
|
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
|
||||||
body: web::Json<AddVersionRequest>,
|
body: web::Json<AddVersionRequest>,
|
||||||
) -> Result<impl Responder> {
|
) -> Result<impl Responder> {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use crate::server::SyncServer;
|
use crate::api::ServerState;
|
||||||
use crate::types::{ClientId, VersionId};
|
use crate::types::{ClientId, VersionId};
|
||||||
use actix_web::{error, get, http::StatusCode, web, HttpResponse, Result};
|
use actix_web::{error, get, http::StatusCode, web, HttpResponse, Result};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[get("/client/{client_id}/get-child-version/{parent_version_id}")]
|
#[get("/client/{client_id}/get-child-version/{parent_version_id}")]
|
||||||
pub(crate) async fn service(
|
pub(crate) async fn service(
|
||||||
data: web::Data<Arc<SyncServer>>,
|
data: web::Data<ServerState>,
|
||||||
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
|
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
|
||||||
) -> Result<HttpResponse> {
|
) -> Result<HttpResponse> {
|
||||||
let result = data
|
let result = data
|
||||||
|
|||||||
@@ -1,2 +1,8 @@
|
|||||||
|
use crate::server::SyncServer;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub(crate) mod add_version;
|
pub(crate) mod add_version;
|
||||||
pub(crate) mod get_child_version;
|
pub(crate) mod get_child_version;
|
||||||
|
|
||||||
|
/// The type containing a reference to the SyncServer object in the Actix state.
|
||||||
|
pub(crate) type ServerState = Arc<Box<dyn SyncServer>>;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use actix_web::{App, HttpServer};
|
use actix_web::{App, HttpServer};
|
||||||
use server::SyncServer;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
@@ -10,11 +9,11 @@ mod types;
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let sync_server = Arc::new(SyncServer::new());
|
let server_state = Arc::new(Box::new(server::NullSyncServer::new()));
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(sync_server.clone())
|
.data(server_state.clone())
|
||||||
.service(api::get_child_version::service)
|
.service(api::get_child_version::service)
|
||||||
.service(api::add_version::service)
|
.service(api::add_version::service)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,16 +2,34 @@ use crate::types::{AddVersionResult, ClientId, GetVersionResult, HistorySegment,
|
|||||||
use failure::Fallible;
|
use failure::Fallible;
|
||||||
use taskchampion::Uuid;
|
use taskchampion::Uuid;
|
||||||
|
|
||||||
/// The sync server's implementation; HTTP API method call through to methods on a single
|
pub(crate) trait SyncServer {
|
||||||
/// instance of this type.
|
fn get_child_version(
|
||||||
pub(crate) struct SyncServer {}
|
&self,
|
||||||
|
client_id: ClientId,
|
||||||
|
parent_version_id: VersionId,
|
||||||
|
) -> Fallible<Option<GetVersionResult>>;
|
||||||
|
|
||||||
impl SyncServer {
|
fn add_version(
|
||||||
|
&self,
|
||||||
|
client_id: ClientId,
|
||||||
|
parent_version_id: VersionId,
|
||||||
|
history_segment: &HistorySegment,
|
||||||
|
) -> Fallible<AddVersionResult>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: temporary
|
||||||
|
/// A "null" sync server's implementation; HTTP API methods call through to methods on a single
|
||||||
|
/// instance of this type.
|
||||||
|
pub(crate) struct NullSyncServer {}
|
||||||
|
|
||||||
|
impl NullSyncServer {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_child_version(
|
impl SyncServer for NullSyncServer {
|
||||||
|
fn get_child_version(
|
||||||
&self,
|
&self,
|
||||||
_client_id: ClientId,
|
_client_id: ClientId,
|
||||||
parent_version_id: VersionId,
|
parent_version_id: VersionId,
|
||||||
@@ -23,7 +41,7 @@ impl SyncServer {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_version(
|
fn add_version(
|
||||||
&self,
|
&self,
|
||||||
_client_id: ClientId,
|
_client_id: ClientId,
|
||||||
_parent_version_id: VersionId,
|
_parent_version_id: VersionId,
|
||||||
Reference in New Issue
Block a user