cleans up
This commit is contained in:
@@ -1,11 +1,53 @@
|
||||
use std::env;
|
||||
use futures::Future;
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use lootalot_db::Pool;
|
||||
use crate::api;
|
||||
use actix_web::{web, App, HttpServer, HttpResponse, Error};
|
||||
use lootalot_db::{Pool, DbApi, QueryResult};
|
||||
|
||||
type AppPool = web::Data<Pool>;
|
||||
|
||||
/// Wraps call to the DbApi and process its result as a async HttpResponse
|
||||
///
|
||||
/// Provides a convenient way to call the api inside a route definition. Given a connection pool,
|
||||
/// access to the api is granted in a closure. The closure is called in a blocking way and should
|
||||
/// return a QueryResult.
|
||||
/// If the query succeeds, it's result is returned as JSON data. Otherwise, an InternalServerError
|
||||
/// is returned.
|
||||
///
|
||||
/// # Usage
|
||||
/// ```
|
||||
/// (...)
|
||||
/// .route("path/to/",
|
||||
/// move |pool: web::Data<Pool>| {
|
||||
/// // user data can be processed here
|
||||
/// // ...
|
||||
/// db_call(pool, move |api| {
|
||||
/// // ...do what you want with the api
|
||||
/// }
|
||||
/// }
|
||||
/// )
|
||||
/// ```
|
||||
pub fn db_call<
|
||||
J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
>(
|
||||
pool: AppPool,
|
||||
query: Q,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
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()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn serve() -> std::io::Result<()> {
|
||||
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
||||
dbg!(&www_root);
|
||||
@@ -17,14 +59,14 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
.route(
|
||||
"/players",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
api::db_call(pool, move |api| api.fetch_players())
|
||||
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())
|
||||
db_call(pool, move |api| api.as_player(player_id).loot())
|
||||
}),
|
||||
)
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
|
||||
Reference in New Issue
Block a user