refactors api

This commit is contained in:
2019-06-22 14:57:51 +02:00
parent 23adaa3e79
commit f5e495734f
3 changed files with 59 additions and 73 deletions

View File

@@ -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")?