From 8c7a0ab29bcdd2ec93545557745555ef3239de34 Mon Sep 17 00:00:00 2001 From: Gorm Casper Date: Wed, 4 Jul 2018 17:12:33 +0200 Subject: [PATCH] create form example --- Cargo.lock | 80 ++++++++++++++++++++++++------------------- Cargo.toml | 1 + form/Cargo.toml | 10 ++++++ form/src/main.rs | 80 +++++++++++++++++++++++++++++++++++++++++++ form/static/form.html | 39 +++++++++++++++++++++ 5 files changed, 175 insertions(+), 35 deletions(-) create mode 100644 form/Cargo.toml create mode 100644 form/src/main.rs create mode 100644 form/static/form.html diff --git a/Cargo.lock b/Cargo.lock index 06c7917e..df4c8d04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,7 +35,7 @@ dependencies = [ "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "redis-async 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -75,7 +75,7 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "serde_urlencoded 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -99,8 +99,8 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -177,8 +177,8 @@ dependencies = [ "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -200,8 +200,8 @@ dependencies = [ "actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "validator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "validator_derive 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -342,8 +342,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -520,8 +520,8 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -703,6 +703,16 @@ name = "foreign-types-shared" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "form-example" +version = "0.1.0" +dependencies = [ + "actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -887,8 +897,8 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.11.13 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -901,8 +911,8 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "juniper_codegen 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "ordermap 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -916,8 +926,8 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "juniper 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1178,7 +1188,7 @@ name = "ordermap" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1374,8 +1384,8 @@ dependencies = [ "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1602,7 +1612,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1612,12 +1622,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.66" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.66" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1632,7 +1642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1642,7 +1652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1819,7 +1829,7 @@ dependencies = [ "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "pest_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "slug 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2164,7 +2174,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2185,8 +2195,8 @@ dependencies = [ "idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2248,7 +2258,7 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2265,8 +2275,8 @@ dependencies = [ "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2503,8 +2513,8 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" -"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79" +"checksum serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)" = "210e5a3b159c566d7527e9b22e44be73f2e0fcc330bb78fef4dbccb56d2e74c8" +"checksum serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)" = "dd724d68017ae3a7e63600ee4b2fdb3cad2158ffd1821d44aff4580f63e2b593" "checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875" "checksum serde_urlencoded 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e703cef904312097cfceab9ce131ff6bbe09e8c964a0703345a5f49238757bc1" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" diff --git a/Cargo.toml b/Cargo.toml index 1750cf17..76331ac4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "cookie-auth", "cookie-session", "diesel", + "form", "hello-world", "http-proxy", "json", diff --git a/form/Cargo.toml b/form/Cargo.toml new file mode 100644 index 00000000..1b1b2909 --- /dev/null +++ b/form/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "form-example" +version = "0.1.0" +authors = ["Gorm Casper "] + +[dependencies] +actix = "0.5" +actix-web = "^0.6" +serde = "1.0.69" +serde_derive = "1.0.69" diff --git a/form/src/main.rs b/form/src/main.rs new file mode 100644 index 00000000..615fff2e --- /dev/null +++ b/form/src/main.rs @@ -0,0 +1,80 @@ +extern crate actix; +extern crate actix_web; + +#[macro_use] +extern crate serde_derive; + +use actix_web::{ + http, middleware, server, App, Form, HttpRequest, HttpResponse, Result, State, +}; + +struct AppState { + foo: String, +} + +fn main() { + let sys = actix::System::new("form-example"); + + let _addr = server::new(|| { + App::with_state(AppState { + foo: "bar".to_string(), + }).middleware(middleware::Logger::default()) + .resource("/", |r| { + r.method(http::Method::GET).with(index); + }) + .resource("/post1", |r| { + r.method(http::Method::POST).with(handle_post_1) + }) + .resource("/post2", |r| { + r.method(http::Method::POST).with(handle_post_2) + }) + .resource("/post3", |r| { + r.method(http::Method::POST).with(handle_post_3) + }) + }).bind("127.0.0.1:8080") + .expect("Can not bind to 127.0.0.1:8080") + .start(); + + println!("Starting http server: 127.0.0.1:8080"); + let _ = sys.run(); +} + +fn index(_req: HttpRequest) -> Result { + Ok(HttpResponse::build(http::StatusCode::OK) + .content_type("text/html; charset=utf-8") + .body(include_str!("../static/form.html"))) +} + +#[derive(Deserialize)] +pub struct MyParams { + name: String, +} + +/// Simple handle POST request +fn handle_post_1(params: Form) -> Result { + Ok(HttpResponse::build(http::StatusCode::OK) + .content_type("text/plain") + .body(format!("Your name is {}", params.name))) +} + +/// State and POST Params +fn handle_post_2( + (state, params): (State, Form), +) -> Result { + Ok(HttpResponse::build(http::StatusCode::OK) + .content_type("text/plain") + .body(format!( + "Your name is {}, and in AppState I have foo: {}", + params.name, state.foo + ))) +} + +/// Request and POST Params +fn handle_post_3( + (req, params): (HttpRequest, Form), +) -> Result { + println!("Handling POST request: {:?}", req); + Ok(HttpResponse::build(http::StatusCode::OK) + .content_type("text/plain") + .body(format!("Your name is {}", params.name))) +} diff --git a/form/static/form.html b/form/static/form.html new file mode 100644 index 00000000..13aad83f --- /dev/null +++ b/form/static/form.html @@ -0,0 +1,39 @@ + + + + + Forms + + +

Will hit handle_post_1

+
+ + +
+ +
+ +

Will hit handle_post_2

+
+ + +
+ +
+ +

Will hit handle_post_3

+
+ + +
+ +