2016-03-22 23:27:12 +00:00
|
|
|
extern crate rocket;
|
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
use rocket::{Request, Response, Route, Data};
|
|
|
|
use rocket::request::FromParam;
|
2016-10-04 00:09:13 +00:00
|
|
|
use rocket::http::Method::*;
|
2016-03-22 23:27:12 +00:00
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
fn forward(_req: &Request, data: Data) -> Response<'static> {
|
|
|
|
Response::forward(data)
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:20:49 +00:00
|
|
|
fn hi(_req: &Request, _: Data) -> Response<'static> {
|
|
|
|
Response::new("Hello!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name<'a>(req: &'a Request, _: Data) -> Response<'a> {
|
2016-10-07 03:38:13 +00:00
|
|
|
Response::new(req.get_param(0).unwrap_or("unnamed"))
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:27:12 +00:00
|
|
|
#[allow(dead_code)]
|
2016-10-08 06:20:49 +00:00
|
|
|
fn echo_url<'a>(req: &'a Request, _: Data) -> Response<'a> {
|
|
|
|
let param = req.uri().as_str().split_at(6).1;
|
|
|
|
Response::new(String::from_param(param))
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-10-08 06:20:49 +00:00
|
|
|
let always_forward = Route::ranked(1, Get, "/", forward);
|
|
|
|
let hello = Route::ranked(2, Get, "/", hi);
|
|
|
|
|
2016-04-03 10:36:30 +00:00
|
|
|
let echo = Route::new(Get, "/", echo_url);
|
2016-10-08 06:20:49 +00:00
|
|
|
let name = Route::new(Get, "/<name>", name);
|
2016-03-22 23:27:12 +00:00
|
|
|
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
2016-10-08 06:20:49 +00:00
|
|
|
.mount("/", vec![always_forward, hello])
|
|
|
|
.mount("/hello", vec![name.clone()])
|
|
|
|
.mount("/hi", vec![name])
|
2016-10-04 02:48:33 +00:00
|
|
|
.mount("/echo:<str>", vec![echo])
|
|
|
|
.launch();
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|