Move rink-web to iron

This commit is contained in:
Tiffany Bennett 2016-10-03 23:30:26 -04:00
parent 2363eeb292
commit 3067a1bc20
2 changed files with 39 additions and 57 deletions

View file

@ -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"

View file

@ -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##"
<html>
@ -12,66 +20,39 @@ static TEMPLATE: &'static str = r##"
<title>Rink</title>
</head>
<body>
<form action="#" method="POST" target="_self">
<input name="value" type="text" />
<form action="/api" method="POST" target="_self">
<input name="query" type="text" />
<input type="submit" />
</form>
</body>
</html>
"##;
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<Response> {
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>(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::<header::ContentType>()
.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<Response> {
use params::{Params, Value};
let map = req.get_ref::<Params>().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::<u16>().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();
}