makes one database query work :)
This commit is contained in:
@@ -9,6 +9,9 @@ actix-web = "1.0.*"
|
|||||||
actix-files = "*"
|
actix-files = "*"
|
||||||
lootalot-db = { version = "0.1", path = "./lootalot_db" }
|
lootalot-db = { version = "0.1", path = "./lootalot_db" }
|
||||||
dotenv = "*"
|
dotenv = "*"
|
||||||
|
env_logger = "*"
|
||||||
|
futures = "0.1"
|
||||||
|
diesel = "*"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dotenv = "*"
|
dotenv = "*"
|
||||||
|
serde = "*"
|
||||||
|
serde_derive = "*"
|
||||||
|
|
||||||
[dependencies.diesel]
|
[dependencies.diesel]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
features = ["sqlite"]
|
features = ["sqlite", "r2d2"]
|
||||||
|
|||||||
Binary file not shown.
@@ -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 diesel::prelude::*;
|
||||||
use dotenv::dotenv;
|
use diesel::r2d2::{self, ConnectionManager };
|
||||||
use std::env;
|
|
||||||
|
|
||||||
pub fn establish_connection() -> Result<SqliteConnection, String> {
|
pub fn establish_connection() -> Result<SqliteConnection, String> {
|
||||||
dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set !");
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set !");
|
||||||
SqliteConnection::establish(&database_url)
|
SqliteConnection::establish(&database_url)
|
||||||
.map_err(|e| format!("Error connecting to {} : {:?}", database_url, e))
|
.map_err(|e| format!("Error connecting to {} : {:?}", database_url, e))
|
||||||
}
|
}
|
||||||
@@ -16,14 +15,28 @@ pub fn establish_connection() -> Result<SqliteConnection, String> {
|
|||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
pub fn list_players() -> Vec<models::Player> {
|
pub fn list_players(conn: &SqliteConnection)
|
||||||
|
-> Result<Vec<models::Player>, diesel::result::Error> {
|
||||||
use schema::players::dsl::*;
|
use schema::players::dsl::*;
|
||||||
let conn = establish_connection().unwrap();
|
Ok(players
|
||||||
players
|
.load::<models::Player>(conn)?)
|
||||||
.load::<models::Player>(&conn)
|
|
||||||
.expect("Error loading players")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
|
pub fn create_pool() -> Pool {
|
||||||
|
dotenv::dotenv().ok();
|
||||||
|
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||||
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
||||||
|
r2d2::Pool::builder()
|
||||||
|
.build(manager)
|
||||||
|
.expect("Failed to create pool.")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
use crate::serde_derive::{Serialize};
|
||||||
use crate::schema::players;
|
use crate::schema::players;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
#[derive(Queryable)]
|
#[derive(Debug, Queryable, Serialize)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
id: i32,
|
id: i32,
|
||||||
name: String,
|
name: String,
|
||||||
|
|||||||
40
src/main.rs
40
src/main.rs
@@ -1,26 +1,34 @@
|
|||||||
extern crate actix_web;
|
|
||||||
extern crate dotenv;
|
extern crate dotenv;
|
||||||
|
extern crate env_logger;
|
||||||
|
|
||||||
|
extern crate actix_web;
|
||||||
extern crate lootalot_db;
|
extern crate lootalot_db;
|
||||||
|
|
||||||
use actix_files as fs;
|
|
||||||
use actix_web::{HttpServer, App, web::Json};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use actix_files as fs;
|
||||||
|
use actix_web::{web, HttpServer, App};
|
||||||
|
use lootalot_db::Pool;
|
||||||
|
|
||||||
mod api {
|
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;
|
struct Item;
|
||||||
|
|
||||||
fn player_list() -> Result<Json<Vec<Player>>> {
|
pub fn players_list(pool: web::Data<Pool>) -> impl Future<Item=HttpResponse, Error=Error> {
|
||||||
let players: Vec<Player> = Vec::new();
|
web::block( move || {
|
||||||
Ok(Json(players))
|
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 player_info(_id: i32) -> Result<Json<Player>> {
|
|
||||||
let player = Player;
|
|
||||||
Ok(Json(player))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chest_content(_id: i32) -> Result<Json<Vec<Item>>> {
|
fn chest_content(_id: i32) -> Result<Json<Vec<Item>>> {
|
||||||
@@ -41,12 +49,18 @@ mod api {
|
|||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
env::set_var("RUST_LOG", "actix_web=debug");
|
||||||
|
env_logger::init();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
|
|
||||||
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
||||||
println!("serving files from: {}", &www_root);
|
println!("serving files from: {}", &www_root);
|
||||||
|
|
||||||
|
let pool = lootalot_db::create_pool();
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
|
.data(pool.clone())
|
||||||
|
.route("/players", web::get().to_async(api::players_list))
|
||||||
.service(
|
.service(
|
||||||
fs::Files::new("/", www_root.clone())
|
fs::Files::new("/", www_root.clone())
|
||||||
.index_file("index.html")
|
.index_file("index.html")
|
||||||
|
|||||||
Reference in New Issue
Block a user