From 7683636a4e1dff584b5b27462ead01f918496be8 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 12 Aug 2020 02:03:43 +0530 Subject: [PATCH] Cleanup of main --- src/main.rs | 52 +--------------------------------------------- src/utils/extra.rs | 28 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 51 deletions(-) create mode 100644 src/utils/extra.rs diff --git a/src/main.rs b/src/main.rs index 3716aa3..3fa960c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,20 +2,15 @@ extern crate diesel; #[macro_use] extern crate derive_new; -// #[macro_use] -// extern crate comp; -// #[macro_use] -// extern crate validator_derive; extern crate bcrypt; extern crate custom_error; extern crate regex; extern crate validator; use actix_web::{ - error, get, middleware, post, web, App, Error, HttpResponse, HttpServer, + middleware, web, App, HttpServer, }; -use yarte::Template; use actix_web_httpauth::middleware::HttpAuthentication; @@ -27,10 +22,6 @@ use actix_files as fs; use diesel::prelude::*; use diesel::r2d2::{self, ConnectionManager}; -// use middlewares::csrf; -// use routes; -// use routes::users; -// use utils; mod actions; mod errors; @@ -44,36 +35,6 @@ mod utils; #[macro_use] extern crate log; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -struct MyObj { - name: String, - // number: i32, -} - -#[get("/{id}/{name}")] -async fn index(info: web::Path<(u32, String)>) -> Result { - let (id, name) = (info.0, info.1.clone()); - let template = models::CardTemplate { - title: "My Title", - body: name, - num: id, - }; - template - .call() - .map(|body| HttpResponse::Ok().content_type("text/html").body(body)) - .map_err(|_| { - error::ErrorInternalServerError("Error while parsing template") - }) -} - -/// This handler uses json extractor -#[post("/extractor")] -async fn extract_my_obj(item: web::Json) -> HttpResponse { - debug!("model: {:?}", item); - HttpResponse::Ok().json(item.0) // <- send response -} #[actix_rt::main] async fn main() -> std::io::Result<()> { @@ -108,20 +69,9 @@ async fn main() -> std::io::Result<()> { .same_site(SameSite::Lax), )) .wrap(middleware::Logger::default()) - // .wrap(basic_auth_middleware.clone()) - // .service(extract_my_obj) - // .service(index) - // .service( - // web::scope("/api/users").wrap(basic_auth_middleware.clone()), - // ) - // .service(routes::users::get_user) - // .service(routes::users::get_all_users) - // .service(routes::users::add_user) .service( web::scope("/api/authzd") // endpoint requiring authentication .wrap(basic_auth_middleware.clone()) - // .route("/get/{user_id}", web::to(routes::users::get_user)) - // .route("/get", web::to(routes::users::get_all_users)) .service(routes::users::get_user) .service(routes::users::get_all_users), ) diff --git a/src/utils/extra.rs b/src/utils/extra.rs new file mode 100644 index 0000000..e4de43e --- /dev/null +++ b/src/utils/extra.rs @@ -0,0 +1,28 @@ +#[derive(Debug, Serialize, Deserialize)] +struct MyObj { + name: String, + // number: i32, +} + +#[get("/{id}/{name}")] +async fn index(info: web::Path<(u32, String)>) -> Result { + let (id, name) = (info.0, info.1.clone()); + let template = models::CardTemplate { + title: "My Title", + body: name, + num: id, + }; + template + .call() + .map(|body| HttpResponse::Ok().content_type("text/html").body(body)) + .map_err(|_| { + error::ErrorInternalServerError("Error while parsing template") + }) +} + +/// This handler uses json extractor +#[post("/extractor")] +async fn extract_my_obj(item: web::Json) -> HttpResponse { + debug!("model: {:?}", item); + HttpResponse::Ok().json(item.0) // <- send response +} \ No newline at end of file