refactors api
This commit is contained in:
@@ -12,6 +12,7 @@ dotenv = "*"
|
||||
env_logger = "*"
|
||||
futures = "0.1"
|
||||
diesel = "*"
|
||||
serde = "*"
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
|
||||
110
src/api.rs
110
src/api.rs
@@ -1,32 +1,37 @@
|
||||
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
||||
use futures::Future;
|
||||
|
||||
use lootalot_db::{Pool};
|
||||
use lootalot_db::Pool;
|
||||
use lootalot_db::{ActionResult, DbConnection, Item, Player as PlayerData, QueryResult};
|
||||
|
||||
// Wrapper for database queries.
|
||||
//fn db_query(q: fn(...)) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
//
|
||||
//}
|
||||
pub struct DbApi<'q>(&'q DbConnection);
|
||||
|
||||
mod player {
|
||||
use lootalot_db::{Player as PlayerData, Item, ActionResult, QueryResult, DbConnection};
|
||||
|
||||
/// A reference to a player in database
|
||||
pub(super) struct Player(pub(super) i32);
|
||||
|
||||
impl Player {
|
||||
/// Fetch all players from db
|
||||
pub(super) fn all(conn: &DbConnection) -> QueryResult<Vec<PlayerData>> {
|
||||
PlayerData::fetch_list(conn)
|
||||
impl<'q> DbApi<'q> {
|
||||
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
||||
Self(conn)
|
||||
}
|
||||
|
||||
pub fn fetch_players(self) -> QueryResult<Vec<PlayerData>> {
|
||||
PlayerData::fetch_list(self.0)
|
||||
}
|
||||
|
||||
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
||||
AsPlayer { id, conn: self.0 }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsPlayer<'q> {
|
||||
id: i32,
|
||||
conn: &'q DbConnection,
|
||||
}
|
||||
|
||||
impl<'q> AsPlayer<'q> {
|
||||
/// Fetch the content of a player's chest
|
||||
pub(super) fn loot(self, conn: &DbConnection) -> QueryResult<Vec<Item>> {
|
||||
PlayerData::fetch_loot(self.0, conn)
|
||||
pub fn loot(self) -> QueryResult<Vec<Item>> {
|
||||
PlayerData::fetch_loot(self.id, self.conn)
|
||||
}
|
||||
pub(super) fn claim(self, item: i32, conn: &DbConnection) -> ActionResult {
|
||||
PlayerData::action_claim_object(self.0, item, conn)
|
||||
}
|
||||
|
||||
pub fn claim(self, item: i32) -> ActionResult {
|
||||
PlayerData::action_claim_object(self.id, item, self.conn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,62 +44,31 @@ mod player {
|
||||
// .claim(item_id) -> Success status (bool)
|
||||
// .unclaim(item_id) -> Success status (bool)
|
||||
// .sell(item_id) -> Success status (bool, earned)
|
||||
// .income(wealth_data) -> Success status (bool, new_wealth)
|
||||
// .with_admin()
|
||||
// .resolve_claims()
|
||||
//
|
||||
|
||||
// fn db_call(f: fn(DbApi) -> ApiResponse) { ... }
|
||||
// ApiResponse needs to be Serialize
|
||||
|
||||
// endpoint example
|
||||
// fn endpoint_name(params...) -> impl ... {
|
||||
// let param_1 = { ... };
|
||||
// db_call(move |api| api.with_player(param_1).loot())
|
||||
// }
|
||||
|
||||
pub fn players_list(
|
||||
pool: web::Data<Pool>
|
||||
pub fn db_call<
|
||||
J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
>(
|
||||
pool: web::Data<Pool>,
|
||||
query: Q,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player::all(&conn)
|
||||
web::block(move || {
|
||||
let api = DbApi::with_conn(&conn);
|
||||
query(api)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => {
|
||||
HttpResponse::Ok().json(players)
|
||||
}
|
||||
Err(_) => {
|
||||
Ok(players) => HttpResponse::Ok().json(players),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn chest_content(
|
||||
player_id: web::Path<i32>,
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player(*player_id).loot(&conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(items) => HttpResponse::Ok().json(items),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
})
|
||||
}
|
||||
|
||||
fn put_request(
|
||||
params: web::Path<(i32, i32)>,
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let (player_id, item_id) = *params;
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player(player_id).claim(item_id, &conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(status) => HttpResponse::Ok().json(status),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
})
|
||||
}
|
||||
|
||||
fn withdraw_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
||||
Ok(Json(false))
|
||||
}
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -11,21 +11,32 @@ use std::env;
|
||||
mod api;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
println!("Hello, world!");
|
||||
|
||||
env::set_var("RUST_LOG", "actix_web=debug");
|
||||
env_logger::init();
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
||||
println!("serving files from: {}", &www_root);
|
||||
|
||||
use lootalot_db::Pool;
|
||||
type AppPool = web::Data<Pool>;
|
||||
|
||||
let pool = lootalot_db::create_pool();
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(pool.clone())
|
||||
.route("/players", web::get().to_async(api::players_list))
|
||||
.route("/loot/{player_id}", web::get().to_async(api::chest_content))
|
||||
.route(
|
||||
"/players",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
api::db_call(pool, move |api| api.fetch_players())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/loot/{player_id}",
|
||||
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
|
||||
let player_id: i32 = *player_id;
|
||||
api::db_call(pool, move |api| api.as_player(player_id).loot())
|
||||
}),
|
||||
)
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
|
||||
Reference in New Issue
Block a user