forked from rojo-rbx/rojo
Start to stub out sub-services
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
@@ -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<Future<Item = hyper::Response<Self::ReqBody>, Error = Self::Error> + Send>;
|
||||
|
||||
fn call(&mut self, request: hyper::Request<Self::ReqBody>) -> Self::Future {
|
||||
Box::new(future::ok(hyper::Response::new(Body::from("Hello, from API!"))))
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiServer {
|
||||
pub fn new(live_session: Arc<LiveSession>) -> ApiServer {
|
||||
ApiServer {
|
||||
|
||||
@@ -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<Future<Item = hyper::Response<Self::ReqBody>, Error = Self::Error> + Send>;
|
||||
|
||||
fn call(&mut self, request: hyper::Request<Self::ReqBody>) -> Self::Future {
|
||||
Box::new(future::ok(hyper::Response::new(Body::from("Hello, from interface!"))))
|
||||
}
|
||||
}
|
||||
|
||||
impl InterfaceServer {
|
||||
pub fn new(live_session: Arc<LiveSession>) -> InterfaceServer {
|
||||
InterfaceServer {
|
||||
|
||||
@@ -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<Future<Item = Response<Self::ReqBody>, Error = Self::Error> + Send>;
|
||||
|
||||
fn call(&mut self, request: Request<Self::ReqBody>) -> 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<LiveSession>) -> Server {
|
||||
Server {
|
||||
impl RootService {
|
||||
pub fn new(live_session: Arc<LiveSession>) -> 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<LiveSession>,
|
||||
}
|
||||
|
||||
impl LiveServer {
|
||||
pub fn new(live_session: Arc<LiveSession>) -> 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<Blah, hyper::Error> = future::ok(Blah);
|
||||
let service: FutureResult<RootService, hyper::Error> =
|
||||
future::ok(RootService::new(Arc::clone(&self.live_session)));
|
||||
service
|
||||
})
|
||||
.map_err(|e| eprintln!("Server error: {}", e));
|
||||
|
||||
Reference in New Issue
Block a user