extracts server in its own module
This commit is contained in:
@@ -48,7 +48,7 @@ impl Player {
|
|||||||
|
|
||||||
|
|
||||||
pub fn create_pool() -> Pool {
|
pub fn create_pool() -> Pool {
|
||||||
dotenv::dotenv().ok();
|
dbg!(dotenv::dotenv().ok());
|
||||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||||
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
||||||
r2d2::Pool::builder()
|
r2d2::Pool::builder()
|
||||||
|
|||||||
10
src/api.rs
10
src/api.rs
@@ -2,7 +2,7 @@ use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
|||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use lootalot_db::Pool;
|
use lootalot_db::Pool;
|
||||||
use lootalot_db::{ActionResult, DbConnection, Item, Player as PlayerData, QueryResult};
|
use lootalot_db::{ActionResult, DbConnection, Item, Player, QueryResult};
|
||||||
|
|
||||||
pub struct DbApi<'q>(&'q DbConnection);
|
pub struct DbApi<'q>(&'q DbConnection);
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ impl<'q> DbApi<'q> {
|
|||||||
Self(conn)
|
Self(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_players(self) -> QueryResult<Vec<PlayerData>> {
|
pub fn fetch_players(self) -> QueryResult<Vec<Player>> {
|
||||||
PlayerData::fetch_list(self.0)
|
Player::fetch_list(self.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
||||||
@@ -28,10 +28,10 @@ pub struct AsPlayer<'q> {
|
|||||||
impl<'q> AsPlayer<'q> {
|
impl<'q> AsPlayer<'q> {
|
||||||
/// Fetch the content of a player's chest
|
/// Fetch the content of a player's chest
|
||||||
pub fn loot(self) -> QueryResult<Vec<Item>> {
|
pub fn loot(self) -> QueryResult<Vec<Item>> {
|
||||||
PlayerData::fetch_loot(self.id, self.conn)
|
Player::fetch_loot(self.id, self.conn)
|
||||||
}
|
}
|
||||||
pub fn claim(self, item: i32) -> ActionResult {
|
pub fn claim(self, item: i32) -> ActionResult {
|
||||||
PlayerData::action_claim_object(self.id, item, self.conn)
|
Player::action_claim_object(self.id, item, self.conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
37
src/main.rs
37
src/main.rs
@@ -1,44 +1,13 @@
|
|||||||
extern crate dotenv;
|
extern crate dotenv;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate lootalot_db;
|
extern crate lootalot_db;
|
||||||
|
|
||||||
use actix_files as fs;
|
|
||||||
use actix_web::{web, App, HttpServer};
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
mod server;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() {
|
||||||
env::set_var("RUST_LOG", "actix_web=debug");
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
server::serve();
|
||||||
println!("serving files from: {}", &www_root);
|
|
||||||
|
|
||||||
use lootalot_db::Pool;
|
|
||||||
type AppPool = web::Data<Pool>;
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/server.rs
Normal file
34
src/server.rs
Normal 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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user