2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2017-04-20 20:43:01 +00:00
|
|
|
|
2018-06-28 15:55:15 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2017-04-20 20:43:01 +00:00
|
|
|
|
|
|
|
use std::io::Cursor;
|
2017-05-15 04:46:01 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-04-20 20:43:01 +00:00
|
|
|
|
2017-05-17 08:39:36 +00:00
|
|
|
use rocket::{Request, State, Data, Response};
|
2017-05-15 04:46:01 +00:00
|
|
|
use rocket::fairing::{AdHoc, Fairing, Info, Kind};
|
|
|
|
use rocket::http::{Method, ContentType, Status};
|
2017-04-20 20:43:01 +00:00
|
|
|
|
2017-05-17 08:39:36 +00:00
|
|
|
struct Token(i64);
|
|
|
|
|
2017-04-20 20:43:01 +00:00
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
|
2017-05-15 04:46:01 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Counter {
|
|
|
|
get: AtomicUsize,
|
|
|
|
post: AtomicUsize,
|
|
|
|
}
|
|
|
|
|
2020-01-31 04:47:57 +00:00
|
|
|
#[rocket::async_trait]
|
2017-05-15 04:46:01 +00:00
|
|
|
impl Fairing for Counter {
|
|
|
|
fn info(&self) -> Info {
|
|
|
|
Info {
|
|
|
|
name: "GET/POST Counter",
|
|
|
|
kind: Kind::Request | Kind::Response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 04:47:57 +00:00
|
|
|
async fn on_request<'a>(&'a self, request: &'a mut Request<'_>, _: &'a Data) {
|
|
|
|
if request.method() == Method::Get {
|
|
|
|
self.get.fetch_add(1, Ordering::Relaxed);
|
|
|
|
} else if request.method() == Method::Post {
|
|
|
|
self.post.fetch_add(1, Ordering::Relaxed);
|
|
|
|
}
|
2017-05-15 04:46:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 04:47:57 +00:00
|
|
|
async fn on_response<'a>(&'a self, req: &'a Request<'_>, res: &'a mut Response<'_>) {
|
|
|
|
if res.status() != Status::NotFound {
|
|
|
|
return
|
|
|
|
}
|
2017-05-15 04:46:01 +00:00
|
|
|
|
2020-01-31 04:47:57 +00:00
|
|
|
if req.method() == Method::Get && req.uri().path() == "/counts" {
|
|
|
|
let get_count = self.get.load(Ordering::Relaxed);
|
|
|
|
let post_count = self.post.load(Ordering::Relaxed);
|
2017-05-15 04:46:01 +00:00
|
|
|
|
2020-01-31 04:47:57 +00:00
|
|
|
let body = format!("Get: {}\nPost: {}", get_count, post_count);
|
|
|
|
res.set_status(Status::Ok);
|
|
|
|
res.set_header(ContentType::Plain);
|
|
|
|
res.set_sized_body(Cursor::new(body)).await;
|
|
|
|
}
|
2017-05-15 04:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:43:01 +00:00
|
|
|
#[put("/")]
|
|
|
|
fn hello() -> &'static str {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:39:36 +00:00
|
|
|
#[get("/token")]
|
2019-06-13 02:41:29 +00:00
|
|
|
fn token(token: State<'_, Token>) -> String {
|
2017-05-17 08:39:36 +00:00
|
|
|
format!("{}", token.0)
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:43:01 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite()
|
2017-05-17 08:39:36 +00:00
|
|
|
.mount("/", routes![hello, token])
|
2017-05-15 04:46:01 +00:00
|
|
|
.attach(Counter::default())
|
2020-06-14 15:57:53 +00:00
|
|
|
.attach(AdHoc::on_attach("Token State", |mut rocket| async {
|
2017-05-17 08:39:36 +00:00
|
|
|
println!("Adding token managed state...");
|
2020-06-14 15:57:54 +00:00
|
|
|
let token_val = rocket.config().await.get_int("token").unwrap_or(-1);
|
2017-05-17 08:39:36 +00:00
|
|
|
Ok(rocket.manage(Token(token_val)))
|
|
|
|
}))
|
2018-08-14 16:14:06 +00:00
|
|
|
.attach(AdHoc::on_launch("Launch Message", |_| {
|
2017-05-17 08:39:36 +00:00
|
|
|
println!("Rocket is about to launch!");
|
2017-05-15 04:46:01 +00:00
|
|
|
}))
|
2018-08-14 16:14:06 +00:00
|
|
|
.attach(AdHoc::on_request("PUT Rewriter", |req, _| {
|
2019-12-11 00:34:32 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
println!(" => Incoming request: {}", req);
|
|
|
|
if req.uri().path() == "/" {
|
|
|
|
println!(" => Changing method to `PUT`.");
|
|
|
|
req.set_method(Method::Put);
|
|
|
|
}
|
|
|
|
})
|
2017-05-15 04:46:01 +00:00
|
|
|
}))
|
2018-08-14 16:14:06 +00:00
|
|
|
.attach(AdHoc::on_response("Response Rewriter", |req, res| {
|
2019-08-04 22:36:55 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
if req.uri().path() == "/" {
|
|
|
|
println!(" => Rewriting response body.");
|
2020-01-07 06:28:57 +00:00
|
|
|
res.set_sized_body(Cursor::new("Hello, fairings!")).await;
|
2019-08-04 22:36:55 +00:00
|
|
|
}
|
|
|
|
})
|
2017-05-15 04:46:01 +00:00
|
|
|
}))
|
2017-04-20 20:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-08-25 02:19:11 +00:00
|
|
|
let _ = rocket().launch();
|
2017-04-20 20:43:01 +00:00
|
|
|
}
|