2016-03-12 18:45:19 +00:00
|
|
|
extern crate hyper;
|
|
|
|
|
2016-03-17 08:57:04 +00:00
|
|
|
pub mod method;
|
|
|
|
pub mod error;
|
|
|
|
pub mod response;
|
|
|
|
pub mod request;
|
|
|
|
pub mod param;
|
2016-03-17 10:29:55 +00:00
|
|
|
pub mod router;
|
2016-03-12 18:45:19 +00:00
|
|
|
|
|
|
|
pub use method::Method;
|
|
|
|
pub use error::Error;
|
2016-03-17 08:57:04 +00:00
|
|
|
pub use response::{Response, HypResponse, HypFresh, Responder};
|
2016-03-15 03:43:52 +00:00
|
|
|
pub use request::Request;
|
2016-03-17 08:57:04 +00:00
|
|
|
pub use param::FromParam;
|
2016-03-17 10:29:55 +00:00
|
|
|
pub use router::Router;
|
2016-03-12 18:45:19 +00:00
|
|
|
|
|
|
|
use hyper::server::Handler as HypHandler;
|
|
|
|
use hyper::server::Request as HypRequest;
|
|
|
|
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)]
|
2016-03-17 08:57:04 +00:00
|
|
|
pub struct Route<'a> {
|
2016-03-12 18:45:19 +00:00
|
|
|
pub method: Method,
|
2016-03-17 08:57:04 +00:00
|
|
|
pub path: &'static str,
|
|
|
|
pub handler: Handler<'a>
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Rocket {
|
|
|
|
address: &'static str,
|
|
|
|
port: isize,
|
2016-03-17 08:57:04 +00:00
|
|
|
handler: Option<Route<'static>>, // just for testing
|
2016-03-17 10:29:55 +00:00
|
|
|
router: Router
|
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>,
|
2016-03-18 03:37:34 +00:00
|
|
|
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() {
|
2016-03-17 08:57:04 +00:00
|
|
|
let handler = self.handler.as_ref();
|
|
|
|
let mut response = (handler.unwrap().handler)(Request::empty());
|
|
|
|
response.body.respond(res);
|
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
|
|
|
}
|
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,
|
2016-03-17 10:29:55 +00:00
|
|
|
handler: None,
|
|
|
|
router: Router::new()
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 08:57:04 +00:00
|
|
|
pub fn mount(&mut self, base: &str, routes: &[&Route<'static>]) -> &mut Self {
|
|
|
|
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-17 10:29:55 +00:00
|
|
|
|
2016-03-15 03:43:52 +00:00
|
|
|
println!("\t* {} '{}'", route.method, route.path);
|
2016-03-17 10:29:55 +00:00
|
|
|
self.router.add_route(route.method.clone(), base, route.path);
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-03-17 08:57:04 +00:00
|
|
|
pub fn mount_and_launch(mut self, base: &str, routes: &[&Route<'static>]) {
|
2016-03-12 18:45:19 +00:00
|
|
|
self.mount(base, routes);
|
|
|
|
self.launch();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn launch(self) {
|
2016-03-17 10:29:55 +00:00
|
|
|
if self.router.has_collisions() {
|
|
|
|
println!("Warning: route collisions detected!");
|
|
|
|
}
|
|
|
|
|
2016-03-12 18:45:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|