diff --git a/Cargo.toml b/Cargo.toml index 5d198fe..0fd4ed9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ actix-web = "1.0.*" actix-files = "*" lootalot-db = { version = "0.1", path = "./lootalot_db" } dotenv = "*" +env_logger = "*" +futures = "0.1" +diesel = "*" [workspace] members = [ diff --git a/lootalot_db/Cargo.toml b/lootalot_db/Cargo.toml index 4b8ddf0..a09ca54 100644 --- a/lootalot_db/Cargo.toml +++ b/lootalot_db/Cargo.toml @@ -6,7 +6,9 @@ edition = "2018" [dependencies] dotenv = "*" +serde = "*" +serde_derive = "*" [dependencies.diesel] version = "1.0" -features = ["sqlite"] +features = ["sqlite", "r2d2"] diff --git a/lootalot_db/db.sqlite3 b/lootalot_db/db.sqlite3 index 9e908f0..da9f626 100644 Binary files a/lootalot_db/db.sqlite3 and b/lootalot_db/db.sqlite3 differ diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index ffc8932..256d49c 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -1,14 +1,13 @@ -#[macro_use] -extern crate diesel; -extern crate dotenv; + extern crate dotenv; +#[macro_use] extern crate diesel; +#[macro_use] extern crate serde_derive; use diesel::prelude::*; -use dotenv::dotenv; -use std::env; +use diesel::r2d2::{self, ConnectionManager }; pub fn establish_connection() -> Result { - dotenv().ok(); - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set !"); + dotenv::dotenv().ok(); + let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set !"); SqliteConnection::establish(&database_url) .map_err(|e| format!("Error connecting to {} : {:?}", database_url, e)) } @@ -16,14 +15,28 @@ pub fn establish_connection() -> Result { pub mod models; pub mod schema; -pub fn list_players() -> Vec { +pub fn list_players(conn: &SqliteConnection) + -> Result, diesel::result::Error> { use schema::players::dsl::*; - let conn = establish_connection().unwrap(); - players - .load::(&conn) - .expect("Error loading players") + Ok(players + .load::(conn)?) } +pub type Pool = r2d2::Pool>; + +pub fn create_pool() -> Pool { + dotenv::dotenv().ok(); + let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); + let manager = ConnectionManager::::new(connspec); + r2d2::Pool::builder() + .build(manager) + .expect("Failed to create pool.") +} + + + + + #[cfg(test)] mod tests { use super::*; diff --git a/lootalot_db/src/models.rs b/lootalot_db/src/models.rs index 16a1fc3..3d8e226 100644 --- a/lootalot_db/src/models.rs +++ b/lootalot_db/src/models.rs @@ -1,7 +1,8 @@ +use crate::serde_derive::{Serialize}; use crate::schema::players; use crate::*; -#[derive(Queryable)] +#[derive(Debug, Queryable, Serialize)] pub struct Player { id: i32, name: String, diff --git a/src/main.rs b/src/main.rs index b7870ed..ca89765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>> { - let players: Vec = Vec::new(); - Ok(Json(players)) - } - - fn player_info(_id: i32) -> Result> { - let player = Player; - Ok(Json(player)) + pub fn players_list(pool: web::Data) -> impl Future { + 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>> { @@ -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")