From 05a08ea337b3731094a9b9d16ba32632ef6481f7 Mon Sep 17 00:00:00 2001 From: Artus Date: Sun, 20 Oct 2019 21:23:57 +0200 Subject: [PATCH] adds forbidden_to_players wrapper for db_call --- src/server.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/server.rs b/src/server.rs index 5659e67..c2c8637 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,6 +1,6 @@ use actix_cors::Cors; use actix_files as fs; -use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer}; +use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either}; use futures::{Future, IntoFuture}; use std::env; @@ -18,8 +18,10 @@ struct NewGroupLoot { items: Vec, } +type MaybeForbidden = actix_web::Either>, HttpResponse>; + /// Wraps call to the database query and convert its result as a async HttpResponse -pub fn db_call( +fn db_call( pool: AppPool, query: api::ApiActions, ) -> impl Future @@ -34,6 +36,15 @@ pub fn db_call( }) } +fn forbidden_to_players(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForbidden { + if id != 0 { + Either::B(HttpResponse::Forbidden().finish()) + } else { + Either::A(Box::new(db_call(params.0, params.1))) + } + +} + fn configure_app(config: &mut web::ServiceConfig) { use api::ApiActions as Q; config.service( @@ -92,12 +103,9 @@ fn configure_app(config: &mut web::ServiceConfig) { db_call(pool, Q::BuyItems(*player, data.into_inner())) }, )) - .route(web::post().to_async( + .route(web::post().to( move |pool, (player, data): (PlayerId, web::Json)| { - match *player { - 0 => db_call(pool, Q::AddLoot(data.items.clone())), - _ => HttpResponse::Forbidden().finish().into_future(), - } + forbidden_to_players(*player, (pool, Q::AddLoot(data.items.clone()))) }, )) .route(web::delete().to_async(