cleans up a little + rustfmt
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
extern crate dotenv;
|
extern crate dotenv;
|
||||||
#[macro_use] extern crate diesel;
|
#[macro_use]
|
||||||
#[macro_use] extern crate serde_derive;
|
extern crate diesel;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager };
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
|
||||||
pub fn establish_connection() -> Result<SqliteConnection, String> {
|
pub fn establish_connection() -> Result<SqliteConnection, String> {
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
@@ -15,11 +17,15 @@ pub fn establish_connection() -> Result<SqliteConnection, String> {
|
|||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
pub fn list_players(conn: &SqliteConnection)
|
pub use models::Player;
|
||||||
-> Result<Vec<models::Player>, diesel::result::Error> {
|
|
||||||
use schema::players::dsl::*;
|
impl Player {
|
||||||
Ok(players
|
pub fn fetch_list(
|
||||||
.load::<models::Player>(conn)?)
|
conn: &SqliteConnection,
|
||||||
|
) -> Result<Vec<models::Player>, diesel::result::Error> {
|
||||||
|
use schema::players::dsl::*;
|
||||||
|
Ok(players.load::<models::Player>(conn)?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||||
@@ -33,10 +39,6 @@ pub fn create_pool() -> Pool {
|
|||||||
.expect("Failed to create pool.")
|
.expect("Failed to create pool.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::serde_derive::{Serialize};
|
|
||||||
use crate::schema::players;
|
use crate::schema::players;
|
||||||
|
use crate::serde_derive::Serialize;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
#[derive(Debug, Queryable, Serialize)]
|
#[derive(Debug, Queryable, Serialize)]
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@@ -4,29 +4,33 @@ extern crate env_logger;
|
|||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate lootalot_db;
|
extern crate lootalot_db;
|
||||||
|
|
||||||
use std::env;
|
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::{web, HttpServer, App};
|
use actix_web::{web, App, HttpServer};
|
||||||
use lootalot_db::Pool;
|
use lootalot_db::Pool;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
mod api {
|
mod api {
|
||||||
|
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use actix_web::{web, HttpResponse, Result, Error, web::Json};
|
|
||||||
|
|
||||||
use super::Pool;
|
use super::Pool;
|
||||||
use lootalot_db::models::Player;
|
use lootalot_db::Player;
|
||||||
struct Item;
|
struct Item;
|
||||||
|
|
||||||
pub fn players_list(pool: web::Data<Pool>) -> impl Future<Item=HttpResponse, Error=Error> {
|
pub fn players_list(pool: web::Data<Pool>) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
web::block( move || {
|
web::block(move || {
|
||||||
let conn = pool.get().unwrap();
|
let conn = pool.get().unwrap();
|
||||||
println!("Waiting for player list...");
|
println!("Waiting for player list...");
|
||||||
lootalot_db::list_players(&conn)
|
Player::fetch_list(&conn)
|
||||||
})
|
})
|
||||||
.then(|res| {
|
.then(|res| match res {
|
||||||
match res {
|
Ok(players) => {
|
||||||
Ok(players) => { println!("Ok! {:?}", &players); HttpResponse::Ok().json(players)},
|
println!("Ok! {:?}", &players);
|
||||||
Err(_) => { println!("Error!"); HttpResponse::InternalServerError().finish()},
|
HttpResponse::Ok().json(players)
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
println!("Error!");
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -45,7 +49,6 @@ mod api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
@@ -61,11 +64,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.data(pool.clone())
|
.data(pool.clone())
|
||||||
.route("/players", web::get().to_async(api::players_list))
|
.route("/players", web::get().to_async(api::players_list))
|
||||||
.service(
|
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||||
fs::Files::new("/", www_root.clone())
|
})
|
||||||
.index_file("index.html")
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.bind("127.0.0.1:8088")?
|
.bind("127.0.0.1:8088")?
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user