Rocket/examples/state/src/main.rs

37 lines
833 B
Rust
Raw Normal View History

#![feature(plugin, decl_macro)]
2017-01-21 03:31:46 +00:00
#![plugin(rocket_codegen)]
extern crate rocket;
#[cfg(test)] mod tests;
use std::sync::atomic::{AtomicUsize, Ordering};
use rocket::State;
use rocket::response::content;
struct HitCount(AtomicUsize);
#[get("/")]
fn index(hit_count: State<HitCount>) -> content::Html<String> {
2017-01-21 03:31:46 +00:00
hit_count.0.fetch_add(1, Ordering::Relaxed);
let msg = "Your visit has been recorded!";
let count = format!("Visits: {}", count(hit_count));
content::Html(format!("{}<br /><br />{}", msg, count))
2017-01-21 03:31:46 +00:00
}
#[get("/count")]
fn count(hit_count: State<HitCount>) -> String {
hit_count.0.load(Ordering::Relaxed).to_string()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, count])
.manage(HitCount(AtomicUsize::new(0)))
}
fn main() {
rocket().launch();
}