extracts server in its own module

This commit is contained in:
2019-06-22 15:23:29 +02:00
parent f5e495734f
commit a77fce417c
4 changed files with 43 additions and 40 deletions

34
src/server.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::env;
use actix_files as fs;
use actix_web::{web, App, HttpServer};
use lootalot_db::Pool;
use crate::api;
type AppPool = web::Data<Pool>;
pub(crate) fn serve() -> std::io::Result<()> {
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
dbg!(&www_root);
let pool = lootalot_db::create_pool();
HttpServer::new(move || {
App::new()
.data(pool.clone())
.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")?
.run()
}