Remove Rouille and port everything to Hyper

This commit is contained in:
Lucien Greathouse
2019-02-22 15:11:27 -08:00
parent 105d8aeb6b
commit c9a663ed39
7 changed files with 132 additions and 399 deletions

View File

@@ -5,15 +5,14 @@ use std::sync::Arc;
use futures::{future, Future};
use hyper::{
service::Service,
header,
Body,
};
use rouille::{
self,
router,
Method,
StatusCode,
Request,
Response,
};
use ritz::{html};
use ritz::html;
use crate::{
live_session::LiveSession,
@@ -22,47 +21,41 @@ use crate::{
static HOME_CSS: &str = include_str!("../../assets/index.css");
pub struct InterfaceServer {
pub struct InterfaceService {
live_session: Arc<LiveSession>,
server_version: &'static str,
}
impl Service for InterfaceServer {
impl Service for InterfaceService {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
type Future = Box<Future<Item = hyper::Response<Self::ReqBody>, Error = Self::Error> + Send>;
type Future = Box<dyn Future<Item = 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!"))))
fn call(&mut self, request: Request<Self::ReqBody>) -> Self::Future {
let response = match (request.method(), request.uri().path()) {
(&Method::GET, "/") => self.handle_home(),
(&Method::GET, "/visualize/rbx") => self.handle_visualize_rbx(),
(&Method::GET, "/visualize/imfs") => self.handle_visualize_imfs(),
_ => Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
.unwrap(),
};
Box::new(future::ok(response))
}
}
impl InterfaceServer {
pub fn new(live_session: Arc<LiveSession>) -> InterfaceServer {
InterfaceServer {
impl InterfaceService {
pub fn new(live_session: Arc<LiveSession>) -> InterfaceService {
InterfaceService {
live_session,
server_version: env!("CARGO_PKG_VERSION"),
}
}
#[allow(unreachable_code)]
pub fn handle_request(&self, request: &Request) -> Response {
router!(request,
(GET) (/) => {
self.handle_home()
},
(GET) (/visualize/rbx) => {
self.handle_visualize_rbx()
},
(GET) (/visualize/imfs) => {
self.handle_visualize_imfs()
},
_ => Response::empty_404()
)
}
fn handle_home(&self) -> Response {
fn handle_home(&self) -> Response<Body> {
let page = html! {
<html>
<head>
@@ -88,26 +81,41 @@ impl InterfaceServer {
</html>
};
Response::html(format!("<!DOCTYPE html>{}", page))
Response::builder()
.header(header::CONTENT_TYPE, "text/html")
.body(Body::from(format!("<!DOCTYPE html>{}", page)))
.unwrap()
}
fn handle_visualize_rbx(&self) -> Response {
fn handle_visualize_rbx(&self) -> Response<Body> {
let rbx_session = self.live_session.rbx_session.lock().unwrap();
let dot_source = format!("{}", VisualizeRbxSession(&rbx_session));
match graphviz_to_svg(&dot_source) {
Some(svg) => Response::svg(svg),
None => Response::text(dot_source),
Some(svg) => Response::builder()
.header(header::CONTENT_TYPE, "image/svg+xml")
.body(Body::from(svg))
.unwrap(),
None => Response::builder()
.header(header::CONTENT_TYPE, "text/plain")
.body(Body::from(dot_source))
.unwrap(),
}
}
fn handle_visualize_imfs(&self) -> Response {
fn handle_visualize_imfs(&self) -> Response<Body> {
let imfs = self.live_session.imfs.lock().unwrap();
let dot_source = format!("{}", VisualizeImfs(&imfs));
match graphviz_to_svg(&dot_source) {
Some(svg) => Response::svg(svg),
None => Response::text(dot_source),
Some(svg) => Response::builder()
.header(header::CONTENT_TYPE, "image/svg+xml")
.body(Body::from(svg))
.unwrap(),
None => Response::builder()
.header(header::CONTENT_TYPE, "text/plain")
.body(Body::from(dot_source))
.unwrap(),
}
}
}