small fix
This commit is contained in:
@@ -5,6 +5,7 @@ use futures::Future;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use std::env;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
type AppPool = web::Data<Pool>;
|
||||
|
||||
/// Wraps call to the DbApi and process its result as a async HttpResponse
|
||||
@@ -28,25 +29,25 @@ type AppPool = web::Data<Pool>;
|
||||
/// }
|
||||
/// )
|
||||
/// ```
|
||||
pub fn db_call<
|
||||
J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
>(
|
||||
pub fn db_call<J,Q>(
|
||||
pool: AppPool,
|
||||
query: Q,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
) -> impl Future<Item=HttpResponse, Error=Error>
|
||||
where J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
{
|
||||
let conn = pool.get().unwrap();
|
||||
web::block(move || {
|
||||
let api = DbApi::with_conn(&conn);
|
||||
query(api)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => HttpResponse::Ok().json(players),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => HttpResponse::Ok().json(players),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@@ -83,7 +84,7 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
.data(pool.clone())
|
||||
.wrap(
|
||||
Cors::new()
|
||||
.allowed_origin("http://localhost:8080")
|
||||
.allowed_origin("http://localhost:8088")
|
||||
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
||||
.max_age(3600),
|
||||
)
|
||||
@@ -113,25 +114,25 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.update_wealth(data.value_in_gp))
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/buy",
|
||||
web::post().to_async(move |pool: AppPool, data: web::Json<LootUpdate>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.buy(&data.items)
|
||||
.buy(&data.items),
|
||||
)
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/sell",
|
||||
web::post().to_async(move |pool: AppPool, data: web::Json<LootUpdate>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.sell(&data.items)
|
||||
.sell(&data.items),
|
||||
)
|
||||
})
|
||||
}),
|
||||
)
|
||||
)
|
||||
.service(
|
||||
@@ -167,7 +168,7 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
move |pool: AppPool, data: web::Json<NewPlayer>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_admin()
|
||||
.add_player(&data.name, data.wealth)
|
||||
.add_player(&data.name, data.wealth),
|
||||
)
|
||||
},
|
||||
),
|
||||
@@ -176,6 +177,6 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
)
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user