From 105d8aeb6b795e4016c72e5f4f800e87a883d505 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 22 Feb 2019 13:08:07 -0800 Subject: [PATCH] Start to stub out sub-services --- server/src/commands/serve.rs | 6 +++--- server/src/web/api.rs | 16 +++++++++++++++ server/src/web/interface.rs | 16 +++++++++++++++ server/src/web/mod.rs | 38 +++++++++++++++++++++++++----------- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/server/src/commands/serve.rs b/server/src/commands/serve.rs index 58c0d16e..9e7fac63 100644 --- a/server/src/commands/serve.rs +++ b/server/src/commands/serve.rs @@ -8,7 +8,7 @@ use failure::Fail; use crate::{ project::{Project, ProjectLoadFuzzyError}, - web::Server, + web::LiveServer, imfs::FsError, live_session::LiveSession, }; @@ -45,7 +45,7 @@ pub fn serve(options: &ServeOptions) -> Result<(), ServeError> { info!("Using project {:#?}", project); let live_session = Arc::new(LiveSession::new(Arc::clone(&project))?); - let server = Server::new(Arc::clone(&live_session)); + let server = LiveServer::new(live_session); let port = options.port .or(project.serve_port) @@ -53,7 +53,7 @@ pub fn serve(options: &ServeOptions) -> Result<(), ServeError> { println!("Rojo server listening on port {}", port); - server.listen(port); + server.start(port); Ok(()) } \ No newline at end of file diff --git a/server/src/web/api.rs b/server/src/web/api.rs index 9b1950c9..239bf994 100644 --- a/server/src/web/api.rs +++ b/server/src/web/api.rs @@ -7,6 +7,11 @@ use std::{ sync::{mpsc, Arc}, }; +use futures::{future, Future}; +use hyper::{ + service::Service, + Body, +}; use serde_derive::{Serialize, Deserialize}; use rouille::{ self, @@ -83,6 +88,17 @@ pub struct ApiServer { server_version: &'static str, } +impl Service for ApiServer { + type ReqBody = Body; + type ResBody = Body; + type Error = hyper::Error; + type Future = Box, Error = Self::Error> + Send>; + + fn call(&mut self, request: hyper::Request) -> Self::Future { + Box::new(future::ok(hyper::Response::new(Body::from("Hello, from API!")))) + } +} + impl ApiServer { pub fn new(live_session: Arc) -> ApiServer { ApiServer { diff --git a/server/src/web/interface.rs b/server/src/web/interface.rs index 24f8b6d0..1734a5d0 100644 --- a/server/src/web/interface.rs +++ b/server/src/web/interface.rs @@ -2,6 +2,11 @@ use std::sync::Arc; +use futures::{future, Future}; +use hyper::{ + service::Service, + Body, +}; use rouille::{ self, router, @@ -22,6 +27,17 @@ pub struct InterfaceServer { server_version: &'static str, } +impl Service for InterfaceServer { + type ReqBody = Body; + type ResBody = Body; + type Error = hyper::Error; + type Future = Box, Error = Self::Error> + Send>; + + fn call(&mut self, request: hyper::Request) -> Self::Future { + Box::new(future::ok(hyper::Response::new(Body::from("Hello, from interface!")))) + } +} + impl InterfaceServer { pub fn new(live_session: Arc) -> InterfaceServer { InterfaceServer { diff --git a/server/src/web/mod.rs b/server/src/web/mod.rs index 8e4e2978..3c3454d5 100644 --- a/server/src/web/mod.rs +++ b/server/src/web/mod.rs @@ -14,6 +14,7 @@ use hyper::{ Body, Request, Response, + Server, }; use crate::{ @@ -25,38 +26,53 @@ use self::{ interface::InterfaceServer, }; -pub struct Server { +pub struct RootService { api: api::ApiServer, interface: interface::InterfaceServer, } -struct Blah; - -impl Service for Blah { +impl Service for RootService { type ReqBody = Body; type ResBody = Body; type Error = hyper::Error; type Future = Box, Error = Self::Error> + Send>; fn call(&mut self, request: Request) -> Self::Future { - Box::new(future::ok(Response::new(Body::from("Hello, world!")))) + if request.uri().path().starts_with("/api") { + self.api.call(request) + } else { + self.interface.call(request) + } } } -impl Server { - pub fn new(live_session: Arc) -> Server { - Server { +impl RootService { + pub fn new(live_session: Arc) -> RootService { + RootService { api: ApiServer::new(Arc::clone(&live_session)), interface: InterfaceServer::new(Arc::clone(&live_session)), } } +} - pub fn listen(self, port: u16) { +pub struct LiveServer { + live_session: Arc, +} + +impl LiveServer { + pub fn new(live_session: Arc) -> LiveServer { + LiveServer { + live_session, + } + } + + pub fn start(self, port: u16) { let address = ([127, 0, 0, 1], port).into(); - let server = hyper::Server::bind(&address) + let server = Server::bind(&address) .serve(move || { - let service: FutureResult = future::ok(Blah); + let service: FutureResult = + future::ok(RootService::new(Arc::clone(&self.live_session))); service }) .map_err(|e| eprintln!("Server error: {}", e));