From b8968aebbdfc77000265e340996f898814be1d78 Mon Sep 17 00:00:00 2001 From: Artus Date: Wed, 3 Jul 2019 14:32:35 +0200 Subject: [PATCH] adds cors to test in dev environment --- Cargo.toml | 1 + lootalot_db/src/lib.rs | 10 ++++++---- lootalot_front/src/AppStorage.js | 5 ++++- src/main.rs | 1 + src/server.rs | 7 +++++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 393700c..23835a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ env_logger = "*" futures = "0.1" diesel = "*" serde = "*" +actix-cors = "0.1.0" [workspace] members = [ diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 76e3757..54184b5 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -115,15 +115,17 @@ impl<'q> AsPlayer<'q> { /// Adds the value in gold to the player's wealth. /// /// Value can be negative to substract wealth. - pub fn update_wealth(self, value: f32) -> ActionResult { + pub fn update_wealth(self, value_in_gp: f32) -> ActionResult { use schema::players::dsl::*; - let current_wealth = players.find(self.id) + let current_wealth = players + .find(self.id) .select((cp, sp, gp, pp)) .first::(self.conn)?; // TODO: improve this // should be move inside a WealthUpdate method - let update = - models::WealthUpdate::from_gp(current_wealth.to_gp() + value); + let update = models::WealthUpdate::from_gp( + current_wealth.to_gp() + value_in_gp + ); diesel::update(players) .filter(id.eq(self.id)) .set(&update) diff --git a/lootalot_front/src/AppStorage.js b/lootalot_front/src/AppStorage.js index 5a44985..b7bf4d5 100644 --- a/lootalot_front/src/AppStorage.js +++ b/lootalot_front/src/AppStorage.js @@ -7,7 +7,9 @@ const PLAYER_LIST = [ ]; const fetchPlayerList = function () { - + fetch("http://localhost:8088/players") + .then(r => r.json()) + .then(r => console.log(r)); }; const fetchRequests = function () { @@ -25,6 +27,7 @@ export const AppStorage = { // Initiate the state initStorage (playerId) { if (this.debug) console.log('Initiate with player : ', playerId) + fetchPlayerList(); this.state.player_id = playerId; for (var idx in PLAYER_LIST) { var player = PLAYER_LIST[idx]; diff --git a/src/main.rs b/src/main.rs index f3ebe6d..668161f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate lootalot_db; mod server; fn main() { + std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); dotenv::dotenv().ok(); // TODO: Build a cli app with complete support of DbApi diff --git a/src/server.rs b/src/server.rs index 2c6e73b..d9aefdc 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,6 +2,7 @@ use std::env; use futures::Future; use actix_files as fs; use actix_web::{web, App, HttpServer, HttpResponse, Error}; +use actix_cors::Cors; use lootalot_db::{Pool, DbApi, QueryResult}; type AppPool = web::Data; @@ -56,6 +57,12 @@ pub(crate) fn serve() -> std::io::Result<()> { HttpServer::new(move || { App::new() .data(pool.clone()) + .wrap( + Cors::new() + .allowed_origin("http://localhost:8080") + .allowed_methods(vec!["GET", "POST"]) + .max_age(3600) + ) .route( "/players", web::get().to_async(move |pool: AppPool| {