less verbose logger
This commit is contained in:
14
src/api.rs
14
src/api.rs
@@ -12,6 +12,14 @@ pub struct BuySellParams {
|
||||
global_mod: Option<f64>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct NewGroupLoot {
|
||||
source_name: String,
|
||||
pub items: Vec<db::Item>,
|
||||
}
|
||||
|
||||
|
||||
/// A generic response for all queries
|
||||
#[derive(Serialize, Debug, Default)]
|
||||
pub struct ApiResponse {
|
||||
@@ -74,7 +82,7 @@ pub enum ApiActions {
|
||||
UnclaimItem(i32, i32),
|
||||
UndoLastAction(i32),
|
||||
// Group level
|
||||
AddLoot(Vec<db::Item>),
|
||||
AddLoot(NewGroupLoot),
|
||||
// Admin level
|
||||
//AddPlayer(String, f64),
|
||||
//AddInventoryItem(pub String, pub i32),
|
||||
@@ -233,9 +241,9 @@ pub fn execute(
|
||||
None
|
||||
}
|
||||
// Group actions
|
||||
ApiActions::AddLoot(items) => {
|
||||
ApiActions::AddLoot(data) => {
|
||||
let mut added_items = 0;
|
||||
for item in items.into_iter() {
|
||||
for item in data.items.into_iter() {
|
||||
if let Ok(added) = db::LootManager(conn, 0).add_from(&item) {
|
||||
response.push_update(added);
|
||||
added_items += 1;
|
||||
|
||||
@@ -1,31 +1,28 @@
|
||||
use actix_cors::Cors;
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer, Either};
|
||||
use futures::{Future};
|
||||
use actix_web::{middleware, web, App, Either, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
use std::env;
|
||||
|
||||
use lootalot_db as db;
|
||||
use crate::api;
|
||||
use lootalot_db as db;
|
||||
|
||||
type AppPool = web::Data<db::Pool>;
|
||||
type PlayerId = web::Path<i32>;
|
||||
type ItemId = web::Json<i32>;
|
||||
type IdList = web::Json<api::IdList>;
|
||||
type BuySellParams = web::Json<api::BuySellParams>;
|
||||
type NewGroupLoot = web::Json<api::NewGroupLoot>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct NewGroupLoot {
|
||||
source_name: String,
|
||||
items: Vec<db::Item>,
|
||||
}
|
||||
type MaybeForbidden =
|
||||
actix_web::Either<Box<dyn Future<Item = HttpResponse, Error = Error>>, HttpResponse>;
|
||||
|
||||
type MaybeForbidden = actix_web::Either<Box<dyn Future<Item = HttpResponse, Error = Error>>, HttpResponse>;
|
||||
|
||||
/// Wraps call to the database query and convert its result as a async HttpResponse
|
||||
fn db_call(
|
||||
pool: AppPool,
|
||||
query: api::ApiActions,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
{
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let conn = pool.get().unwrap();
|
||||
web::block(move || api::execute(&conn, query)).then(|res| match res {
|
||||
Ok(r) => HttpResponse::Ok().json(r),
|
||||
@@ -42,7 +39,6 @@ fn restricted_to_group(id: i32, params: (AppPool, api::ApiActions)) -> MaybeForb
|
||||
} else {
|
||||
Either::A(Box::new(db_call(params.0, params.1)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn configure_api(config: &mut web::ServiceConfig) {
|
||||
@@ -52,26 +48,30 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
||||
.service(
|
||||
web::scope("/players")
|
||||
.service(
|
||||
web::resource("/").route(
|
||||
web::get().to_async(|pool| db_call(pool, Q::FetchPlayers)),
|
||||
), //.route(web::post().to_async(endpoints::new_player))
|
||||
web::resource("/")
|
||||
.route(web::get().to_async(|pool| db_call(pool, Q::FetchPlayers))), //.route(web::post().to_async(endpoints::new_player))
|
||||
) // List of players
|
||||
.service(
|
||||
web::scope("/{player_id}")
|
||||
.route("/", web::get().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::FetchPlayer(*player))
|
||||
}))
|
||||
.route("/notifications", web::get().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::FetchNotifications(*player))
|
||||
}))
|
||||
.route(
|
||||
"/",
|
||||
web::get().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::FetchPlayer(*player))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/notifications",
|
||||
web::get().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::FetchNotifications(*player))
|
||||
}),
|
||||
)
|
||||
.service(
|
||||
web::resource("/claims")
|
||||
//.route(web::get().to_async(endpoints::player_claims))
|
||||
.route(web::post().to_async(
|
||||
|pool, (player, data): (PlayerId, IdList)|
|
||||
{
|
||||
db_call(pool, Q::ClaimItems(*player, data.clone()))
|
||||
}
|
||||
|pool, (player, data): (PlayerId, IdList)| {
|
||||
db_call(pool, Q::ClaimItems(*player, data.into_inner()))
|
||||
},
|
||||
))
|
||||
.route(web::put().to_async(
|
||||
|pool, (player, data): (PlayerId, ItemId)| {
|
||||
@@ -80,10 +80,7 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
||||
))
|
||||
.route(web::delete().to_async(
|
||||
|pool, (player, data): (PlayerId, ItemId)| {
|
||||
db_call(
|
||||
pool,
|
||||
Q::UnclaimItem(*player, *data),
|
||||
)
|
||||
db_call(pool, Q::UnclaimItem(*player, *data))
|
||||
},
|
||||
)),
|
||||
)
|
||||
@@ -92,10 +89,7 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
||||
//.route(web::get().to_async(...))
|
||||
.route(web::put().to_async(
|
||||
|pool, (player, data): (PlayerId, web::Json<f64>)| {
|
||||
db_call(
|
||||
pool,
|
||||
Q::UpdateWealth(*player, *data),
|
||||
)
|
||||
db_call(pool, Q::UpdateWealth(*player, *data))
|
||||
},
|
||||
)),
|
||||
)
|
||||
@@ -105,35 +99,39 @@ fn configure_api(config: &mut web::ServiceConfig) {
|
||||
db_call(pool, Q::FetchLoot(*player))
|
||||
}))
|
||||
.route(web::put().to_async(
|
||||
move |pool, (player, data): (PlayerId, web::Json<api::BuySellParams>)| {
|
||||
move |pool, (player, data): (PlayerId, BuySellParams)| {
|
||||
db_call(pool, Q::BuyItems(*player, data.into_inner()))
|
||||
},
|
||||
))
|
||||
.route(web::post().to(
|
||||
move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| {
|
||||
restricted_to_group(*player, (pool, Q::AddLoot(data.into_inner().items)))
|
||||
move |pool, (player, data): (PlayerId, NewGroupLoot)| {
|
||||
restricted_to_group(
|
||||
*player,
|
||||
(pool, Q::AddLoot(data.into_inner())),
|
||||
)
|
||||
},
|
||||
))
|
||||
.route(web::delete().to_async(
|
||||
move |pool, (player, data): (PlayerId, web::Json<api::BuySellParams>)| {
|
||||
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
||||
move |pool, (player, data): (PlayerId, BuySellParams)| {
|
||||
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
||||
},
|
||||
)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/events")
|
||||
.route("/last", web::delete().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::UndoLastAction(*player))
|
||||
}))
|
||||
)
|
||||
|
||||
.service(web::scope("/events").route(
|
||||
"/last",
|
||||
web::delete().to_async(|pool, player: PlayerId| {
|
||||
db_call(pool, Q::UndoLastAction(*player))
|
||||
}),
|
||||
)),
|
||||
),
|
||||
)
|
||||
.route("/claims", web::get().to_async(|pool| db_call(pool, Q::FetchClaims)))
|
||||
.route(
|
||||
"/claims",
|
||||
web::get().to_async(|pool| db_call(pool, Q::FetchClaims)),
|
||||
)
|
||||
.route(
|
||||
"/items",
|
||||
web::get()
|
||||
.to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)),
|
||||
web::get().to_async(move |pool: AppPool| db_call(pool, Q::FetchInventory)),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -153,7 +151,8 @@ pub fn serve() -> std::io::Result<()> {
|
||||
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
||||
.max_age(3600),
|
||||
)
|
||||
.wrap(middleware::Logger::default())
|
||||
//.wrap(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::new("%r -> %s (%{User-Agent}i)"))
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
|
||||
Reference in New Issue
Block a user