makes one database query work :)

This commit is contained in:
2019-06-19 15:26:27 +02:00
parent f589751f8c
commit 396989f3dd
6 changed files with 61 additions and 28 deletions

View File

@@ -1,26 +1,34 @@
extern crate actix_web;
extern crate dotenv;
extern crate env_logger;
extern crate actix_web;
extern crate lootalot_db;
use actix_files as fs;
use actix_web::{HttpServer, App, web::Json};
use std::env;
use actix_files as fs;
use actix_web::{web, HttpServer, App};
use lootalot_db::Pool;
mod api {
use actix_web::{ Result, web::Json};
use futures::Future;
use actix_web::{web, HttpResponse, Result, Error, web::Json};
struct Player;
use super::Pool;
use lootalot_db::models::Player;
struct Item;
fn player_list() -> Result<Json<Vec<Player>>> {
let players: Vec<Player> = Vec::new();
Ok(Json(players))
}
fn player_info(_id: i32) -> Result<Json<Player>> {
let player = Player;
Ok(Json(player))
pub fn players_list(pool: web::Data<Pool>) -> impl Future<Item=HttpResponse, Error=Error> {
web::block( move || {
let conn = pool.get().unwrap();
println!("Waiting for player list...");
lootalot_db::list_players(&conn)
})
.then(|res| {
match res {
Ok(players) => { println!("Ok! {:?}", &players); HttpResponse::Ok().json(players)},
Err(_) => { println!("Error!"); HttpResponse::InternalServerError().finish()},
}
})
}
fn chest_content(_id: i32) -> Result<Json<Vec<Item>>> {
@@ -41,12 +49,18 @@ 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);
let pool = lootalot_db::create_pool();
HttpServer::new(move || {
App::new()
.data(pool.clone())
.route("/players", web::get().to_async(api::players_list))
.service(
fs::Files::new("/", www_root.clone())
.index_file("index.html")