2016-03-12 18:45:19 +00:00
|
|
|
extern crate hyper;
|
|
|
|
|
|
|
|
mod method;
|
|
|
|
mod error;
|
2016-03-15 03:43:52 +00:00
|
|
|
mod response;
|
|
|
|
mod request;
|
2016-03-12 18:45:19 +00:00
|
|
|
|
|
|
|
pub use method::Method;
|
|
|
|
pub use error::Error;
|
2016-03-15 03:43:52 +00:00
|
|
|
pub use response::Response;
|
|
|
|
pub use request::Request;
|
2016-03-12 18:45:19 +00:00
|
|
|
|
|
|
|
use hyper::server::Handler as HypHandler;
|
|
|
|
use hyper::server::Request as HypRequest;
|
|
|
|
use hyper::server::Response as HypResponse;
|
|
|
|
use hyper::net::Fresh as HypFresh;
|
|
|
|
use hyper::Server;
|
|
|
|
|
|
|
|
pub type Handler = fn(Request) -> Response;
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Route<'a> {
|
|
|
|
pub method: Method,
|
|
|
|
pub path: &'a str,
|
|
|
|
pub handler: Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Rocket {
|
|
|
|
address: &'static str,
|
|
|
|
port: isize,
|
|
|
|
// mounts: HashMap<&'static str, Route<'a>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HypHandler for Rocket {
|
|
|
|
fn handle<'a, 'k>(&'a self, req: HypRequest<'a, 'k>,
|
|
|
|
res: HypResponse<'a, HypFresh>) {
|
|
|
|
println!("Request: {:?}", req.uri);
|
|
|
|
res.send(b"Hello World!").unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rocket {
|
|
|
|
pub fn new(address: &'static str, port: isize) -> Rocket {
|
|
|
|
Rocket {
|
|
|
|
address: address,
|
|
|
|
port: port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mount(&mut self, base: &str, routes: &[&Route]) -> &mut Self {
|
2016-03-15 03:43:52 +00:00
|
|
|
println!("🛰 Mounting '{}':", base);
|
2016-03-12 18:45:19 +00:00
|
|
|
for route in routes {
|
2016-03-15 03:43:52 +00:00
|
|
|
println!("\t* {} '{}'", route.method, route.path);
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mount_and_launch(mut self, base: &str, routes: &[&Route]) {
|
|
|
|
self.mount(base, routes);
|
|
|
|
self.launch();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn launch(self) {
|
|
|
|
let full_addr = format!("{}:{}", self.address, self.port);
|
2016-03-15 03:43:52 +00:00
|
|
|
println!("🚀 Rocket has launched from {}...", full_addr);
|
2016-03-12 18:45:19 +00:00
|
|
|
let _ = Server::http(full_addr.as_str()).unwrap().handle(self);
|
|
|
|
}
|
|
|
|
}
|