2016-03-22 23:27:12 +00:00
|
|
|
extern crate rocket;
|
|
|
|
|
2017-01-09 02:02:12 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2016-10-09 03:53:04 +00:00
|
|
|
use std::io;
|
|
|
|
use std::fs::File;
|
|
|
|
|
2016-12-15 08:47:31 +00:00
|
|
|
use rocket::{Request, Route, Data, Catcher, Error};
|
|
|
|
use rocket::http::Status;
|
2016-10-08 06:20:49 +00:00
|
|
|
use rocket::request::FromParam;
|
2016-12-15 08:47:31 +00:00
|
|
|
use rocket::response::{self, Responder};
|
2017-01-09 02:02:12 +00:00
|
|
|
use rocket::response::status::Custom;
|
2016-12-15 08:47:31 +00:00
|
|
|
use rocket::handler::Outcome;
|
2016-10-04 00:09:13 +00:00
|
|
|
use rocket::http::Method::*;
|
2016-03-22 23:27:12 +00:00
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn forward(_req: &Request, data: Data) -> Outcome<'static> {
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::forward(data)
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn hi(_req: &Request, _: Data) -> Outcome<'static> {
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::of("Hello!")
|
2016-10-08 06:20:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn name<'a>(req: &'a Request, _: Data) -> Outcome<'a> {
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::of(req.get_param(0).unwrap_or("unnamed"))
|
2016-10-07 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 08:47:31 +00:00
|
|
|
fn echo_url(req: &Request, _: Data) -> Outcome<'static> {
|
2017-01-09 02:02:12 +00:00
|
|
|
let param = req.uri()
|
|
|
|
.as_str()
|
|
|
|
.split_at(6)
|
|
|
|
.1;
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::of(String::from_param(param).unwrap())
|
2016-10-09 03:53:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn upload<'r>(req: &'r Request, data: Data) -> Outcome<'r> {
|
2017-02-01 11:12:24 +00:00
|
|
|
if !req.content_type().map_or(false, |ct| ct.is_plain()) {
|
2016-12-15 08:47:31 +00:00
|
|
|
println!(" => Content-Type of upload must be text/plain. Ignoring.");
|
|
|
|
return Outcome::failure(Status::BadRequest);
|
2016-10-09 03:53:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 11:29:02 +00:00
|
|
|
let file = File::create("/tmp/upload.txt");
|
2016-10-09 03:53:04 +00:00
|
|
|
if let Ok(mut file) = file {
|
2016-10-09 11:29:02 +00:00
|
|
|
if let Ok(n) = io::copy(&mut data.open(), &mut file) {
|
2016-12-15 08:47:31 +00:00
|
|
|
return Outcome::of(format!("OK: {} bytes uploaded.", n));
|
2016-10-09 03:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!(" => Failed copying.");
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::failure(Status::InternalServerError)
|
2016-10-09 03:53:04 +00:00
|
|
|
} else {
|
|
|
|
println!(" => Couldn't open file: {:?}", file.unwrap_err());
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::failure(Status::InternalServerError)
|
2016-10-09 03:53:04 +00:00
|
|
|
}
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn get_upload(_: &Request, _: Data) -> Outcome<'static> {
|
2016-12-15 08:47:31 +00:00
|
|
|
Outcome::of(File::open("/tmp/upload.txt").ok())
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:07:23 +00:00
|
|
|
fn not_found_handler<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
2017-01-09 02:02:12 +00:00
|
|
|
Custom(Status::NotFound, format!("Couldn't find: {}", req.uri())).respond()
|
2016-11-05 18:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 02:02:12 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
2016-10-08 06:20:49 +00:00
|
|
|
let always_forward = Route::ranked(1, Get, "/", forward);
|
|
|
|
let hello = Route::ranked(2, Get, "/", hi);
|
|
|
|
|
2016-12-15 08:47:31 +00:00
|
|
|
let echo = Route::new(Get, "/echo:<str>", echo_url);
|
2016-10-08 06:20:49 +00:00
|
|
|
let name = Route::new(Get, "/<name>", name);
|
2016-12-15 08:47:31 +00:00
|
|
|
let post_upload = Route::new(Post, "/", upload);
|
|
|
|
let get_upload = Route::new(Get, "/", get_upload);
|
2016-03-22 23:27:12 +00:00
|
|
|
|
2016-11-05 18:31:50 +00:00
|
|
|
let not_found_catcher = Catcher::new(404, not_found_handler);
|
|
|
|
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
2016-12-15 08:47:31 +00:00
|
|
|
.mount("/", vec![always_forward, hello, echo])
|
|
|
|
.mount("/upload", vec![get_upload, post_upload])
|
2016-10-08 06:20:49 +00:00
|
|
|
.mount("/hello", vec![name.clone()])
|
|
|
|
.mount("/hi", vec![name])
|
2016-11-05 18:31:50 +00:00
|
|
|
.catch(vec![not_found_catcher])
|
2017-01-09 02:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket().launch();
|
2016-03-22 23:27:12 +00:00
|
|
|
}
|