Add Imfs to ServeSession, threading through generic ImfsFetcher

This commit is contained in:
Lucien Greathouse
2019-09-13 12:52:15 -07:00
parent 651e63a0fb
commit 8481caa67c
5 changed files with 37 additions and 29 deletions

View File

@@ -70,7 +70,7 @@ pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
let patch_set = compute_patch_set(&snapshot, &tree, root_id);
apply_patch_set(&mut tree, &patch_set);
let session = Arc::new(ServeSession::new(tree, maybe_project));
let session = Arc::new(ServeSession::new(imfs, tree, maybe_project));
let server = LiveServer::new(session);
server.start(port);

View File

@@ -1,19 +1,24 @@
use std::collections::HashSet;
use crate::{
message_queue::MessageQueue, project::Project, session_id::SessionId, snapshot::RojoTree,
imfs::new::{Imfs, ImfsFetcher},
message_queue::MessageQueue,
project::Project,
session_id::SessionId,
snapshot::RojoTree,
};
/// Contains all of the state for a Rojo serve session.
pub struct ServeSession {
pub struct ServeSession<F> {
root_project: Option<Project>,
session_id: SessionId,
tree: RojoTree,
message_queue: MessageQueue<()>, // TODO: Real message type
imfs: Imfs<F>,
}
impl ServeSession {
pub fn new(tree: RojoTree, root_project: Option<Project>) -> ServeSession {
impl<F: ImfsFetcher> ServeSession<F> {
pub fn new(imfs: Imfs<F>, tree: RojoTree, root_project: Option<Project>) -> Self {
let session_id = SessionId::new();
let message_queue = MessageQueue::new();
@@ -22,6 +27,7 @@ impl ServeSession {
root_project,
tree,
message_queue,
imfs,
}
}

View File

@@ -9,6 +9,7 @@ use hyper::{header, service::Service, Body, Method, Request, Response, StatusCod
use rbx_dom_weak::RbxId;
use crate::{
imfs::new::ImfsFetcher,
serve_session::ServeSession,
web::{
interface::{
@@ -19,11 +20,11 @@ use crate::{
},
};
pub struct ApiService {
serve_session: Arc<ServeSession>,
pub struct ApiService<F> {
serve_session: Arc<ServeSession<F>>,
}
impl Service for ApiService {
impl<F: ImfsFetcher> Service for ApiService<F> {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
@@ -42,13 +43,13 @@ impl Service for ApiService {
}
}
impl ApiService {
pub fn new(serve_session: Arc<ServeSession>) -> ApiService {
impl<F: ImfsFetcher> ApiService<F> {
pub fn new(serve_session: Arc<ServeSession<F>>) -> Self {
ApiService { serve_session }
}
/// Get a summary of information about the server
fn handle_api_rojo(&self) -> <ApiService as Service>::Future {
fn handle_api_rojo(&self) -> <Self as Service>::Future {
let tree = self.serve_session.tree();
let root_instance_id = tree.get_root_id();
@@ -63,7 +64,7 @@ impl ApiService {
/// Retrieve any messages past the given cursor index, and if
/// there weren't any, subscribe to receive any new messages.
fn handle_api_subscribe(&self, request: Request<Body>) -> <ApiService as Service>::Future {
fn handle_api_subscribe(&self, request: Request<Body>) -> <Self as Service>::Future {
let argument = &request.uri().path()["/api/subscribe/".len()..];
let _input_cursor: u32 = match argument.parse() {
Ok(v) => v,
@@ -93,7 +94,7 @@ impl ApiService {
// })
}
fn handle_api_read(&self, request: Request<Body>) -> <ApiService as Service>::Future {
fn handle_api_read(&self, request: Request<Body>) -> <Self as Service>::Future {
let argument = &request.uri().path()["/api/read/".len()..];
let requested_ids: Option<Vec<RbxId>> = argument.split(',').map(RbxId::parse_str).collect();

View File

@@ -12,16 +12,16 @@ use futures::{
use hyper::{service::Service, Body, Request, Response, Server};
use log::trace;
use crate::serve_session::ServeSession;
use crate::{imfs::new::ImfsFetcher, serve_session::ServeSession};
use self::{api::ApiService, ui::UiService};
pub struct RootService {
api: ApiService,
ui: UiService,
pub struct RootService<F> {
api: ApiService<F>,
ui: UiService<F>,
}
impl Service for RootService {
impl<F: ImfsFetcher> Service for RootService<F> {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
@@ -38,8 +38,8 @@ impl Service for RootService {
}
}
impl RootService {
pub fn new(serve_session: Arc<ServeSession>) -> RootService {
impl<F: ImfsFetcher> RootService<F> {
pub fn new(serve_session: Arc<ServeSession<F>>) -> Self {
RootService {
api: ApiService::new(Arc::clone(&serve_session)),
ui: UiService::new(Arc::clone(&serve_session)),
@@ -47,12 +47,12 @@ impl RootService {
}
}
pub struct LiveServer {
serve_session: Arc<ServeSession>,
pub struct LiveServer<F> {
serve_session: Arc<ServeSession<F>>,
}
impl LiveServer {
pub fn new(serve_session: Arc<ServeSession>) -> LiveServer {
impl<F: ImfsFetcher + Send + Sync + 'static> LiveServer<F> {
pub fn new(serve_session: Arc<ServeSession<F>>) -> Self {
LiveServer { serve_session }
}

View File

@@ -7,6 +7,7 @@ use hyper::{header, service::Service, Body, Method, Request, Response, StatusCod
use ritz::html;
use crate::{
imfs::new::ImfsFetcher,
serve_session::ServeSession,
web::{
interface::{NotFoundError, SERVER_VERSION},
@@ -16,12 +17,12 @@ use crate::{
static HOME_CSS: &str = include_str!("../../assets/index.css");
pub struct UiService {
pub struct UiService<F> {
#[allow(unused)] // TODO: Fill out interface service
serve_session: Arc<ServeSession>,
serve_session: Arc<ServeSession<F>>,
}
impl Service for UiService {
impl<F: ImfsFetcher> Service for UiService<F> {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
@@ -39,8 +40,8 @@ impl Service for UiService {
}
}
impl UiService {
pub fn new(serve_session: Arc<ServeSession>) -> UiService {
impl<F: ImfsFetcher> UiService<F> {
pub fn new(serve_session: Arc<ServeSession<F>>) -> Self {
UiService { serve_session }
}