mirror of https://github.com/rwf2/Rocket.git
Migrate all examples to Rust 2018.
This commit is contained in:
parent
2315171971
commit
d9f989a496
|
@ -2,6 +2,7 @@
|
||||||
name = "config"
|
name = "config"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
extern crate rocket;
|
|
||||||
|
|
||||||
// This example's illustration is the Rocket.toml file.
|
// This example's illustration is the Rocket.toml file.
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite().launch();
|
rocket::ignite().launch();
|
||||||
|
|
|
@ -7,8 +7,8 @@ use rocket::local::Client;
|
||||||
struct LocalConfig(Config);
|
struct LocalConfig(Config);
|
||||||
|
|
||||||
#[get("/check_config")]
|
#[get("/check_config")]
|
||||||
fn check_config(config: State<LocalConfig>) -> Option<()> {
|
fn check_config(config: State<'_, LocalConfig>) -> Option<()> {
|
||||||
let environment = match ::std::env::var("ROCKET_ENV") {
|
let environment = match std::env::var("ROCKET_ENV") {
|
||||||
Ok(name) => name,
|
Ok(name) => name,
|
||||||
Err(_) => return None
|
Err(_) => return None
|
||||||
};
|
};
|
||||||
|
@ -52,7 +52,7 @@ fn check_config(config: State<LocalConfig>) -> Option<()> {
|
||||||
pub fn test_config(environment: Environment) {
|
pub fn test_config(environment: Environment) {
|
||||||
// Manually set the config environment variable. Rocket will initialize the
|
// Manually set the config environment variable. Rocket will initialize the
|
||||||
// environment in `ignite()`. We'll read this back in the handler to config.
|
// environment in `ignite()`. We'll read this back in the handler to config.
|
||||||
::std::env::set_var("ROCKET_ENV", environment.to_string());
|
std::env::set_var("ROCKET_ENV", environment.to_string());
|
||||||
|
|
||||||
let rocket = rocket::ignite()
|
let rocket = rocket::ignite()
|
||||||
.attach(AdHoc::on_attach("Local Config", |rocket| {
|
.attach(AdHoc::on_attach("Local Config", |rocket| {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "content_types"
|
name = "content_types"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate serde_json;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -42,7 +41,7 @@ fn post_hello(age: u8, name_data: Data) -> io::Result<content::Json<String>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(request: &Request) -> content::Html<String> {
|
fn not_found(request: &Request<'_>) -> content::Html<String> {
|
||||||
let html = match request.format() {
|
let html = match request.format() {
|
||||||
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
|
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
|
||||||
format!("<p>'{}' requests are not supported.</p>", mt)
|
format!("<p>'{}' requests are not supported.</p>", mt)
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use super::serde_json;
|
|
||||||
use super::Person;
|
use super::Person;
|
||||||
use rocket::http::{Accept, ContentType, Header, MediaType, Method, Status};
|
use rocket::http::{Accept, ContentType, Header, MediaType, Method, Status};
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "cookies"
|
name = "cookies"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -19,13 +18,13 @@ struct Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/submit", data = "<message>")]
|
#[post("/submit", data = "<message>")]
|
||||||
fn submit(mut cookies: Cookies, message: Form<Message>) -> Redirect {
|
fn submit(mut cookies: Cookies<'_>, message: Form<Message>) -> Redirect {
|
||||||
cookies.add(Cookie::new("message", message.into_inner().message));
|
cookies.add(Cookie::new("message", message.into_inner().message));
|
||||||
Redirect::to("/")
|
Redirect::to("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(cookies: Cookies) -> Template {
|
fn index(cookies: Cookies<'_>) -> Template {
|
||||||
let cookie = cookies.get("message");
|
let cookie = cookies.get("message");
|
||||||
let mut context = HashMap::new();
|
let mut context = HashMap::new();
|
||||||
if let Some(ref cookie) = cookie {
|
if let Some(ref cookie) = cookie {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "errors"
|
name = "errors"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -12,7 +12,7 @@ fn hello(name: String, age: i8) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(req: &rocket::Request) -> content::Html<String> {
|
fn not_found(req: &rocket::Request<'_>) -> content::Html<String> {
|
||||||
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
|
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
|
||||||
<p>Try visiting /hello/<name>/<age> instead.</p>",
|
<p>Try visiting /hello/<name>/<age> instead.</p>",
|
||||||
req.uri()))
|
req.uri()))
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "fairings"
|
name = "fairings"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -27,7 +27,7 @@ impl Fairing for Counter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_request(&self, request: &mut Request, _: &Data) {
|
fn on_request(&self, request: &mut Request<'_>, _: &Data) {
|
||||||
if request.method() == Method::Get {
|
if request.method() == Method::Get {
|
||||||
self.get.fetch_add(1, Ordering::Relaxed);
|
self.get.fetch_add(1, Ordering::Relaxed);
|
||||||
} else if request.method() == Method::Post {
|
} else if request.method() == Method::Post {
|
||||||
|
@ -35,7 +35,7 @@ impl Fairing for Counter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_response(&self, request: &Request, response: &mut Response) {
|
fn on_response(&self, request: &Request<'_>, response: &mut Response<'_>) {
|
||||||
if response.status() != Status::NotFound {
|
if response.status() != Status::NotFound {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ fn hello() -> &'static str {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/token")]
|
#[get("/token")]
|
||||||
fn token(token: State<Token>) -> String {
|
fn token(token: State<'_, Token>) -> String {
|
||||||
format!("{}", token.0)
|
format!("{}", token.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "form_kitchen_sink"
|
name = "form_kitchen_sink"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -28,7 +28,7 @@ struct FormInput<'r> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<sink>")]
|
#[post("/", data = "<sink>")]
|
||||||
fn sink(sink: Result<Form<FormInput>, FormError>) -> String {
|
fn sink(sink: Result<Form<FormInput<'_>>, FormError<'_>>) -> String {
|
||||||
match sink {
|
match sink {
|
||||||
Ok(form) => format!("{:?}", &*form),
|
Ok(form) => format!("{:?}", &*form),
|
||||||
Err(FormDataError::Io(_)) => format!("Form input was invalid UTF-8."),
|
Err(FormDataError::Io(_)) => format!("Form input was invalid UTF-8."),
|
||||||
|
|
|
@ -5,7 +5,7 @@ use rocket::local::Client;
|
||||||
use rocket::http::ContentType;
|
use rocket::http::ContentType;
|
||||||
|
|
||||||
impl fmt::Display for FormOption {
|
impl fmt::Display for FormOption {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
FormOption::A => write!(f, "a"),
|
FormOption::A => write!(f, "a"),
|
||||||
FormOption::B => write!(f, "b"),
|
FormOption::B => write!(f, "b"),
|
||||||
|
@ -23,14 +23,14 @@ fn assert_form_eq(client: &Client, form_str: &str, expected: String) {
|
||||||
assert_eq!(res.body_string(), Some(expected));
|
assert_eq!(res.body_string(), Some(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assert_valid_form(client: &Client, input: &FormInput) {
|
fn assert_valid_form(client: &Client, input: &FormInput<'_>) {
|
||||||
let f = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
let f = format!("checkbox={}&number={}&type={}&password={}&textarea={}&select={}",
|
||||||
input.checkbox, input.number, input.radio, input.password,
|
input.checkbox, input.number, input.radio, input.password,
|
||||||
input.text_area, input.select);
|
input.text_area, input.select);
|
||||||
assert_form_eq(client, &f, format!("{:?}", input));
|
assert_form_eq(client, &f, format!("{:?}", input));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assert_valid_raw_form(client: &Client, form_str: &str, input: &FormInput) {
|
fn assert_valid_raw_form(client: &Client, form_str: &str, input: &FormInput<'_>) {
|
||||||
assert_form_eq(client, form_str, format!("{:?}", input));
|
assert_form_eq(client, form_str, format!("{:?}", input));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ fn check_structurally_invalid_forms() {
|
||||||
fn check_bad_utf8() {
|
fn check_bad_utf8() {
|
||||||
let client = Client::new(rocket()).unwrap();
|
let client = Client::new(rocket()).unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
let bad_str = ::std::str::from_utf8_unchecked(b"a=\xff");
|
let bad_str = std::str::from_utf8_unchecked(b"a=\xff");
|
||||||
assert_form_eq(&client, bad_str, "Form input was invalid UTF-8.".into());
|
assert_form_eq(&client, bad_str, "Form input was invalid UTF-8.".into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "form_validation"
|
name = "form_validation"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl<'v> FromFormValue<'v> for AdultAge {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/login", data = "<user>")]
|
#[post("/login", data = "<user>")]
|
||||||
fn login(user: Form<UserLogin>) -> Result<Redirect, String> {
|
fn login(user: Form<UserLogin<'_>>) -> Result<Redirect, String> {
|
||||||
if let Err(e) = user.age {
|
if let Err(e) = user.age {
|
||||||
return Err(format!("Age is invalid: {}", e));
|
return Err(format!("Age is invalid: {}", e));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "handlebars_templates"
|
name = "handlebars_templates"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -10,8 +9,6 @@ use rocket::Request;
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket_contrib::templates::{Template, handlebars};
|
use rocket_contrib::templates::{Template, handlebars};
|
||||||
|
|
||||||
use handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct TemplateContext {
|
struct TemplateContext {
|
||||||
title: &'static str,
|
title: &'static str,
|
||||||
|
@ -47,18 +44,20 @@ fn about() -> Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(req: &Request) -> Template {
|
fn not_found(req: &Request<'_>) -> Template {
|
||||||
let mut map = std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
map.insert("path", req.uri().path());
|
map.insert("path", req.uri().path());
|
||||||
Template::render("error/404", &map)
|
Template::render("error/404", &map)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use self::handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};
|
||||||
|
|
||||||
fn wow_helper(
|
fn wow_helper(
|
||||||
h: &Helper,
|
h: &Helper<'_, '_>,
|
||||||
_: &Handlebars,
|
_: &Handlebars,
|
||||||
_: &Context,
|
_: &Context,
|
||||||
_: &mut RenderContext,
|
_: &mut RenderContext<'_>,
|
||||||
out: &mut Output
|
out: &mut dyn Output
|
||||||
) -> HelperResult {
|
) -> HelperResult {
|
||||||
if let Some(param) = h.param(0) {
|
if let Some(param) = h.param(0) {
|
||||||
out.write("<b><i>")?;
|
out.write("<b><i>")?;
|
||||||
|
|
|
@ -16,7 +16,7 @@ macro_rules! dispatch {
|
||||||
fn test_root() {
|
fn test_root() {
|
||||||
// Check that the redirect works.
|
// Check that the redirect works.
|
||||||
for method in &[Get, Head] {
|
for method in &[Get, Head] {
|
||||||
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse| {
|
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse<'_>| {
|
||||||
assert_eq!(response.status(), Status::SeeOther);
|
assert_eq!(response.status(), Status::SeeOther);
|
||||||
assert!(response.body().is_none());
|
assert!(response.body().is_none());
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ fn test_root() {
|
||||||
|
|
||||||
// Check that other request methods are not accepted (and instead caught).
|
// Check that other request methods are not accepted (and instead caught).
|
||||||
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
|
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
|
||||||
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse| {
|
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let mut map = ::std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
map.insert("path", "/");
|
map.insert("path", "/");
|
||||||
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ fn test_root() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_name() {
|
fn test_name() {
|
||||||
// Check that the /hello/<name> route works.
|
// Check that the /hello/<name> route works.
|
||||||
dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse| {
|
dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let context = TemplateContext {
|
let context = TemplateContext {
|
||||||
title: "Hello",
|
title: "Hello",
|
||||||
name: Some("Jack Daniels".into()),
|
name: Some("Jack Daniels".into()),
|
||||||
|
@ -58,8 +58,8 @@ fn test_name() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_404() {
|
fn test_404() {
|
||||||
// Check that the error catcher works.
|
// Check that the error catcher works.
|
||||||
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse| {
|
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let mut map = ::std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
map.insert("path", "/hello/");
|
map.insert("path", "/hello/");
|
||||||
|
|
||||||
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
name = "hello_2018"
|
name = "hello_2018"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
publish = false
|
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = { path = "../../core/lib" }
|
rocket = { path = "../../core/lib" }
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "hello_person"
|
name = "hello_person"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "hello_world"
|
name = "hello_world"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "json"
|
name = "json"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -26,7 +26,7 @@ struct Message {
|
||||||
|
|
||||||
// TODO: This example can be improved by using `route` with multiple HTTP verbs.
|
// TODO: This example can be improved by using `route` with multiple HTTP verbs.
|
||||||
#[post("/<id>", format = "json", data = "<message>")]
|
#[post("/<id>", format = "json", data = "<message>")]
|
||||||
fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
|
fn new(id: ID, message: Json<Message>, map: State<'_, MessageMap>) -> JsonValue {
|
||||||
let mut hashmap = map.lock().expect("map lock.");
|
let mut hashmap = map.lock().expect("map lock.");
|
||||||
if hashmap.contains_key(&id) {
|
if hashmap.contains_key(&id) {
|
||||||
json!({
|
json!({
|
||||||
|
@ -40,7 +40,7 @@ fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put("/<id>", format = "json", data = "<message>")]
|
#[put("/<id>", format = "json", data = "<message>")]
|
||||||
fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<JsonValue> {
|
fn update(id: ID, message: Json<Message>, map: State<'_, MessageMap>) -> Option<JsonValue> {
|
||||||
let mut hashmap = map.lock().unwrap();
|
let mut hashmap = map.lock().unwrap();
|
||||||
if hashmap.contains_key(&id) {
|
if hashmap.contains_key(&id) {
|
||||||
hashmap.insert(id, message.0.contents);
|
hashmap.insert(id, message.0.contents);
|
||||||
|
@ -51,7 +51,7 @@ fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<Json
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<id>", format = "json")]
|
#[get("/<id>", format = "json")]
|
||||||
fn get(id: ID, map: State<MessageMap>) -> Option<Json<Message>> {
|
fn get(id: ID, map: State<'_, MessageMap>) -> Option<Json<Message>> {
|
||||||
let hashmap = map.lock().unwrap();
|
let hashmap = map.lock().unwrap();
|
||||||
hashmap.get(&id).map(|contents| {
|
hashmap.get(&id).map(|contents| {
|
||||||
Json(Message {
|
Json(Message {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use rocket;
|
use crate::rocket;
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::{Status, ContentType};
|
use rocket::http::{Status, ContentType};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "managed_queue"
|
name = "managed_queue"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../.."
|
workspace = "../.."
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
extern crate crossbeam;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -11,12 +10,12 @@ use crossbeam::queue::SegQueue;
|
||||||
struct LogChannel(SegQueue<String>);
|
struct LogChannel(SegQueue<String>);
|
||||||
|
|
||||||
#[put("/push?<event>")]
|
#[put("/push?<event>")]
|
||||||
fn push(event: String, queue: State<LogChannel>) {
|
fn push(event: String, queue: State<'_, LogChannel>) {
|
||||||
queue.0.push(event);
|
queue.0.push(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/pop")]
|
#[get("/pop")]
|
||||||
fn pop(queue: State<LogChannel>) -> Option<String> {
|
fn pop(queue: State<'_, LogChannel>) -> Option<String> {
|
||||||
queue.0.pop().ok()
|
queue.0.pop().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "manual_routes"
|
name = "manual_routes"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "msgpack"
|
name = "msgpack"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -20,7 +19,7 @@ fn get(id: usize) -> MsgPack<Message<'static>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<data>", format = "msgpack")]
|
#[post("/", data = "<data>", format = "msgpack")]
|
||||||
fn create(data: MsgPack<Message>) -> String {
|
fn create(data: MsgPack<Message<'_>>) -> String {
|
||||||
data.contents.to_string()
|
data.contents.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use rocket;
|
use crate::rocket;
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::{Status, ContentType};
|
use rocket::http::{Status, ContentType};
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "optional_redirect"
|
name = "optional_redirect"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "pastebin"
|
name = "pastebin"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
extern crate rand;
|
|
||||||
|
|
||||||
mod paste_id;
|
mod paste_id;
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
@ -13,7 +12,7 @@ use std::path::Path;
|
||||||
use rocket::Data;
|
use rocket::Data;
|
||||||
use rocket::response::content;
|
use rocket::response::content;
|
||||||
|
|
||||||
use paste_id::PasteID;
|
use crate::paste_id::PasteID;
|
||||||
|
|
||||||
const HOST: &str = "http://localhost:8000";
|
const HOST: &str = "http://localhost:8000";
|
||||||
const ID_LENGTH: usize = 3;
|
const ID_LENGTH: usize = 3;
|
||||||
|
@ -29,7 +28,7 @@ fn upload(paste: Data) -> io::Result<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<id>")]
|
#[get("/<id>")]
|
||||||
fn retrieve(id: PasteID) -> Option<content::Plain<File>> {
|
fn retrieve(id: PasteID<'_>) -> Option<content::Plain<File>> {
|
||||||
let filename = format!("upload/{id}", id = id);
|
let filename = format!("upload/{id}", id = id);
|
||||||
File::open(&filename).map(|f| content::Plain(f)).ok()
|
File::open(&filename).map(|f| content::Plain(f)).ok()
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ impl<'a> PasteID<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Display for PasteID<'a> {
|
impl<'a> fmt::Display for PasteID<'a> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", self.0)
|
write!(f, "{}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "query_params"
|
name = "query_params"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -11,12 +11,12 @@ macro_rules! run_test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn age_and_name_params() {
|
fn age_and_name_params() {
|
||||||
run_test!("?age=10&name=john", |mut response: Response| {
|
run_test!("?age=10&name=john", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("Hello, 10 year old named john!".into()));
|
Some("Hello, 10 year old named john!".into()));
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test!("?age=20&name=john", |mut response: Response| {
|
run_test!("?age=20&name=john", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("20 years old? Hi, john!".into()));
|
Some("20 years old? Hi, john!".into()));
|
||||||
});
|
});
|
||||||
|
@ -24,12 +24,12 @@ fn age_and_name_params() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn age_param_only() {
|
fn age_param_only() {
|
||||||
run_test!("?age=10", |mut response: Response| {
|
run_test!("?age=10", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("We're gonna need a name, and only a name.".into()));
|
Some("We're gonna need a name, and only a name.".into()));
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test!("?age=20", |mut response: Response| {
|
run_test!("?age=20", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("We're gonna need a name, and only a name.".into()));
|
Some("We're gonna need a name, and only a name.".into()));
|
||||||
});
|
});
|
||||||
|
@ -37,19 +37,19 @@ fn age_param_only() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn name_param_only() {
|
fn name_param_only() {
|
||||||
run_test!("?name=John", |mut response: Response| {
|
run_test!("?name=John", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(), Some("Hello John!".into()));
|
assert_eq!(response.body_string(), Some("Hello John!".into()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_params() {
|
fn no_params() {
|
||||||
run_test!("", |mut response: Response| {
|
run_test!("", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("We're gonna need a name, and only a name.".into()));
|
Some("We're gonna need a name, and only a name.".into()));
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test!("?", |mut response: Response| {
|
run_test!("?", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("We're gonna need a name, and only a name.".into()));
|
Some("We're gonna need a name, and only a name.".into()));
|
||||||
});
|
});
|
||||||
|
@ -57,12 +57,12 @@ fn no_params() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn extra_params() {
|
fn extra_params() {
|
||||||
run_test!("?age=20&name=Bob&extra", |mut response: Response| {
|
run_test!("?age=20&name=Bob&extra", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("20 years old? Hi, Bob!".into()));
|
Some("20 years old? Hi, Bob!".into()));
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test!("?age=30&name=Bob&extra", |mut response: Response| {
|
run_test!("?age=30&name=Bob&extra", |mut response: Response<'_>| {
|
||||||
assert_eq!(response.body_string(),
|
assert_eq!(response.body_string(),
|
||||||
Some("We're gonna need a name, and only a name.".into()));
|
Some("We're gonna need a name, and only a name.".into()));
|
||||||
});
|
});
|
||||||
|
@ -70,7 +70,7 @@ fn extra_params() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn wrong_path() {
|
fn wrong_path() {
|
||||||
run_test!("/other?age=20&name=Bob", |response: Response| {
|
run_test!("/other?age=20&name=Bob", |response: Response<'_>| {
|
||||||
assert_eq!(response.status(), Status::NotFound);
|
assert_eq!(response.status(), Status::NotFound);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "ranking"
|
name = "ranking"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
|
|
||||||
fn test(uri: &str, expected: String) {
|
fn test(uri: &str, expected: String) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "raw_sqlite"
|
name = "raw_sqlite"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
extern crate rusqlite;
|
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::types::ToSql;
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
@ -25,7 +25,7 @@ fn init_database(conn: &Connection) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn hello(db_conn: State<DbConn>) -> Result<String, Error> {
|
fn hello(db_conn: State<'_, DbConn>) -> Result<String, Error> {
|
||||||
db_conn.lock()
|
db_conn.lock()
|
||||||
.expect("db connection lock")
|
.expect("db connection lock")
|
||||||
.query_row("SELECT name FROM entries WHERE id = 0",
|
.query_row("SELECT name FROM entries WHERE id = 0",
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "raw_upload"
|
name = "raw_upload"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "redirect"
|
name = "redirect"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "request_guard"
|
name = "request_guard"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "request_local_state"
|
name = "request_local_state"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard1 {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
||||||
let atomics = req.guard::<State<Atomics>>()?;
|
let atomics = req.guard::<State<'_, Atomics>>()?;
|
||||||
atomics.uncached.fetch_add(1, Ordering::Relaxed);
|
atomics.uncached.fetch_add(1, Ordering::Relaxed);
|
||||||
req.local_cache(|| atomics.cached.fetch_add(1, Ordering::Relaxed));
|
req.local_cache(|| atomics.cached.fetch_add(1, Ordering::Relaxed));
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "session"
|
name = "session"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro, never_type)]
|
#![feature(proc_macro_hygiene, decl_macro, never_type)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -35,7 +34,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/login", data = "<login>")]
|
#[post("/login", data = "<login>")]
|
||||||
fn login(mut cookies: Cookies, login: Form<Login>) -> Result<Redirect, Flash<Redirect>> {
|
fn login(mut cookies: Cookies<'_>, login: Form<Login>) -> Result<Redirect, Flash<Redirect>> {
|
||||||
if login.username == "Sergio" && login.password == "password" {
|
if login.username == "Sergio" && login.password == "password" {
|
||||||
cookies.add_private(Cookie::new("user_id", 1.to_string()));
|
cookies.add_private(Cookie::new("user_id", 1.to_string()));
|
||||||
Ok(Redirect::to(uri!(index)))
|
Ok(Redirect::to(uri!(index)))
|
||||||
|
@ -45,7 +44,7 @@ fn login(mut cookies: Cookies, login: Form<Login>) -> Result<Redirect, Flash<Red
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/logout")]
|
#[post("/logout")]
|
||||||
fn logout(mut cookies: Cookies) -> Flash<Redirect> {
|
fn logout(mut cookies: Cookies<'_>) -> Flash<Redirect> {
|
||||||
cookies.remove_private(Cookie::named("user_id"));
|
cookies.remove_private(Cookie::named("user_id"));
|
||||||
Flash::success(Redirect::to(uri!(login_page)), "Successfully logged out.")
|
Flash::success(Redirect::to(uri!(login_page)), "Successfully logged out.")
|
||||||
}
|
}
|
||||||
|
@ -56,7 +55,7 @@ fn login_user(_user: User) -> Redirect {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/login", rank = 2)]
|
#[get("/login", rank = 2)]
|
||||||
fn login_page(flash: Option<FlashMessage>) -> Template {
|
fn login_page(flash: Option<FlashMessage<'_, '_>>) -> Template {
|
||||||
let mut context = HashMap::new();
|
let mut context = HashMap::new();
|
||||||
if let Some(ref msg) = flash {
|
if let Some(ref msg) = flash {
|
||||||
context.insert("flash", msg.msg());
|
context.insert("flash", msg.msg());
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rocket::Response;
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::{Status, Cookie, ContentType};
|
use rocket::http::{Status, Cookie, ContentType};
|
||||||
|
|
||||||
fn user_id_cookie(response: &Response) -> Option<Cookie<'static>> {
|
fn user_id_cookie(response: &Response<'_>) -> Option<Cookie<'static>> {
|
||||||
let cookie = response.headers()
|
let cookie = response.headers()
|
||||||
.get("Set-Cookie")
|
.get("Set-Cookie")
|
||||||
.filter(|v| v.starts_with("user_id"))
|
.filter(|v| v.starts_with("user_id"))
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "state"
|
name = "state"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -12,7 +12,7 @@ use rocket::response::content;
|
||||||
struct HitCount(AtomicUsize);
|
struct HitCount(AtomicUsize);
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(hit_count: State<HitCount>) -> content::Html<String> {
|
fn index(hit_count: State<'_, HitCount>) -> content::Html<String> {
|
||||||
hit_count.0.fetch_add(1, Ordering::Relaxed);
|
hit_count.0.fetch_add(1, Ordering::Relaxed);
|
||||||
let msg = "Your visit has been recorded!";
|
let msg = "Your visit has been recorded!";
|
||||||
let count = format!("Visits: {}", count(hit_count));
|
let count = format!("Visits: {}", count(hit_count));
|
||||||
|
@ -20,7 +20,7 @@ fn index(hit_count: State<HitCount>) -> content::Html<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/count")]
|
#[get("/count")]
|
||||||
fn count(hit_count: State<HitCount>) -> String {
|
fn count(hit_count: State<'_, HitCount>) -> String {
|
||||||
hit_count.0.load(Ordering::Relaxed).to_string()
|
hit_count.0.load(Ordering::Relaxed).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "static_files"
|
name = "static_files"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "stream"
|
name = "stream"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "tera_templates"
|
name = "tera_templates"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate serde_json;
|
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
#[cfg(test)] mod tests;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
@ -31,7 +29,7 @@ fn get(name: String) -> Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(req: &Request) -> Template {
|
fn not_found(req: &Request<'_>) -> Template {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("path", req.uri().path());
|
map.insert("path", req.uri().path());
|
||||||
Template::render("error/404", &map)
|
Template::render("error/404", &map)
|
||||||
|
|
|
@ -15,7 +15,7 @@ macro_rules! dispatch {
|
||||||
fn test_root() {
|
fn test_root() {
|
||||||
// Check that the redirect works.
|
// Check that the redirect works.
|
||||||
for method in &[Get, Head] {
|
for method in &[Get, Head] {
|
||||||
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse| {
|
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse<'_>| {
|
||||||
assert_eq!(response.status(), Status::SeeOther);
|
assert_eq!(response.status(), Status::SeeOther);
|
||||||
assert!(response.body().is_none());
|
assert!(response.body().is_none());
|
||||||
|
|
||||||
|
@ -26,8 +26,8 @@ fn test_root() {
|
||||||
|
|
||||||
// Check that other request methods are not accepted (and instead caught).
|
// Check that other request methods are not accepted (and instead caught).
|
||||||
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
|
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
|
||||||
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse| {
|
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let mut map = ::std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
map.insert("path", "/");
|
map.insert("path", "/");
|
||||||
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ fn test_root() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_name() {
|
fn test_name() {
|
||||||
// Check that the /hello/<name> route works.
|
// Check that the /hello/<name> route works.
|
||||||
dispatch!(Get, "/hello/Jack", |client: &Client, mut response: LocalResponse| {
|
dispatch!(Get, "/hello/Jack", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let context = super::TemplateContext {
|
let context = super::TemplateContext {
|
||||||
name: "Jack".into(),
|
name: "Jack".into(),
|
||||||
items: vec!["One", "Two", "Three"]
|
items: vec!["One", "Two", "Three"]
|
||||||
|
@ -55,8 +55,8 @@ fn test_name() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_404() {
|
fn test_404() {
|
||||||
// Check that the error catcher works.
|
// Check that the error catcher works.
|
||||||
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse| {
|
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse<'_>| {
|
||||||
let mut map = ::std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
map.insert("path", "/hello/");
|
map.insert("path", "/hello/");
|
||||||
|
|
||||||
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "testing"
|
name = "testing"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "tls"
|
name = "tls"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "todo"
|
name = "todo"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -17,7 +17,7 @@ use rocket::response::{Flash, Redirect};
|
||||||
use rocket_contrib::{templates::Template, serve::StaticFiles};
|
use rocket_contrib::{templates::Template, serve::StaticFiles};
|
||||||
use diesel::SqliteConnection;
|
use diesel::SqliteConnection;
|
||||||
|
|
||||||
use task::{Task, Todo};
|
use crate::task::{Task, Todo};
|
||||||
|
|
||||||
// This macro from `diesel_migrations` defines an `embedded_migrations` module
|
// This macro from `diesel_migrations` defines an `embedded_migrations` module
|
||||||
// containing a function named `run`. This allows the example to be run and
|
// containing a function named `run`. This allows the example to be run and
|
||||||
|
@ -71,7 +71,7 @@ fn delete(id: i32, conn: DbConn) -> Result<Flash<Redirect>, Template> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(msg: Option<FlashMessage>, conn: DbConn) -> Template {
|
fn index(msg: Option<FlashMessage<'_, '_>>, conn: DbConn) -> Template {
|
||||||
Template::render("index", &match msg {
|
Template::render("index", &match msg {
|
||||||
Some(ref msg) => Context::raw(&conn, Some((msg.name(), msg.msg()))),
|
Some(ref msg) => Context::raw(&conn, Some((msg.name(), msg.msg()))),
|
||||||
None => Context::raw(&conn, None),
|
None => Context::raw(&conn, None),
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
extern crate parking_lot;
|
|
||||||
extern crate rand;
|
|
||||||
|
|
||||||
use super::task::Task;
|
use super::task::Task;
|
||||||
use self::parking_lot::Mutex;
|
|
||||||
use self::rand::{Rng, thread_rng, distributions::Alphanumeric};
|
use parking_lot::Mutex;
|
||||||
|
use rand::{Rng, thread_rng, distributions::Alphanumeric};
|
||||||
|
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::{Status, ContentType};
|
use rocket::http::{Status, ContentType};
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
name = "uuid"
|
name = "uuid"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
workspace = "../../"
|
workspace = "../../"
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use] extern crate lazy_static;
|
||||||
extern crate rocket_contrib;
|
|
||||||
extern crate uuid;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use rocket_contrib::uuid::Uuid;
|
use rocket_contrib::uuid::Uuid;
|
||||||
|
|
Loading…
Reference in New Issue