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
|
|
|
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2016-03-12 18:45:19 +00:00
|
|
|
pub use method::Method;
|
|
|
|
pub use error::Error;
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
pub use response::{Body, Response};
|
2016-03-15 03:43:52 +00:00
|
|
|
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;
|
|
|
|
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
pub type Handler<'a> = fn(Request) -> Response<'a>;
|
2016-03-12 18:45:19 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Route<'a, 'b> {
|
2016-03-12 18:45:19 +00:00
|
|
|
pub method: Method,
|
|
|
|
pub path: &'a str,
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
pub handler: Handler<'b>
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Rocket {
|
|
|
|
address: &'static str,
|
|
|
|
port: isize,
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
handler: Option<Route<'static, 'static>> // just for testing
|
2016-03-12 18:45:19 +00:00
|
|
|
// mounts: HashMap<&'static str, Route<'a>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HypHandler for Rocket {
|
|
|
|
fn handle<'a, 'k>(&'a self, req: HypRequest<'a, 'k>,
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
mut res: HypResponse<'a, HypFresh>) {
|
2016-03-12 18:45:19 +00:00
|
|
|
println!("Request: {:?}", req.uri);
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
if self.handler.is_some() {
|
|
|
|
let response = (self.handler.as_ref().unwrap().handler)(Request::empty());
|
|
|
|
*(res.headers_mut()) = response.headers;
|
|
|
|
*(res.status_mut()) = response.status;
|
|
|
|
match response.body {
|
|
|
|
Body::Str(string) => {
|
|
|
|
let mut stream = res.start().unwrap();
|
|
|
|
stream.write_all(string.as_bytes()).unwrap();
|
|
|
|
stream.end();
|
|
|
|
}
|
|
|
|
_ => println!("UNIMPLEMENTED")
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rocket {
|
|
|
|
pub fn new(address: &'static str, port: isize) -> Rocket {
|
|
|
|
Rocket {
|
|
|
|
address: address,
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
port: port,
|
|
|
|
handler: None
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
pub fn mount(&mut self, base: &str, routes: &[&Route<'static, 'static>]) -> &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 {
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
if self.handler.is_none() {
|
|
|
|
println!("\t* INSTALLED: {} '{}'", route.method, route.path);
|
|
|
|
self.handler = Some((*route).clone());
|
|
|
|
}
|
2016-03-15 03:43:52 +00:00
|
|
|
println!("\t* {} '{}'", route.method, route.path);
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
Something works! A simple hacked-up handler, that is.
At the moment, I simply install the first route I see into the Rocket struct
directly. This is quite terrible. What's worse is that I assume that the Route's
path and handler are static! The handler, actually, does have to be static, but
its response may have whatever (valid) lifetime, though I'm not sure anything
but `static makes sense. I'll think about it.
In any case, the weird `static` restrictions need to be removed, and I need to
think about which lifetimes are safe here. IE: Must all routes be static? Can I
use a closure as a route? (that'd be neat). If so, how do we make that work?
In any case, it's nice to see SOMETHING work. Yay!
2016-03-15 07:41:22 +00:00
|
|
|
pub fn mount_and_launch(mut self, base: &str, routes: &[&Route<'static, 'static>]) {
|
2016-03-12 18:45:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|