Cleanup of main

This commit is contained in:
Rohan Sircar 2020-08-12 02:03:43 +05:30
parent 667728bb02
commit 7683636a4e
2 changed files with 29 additions and 51 deletions

View File

@ -2,20 +2,15 @@
extern crate diesel; extern crate diesel;
#[macro_use] #[macro_use]
extern crate derive_new; extern crate derive_new;
// #[macro_use]
// extern crate comp;
// #[macro_use]
// extern crate validator_derive;
extern crate bcrypt; extern crate bcrypt;
extern crate custom_error; extern crate custom_error;
extern crate regex; extern crate regex;
extern crate validator; extern crate validator;
use actix_web::{ 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; use actix_web_httpauth::middleware::HttpAuthentication;
@ -27,10 +22,6 @@ use actix_files as fs;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager}; use diesel::r2d2::{self, ConnectionManager};
// use middlewares::csrf;
// use routes;
// use routes::users;
// use utils;
mod actions; mod actions;
mod errors; mod errors;
@ -44,36 +35,6 @@ mod utils;
#[macro_use] #[macro_use]
extern crate log; 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<HttpResponse, Error> {
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<MyObj>) -> HttpResponse {
debug!("model: {:?}", item);
HttpResponse::Ok().json(item.0) // <- send response
}
#[actix_rt::main] #[actix_rt::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -108,20 +69,9 @@ async fn main() -> std::io::Result<()> {
.same_site(SameSite::Lax), .same_site(SameSite::Lax),
)) ))
.wrap(middleware::Logger::default()) .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( .service(
web::scope("/api/authzd") // endpoint requiring authentication web::scope("/api/authzd") // endpoint requiring authentication
.wrap(basic_auth_middleware.clone()) .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_user)
.service(routes::users::get_all_users), .service(routes::users::get_all_users),
) )

28
src/utils/extra.rs Normal file
View File

@ -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<HttpResponse, Error> {
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<MyObj>) -> HttpResponse {
debug!("model: {:?}", item);
HttpResponse::Ok().json(item.0) // <- send response
}