use a trait object for the server, for dynamic dispatch

This commit is contained in:
Dustin J. Mitchell 2020-11-26 10:52:28 -05:00
parent 087333a227
commit a5c06008b3
5 changed files with 36 additions and 15 deletions

View file

@ -1,8 +1,7 @@
use crate::server::SyncServer;
use crate::api::ServerState;
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)]
@ -14,7 +13,7 @@ pub(crate) struct AddVersionRequest {
#[post("/client/{client_id}/add-version/{parent_version_id}")]
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)>,
body: web::Json<AddVersionRequest>,
) -> Result<impl Responder> {

View file

@ -1,11 +1,10 @@
use crate::server::SyncServer;
use crate::api::ServerState;
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>>,
data: web::Data<ServerState>,
web::Path((client_id, parent_version_id)): web::Path<(ClientId, VersionId)>,
) -> Result<HttpResponse> {
let result = data

View file

@ -1,2 +1,8 @@
use crate::server::SyncServer;
use std::sync::Arc;
pub(crate) mod add_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>>;

View file

@ -1,5 +1,4 @@
use actix_web::{App, HttpServer};
use server::SyncServer;
use std::sync::Arc;
mod api;
@ -10,11 +9,11 @@ mod types;
#[actix_web::main]
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 || {
App::new()
.data(sync_server.clone())
.data(server_state.clone())
.service(api::get_child_version::service)
.service(api::add_version::service)
})

View file

@ -2,16 +2,34 @@ use crate::types::{AddVersionResult, ClientId, GetVersionResult, HistorySegment,
use failure::Fallible;
use taskchampion::Uuid;
/// The sync server's implementation; HTTP API method call through to methods on a single
/// instance of this type.
pub(crate) struct SyncServer {}
pub(crate) trait SyncServer {
fn get_child_version(
&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 {
Self {}
}
}
pub(crate) fn get_child_version(
impl SyncServer for NullSyncServer {
fn get_child_version(
&self,
_client_id: ClientId,
parent_version_id: VersionId,
@ -23,7 +41,7 @@ impl SyncServer {
}))
}
pub(crate) fn add_version(
fn add_version(
&self,
_client_id: ClientId,
_parent_version_id: VersionId,