From 6811ba87155614455b29163222aeba12a221a3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Tue, 7 May 2019 00:14:51 +0100 Subject: [PATCH] add initial validation structure --- Cargo.lock | 7 ++++--- Cargo.toml | 1 + src/actions.rs | 8 ++++++++ src/controllers.rs | 19 ++++++++++++++++--- src/main.rs | 2 ++ src/utils.rs | 5 +++++ 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/actions.rs diff --git a/Cargo.lock b/Cargo.lock index 34910a1..7a6617c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,7 +192,7 @@ dependencies = [ "humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1204,6 +1204,7 @@ dependencies = [ "slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1246,7 +1247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.89" +version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2032,7 +2033,7 @@ dependencies = [ "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.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560" -"checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c" +"checksum serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)" = "58fc82bec244f168b23d1963b45c8bf5726e9a15a9d146a067f9081aeed2de79" "checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d48f9f99cd749a2de71d29da5f948de7f2764cc5a9d7f3c97e3514d4ee6eabf2" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" diff --git a/Cargo.toml b/Cargo.toml index 700a66f..74e40ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ askama = "0.8" slog = "2.4.1" slog-term = "2.4.0" slog-async = "2.3.0" +url = "1.7.2" diff --git a/src/actions.rs b/src/actions.rs new file mode 100644 index 0000000..5d19383 --- /dev/null +++ b/src/actions.rs @@ -0,0 +1,8 @@ +pub fn create_subscription() { + +} + + +pub fn remove_subscription() { + +} diff --git a/src/controllers.rs b/src/controllers.rs index a817d33..3ddf9ef 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -1,6 +1,8 @@ -use actix_web::{HttpRequest, HttpResponse}; +use actions::{create_subscription, remove_subscription}; +use actix_web::{http, HttpRequest, HttpResponse}; use askama::Template; -use utils::AppState; +use url::form_urlencoded; +use utils::{validate_parsed_data, AppState}; #[derive(Template)] #[template(path = "index.html")] @@ -12,7 +14,18 @@ pub fn index(_req: HttpRequest) -> HttpResponse { .body(IndexView.render().unwrap()) } -pub fn hub(_req: HttpRequest) -> HttpResponse { +pub fn hub(_req: HttpRequest, params: String) -> HttpResponse { + let log = &_req.state().log; + info!(log, "Received Request"); + debug!(log, "Content: {}", params); + let parsed_data = form_urlencoded::parse(params.as_bytes()); + + if !validate_parsed_data(parsed_data) { + return HttpResponse::Ok() + .status(http::StatusCode::from_u16(400).unwrap()) + .finish(); + } + HttpResponse::Ok().body("Hello World!") } diff --git a/src/main.rs b/src/main.rs index e61a87d..f91cf39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ extern crate diesel; extern crate slog; extern crate slog_async; extern crate slog_term; +extern crate url; use actix_web::actix::{SyncArbiter, System}; use actix_web::{http, server, App}; use clap::Arg; @@ -14,6 +15,7 @@ use controllers::{hub, index}; use diesel::prelude::*; use utils::{setup_logging, AppState, DbExecutor}; +mod actions; mod controllers; mod models; mod schema; diff --git a/src/utils.rs b/src/utils.rs index 0182088..b6e19e8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,7 @@ use actix_web::actix::{Actor, Addr, SyncContext}; use diesel::prelude::*; use slog::Drain; +use url::form_urlencoded::Parse; pub struct DbExecutor(pub SqliteConnection); @@ -19,3 +20,7 @@ pub fn setup_logging() -> slog::Logger { let drain = slog_async::Async::new(drain).build().fuse(); slog::Logger::root(drain, o!()) } + +pub fn validate_parsed_data(data: Parse) -> bool { + false +}