From 3067a1bc20ee6ac17db730cd337e3d5f3e9ab65a Mon Sep 17 00:00:00 2001 From: Tiffany Bennett Date: Mon, 3 Oct 2016 23:30:26 -0400 Subject: [PATCH] Move rink-web to iron --- web/Cargo.toml | 5 +-- web/src/main.rs | 91 +++++++++++++++++++------------------------------ 2 files changed, 39 insertions(+), 57 deletions(-) diff --git a/web/Cargo.toml b/web/Cargo.toml index 1a6119a..c3d5937 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -13,5 +13,6 @@ path = "../" features = ["hyper", "sandbox"] [dependencies] -hyper = "0.9.10" -url = "1.2.0" +iron = "0.4.0" +router = "0.4.0" +params = "0.4.0" diff --git a/web/src/main.rs b/web/src/main.rs index 50e7de3..f8d29ff 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -2,9 +2,17 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -extern crate hyper; -extern crate url; extern crate rink; +extern crate iron; +extern crate router; +extern crate params; + +use iron::prelude::*; +use iron::status; +use router::Router; +use iron::headers; +use iron::modifiers::Header; +use iron::mime::Mime; static TEMPLATE: &'static str = r##" @@ -12,66 +20,39 @@ static TEMPLATE: &'static str = r##" Rink -
- + +
"##; -fn main() { - use hyper; - use hyper::status::StatusCode; - use hyper::server::{Server, Request, Response}; - use hyper::header; - use hyper::mime::{Mime, TopLevel, SubLevel}; - use std::env::args; - use std::io::{Read, Write}; - use url::form_urlencoded; +fn root(_req: &mut Request) -> IronResult { + let mime: Mime = "text/html".parse().unwrap(); + Ok(Response::with((status::Ok, TEMPLATE, mime))) +} - let req = move |mut req: Request, mut res: Response| { - res.headers_mut().set::(header::AccessControlAllowOrigin::Any); - match req.method { - hyper::Get => { - write!(res.start().unwrap(), "{}", TEMPLATE).unwrap(); - }, - hyper::Post => { - let mut buf = String::new(); - match req.read_to_string(&mut buf) { - Ok(_) => (), - Err(e) => { - *res.status_mut() = StatusCode::BadRequest; - write!(res.start().unwrap(), "{}", e).unwrap(); - return - } - }; - let is_form = req.headers.get::() - .map(|x| match x.0 { - Mime(TopLevel::Application, SubLevel::WwwFormUrlEncoded, _) => true, - _ => false, - }) - .unwrap_or(false); - let input = if is_form { - let mut form = form_urlencoded::parse(buf.as_bytes()); - let value = form.find(|&(ref k, _)| k == "value"); - if let Some((_, value)) = value { - value.into_owned() - } else { - *res.status_mut() = StatusCode::BadRequest; - write!(&mut res.start().unwrap(), "Bad form").unwrap(); - return - } - } else { - buf - }; - let reply = rink::one_line_sandbox(&*input); - write!(&mut res.start().unwrap(), "{}", reply).unwrap(); - }, - _ => *res.status_mut() = StatusCode::MethodNotAllowed - } +fn api(req: &mut Request) -> IronResult { + use params::{Params, Value}; + + let map = req.get_ref::().unwrap(); + + let acao = Header(headers::AccessControlAllowOrigin::Any); + + let query = match map.find(&["query"]) { + Some(&Value::String(ref query)) => query, + _ => return Ok(Response::with((acao, status::BadRequest))), }; - let port = args().nth(1).map(|x| x.parse::().expect("Invalid port number")).unwrap_or(8000); - Server::http(&*format!("127.0.0.1:{}", port)).unwrap().handle(req).unwrap(); + let reply = rink::one_line_sandbox(query); + + Ok(Response::with((acao, status::Ok, reply))) +} + +fn main() { + let mut router = Router::new(); + router.get("/", root, "root"); + router.get("/api", api, "api"); + Iron::new(router).http("localhost:8000").unwrap(); }