126 lines
3.5 KiB
Rust
126 lines
3.5 KiB
Rust
use iron::prelude::*;
|
|
use iron::status;
|
|
|
|
use iron::{BeforeMiddleware};
|
|
use persistent::Read;
|
|
use router::Router;
|
|
|
|
use serde::{Deserialize};
|
|
use uuid::Uuid;
|
|
|
|
use account;
|
|
use game;
|
|
|
|
use http::{State, MnmlHttpError, json_object};
|
|
|
|
struct AcpMiddleware;
|
|
impl BeforeMiddleware for AcpMiddleware {
|
|
fn before(&self, req: &mut Request) -> IronResult<()> {
|
|
match req.extensions.get::<account::Account>() {
|
|
Some(a) => {
|
|
if ["ntr", "mashy"].contains(&a.name.to_ascii_lowercase().as_ref()) {
|
|
return Ok(());
|
|
}
|
|
|
|
return Err(MnmlHttpError::Unauthorized.into());
|
|
},
|
|
None => Err(MnmlHttpError::Unauthorized.into()),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug,Clone,Deserialize)]
|
|
struct GetUser {
|
|
name: Option<String>,
|
|
id: Option<Uuid>,
|
|
}
|
|
|
|
fn acp_user(req: &mut Request) -> IronResult<Response> {
|
|
let state = req.get::<Read<State>>().unwrap();
|
|
let params = match req.get::<bodyparser::Struct<GetUser>>() {
|
|
Ok(Some(b)) => b,
|
|
_ => return Err(MnmlHttpError::BadRequest.into()),
|
|
};
|
|
|
|
let db = state.pool.get().or(Err(MnmlHttpError::DbError))?;
|
|
|
|
let user = match params.id {
|
|
Some(id) => account::select(&db, id)
|
|
.or(Err(MnmlHttpError::NotFound))?,
|
|
|
|
None => match params.name {
|
|
Some(n) => account::select_name(&db, &n)
|
|
.or(Err(MnmlHttpError::NotFound))?,
|
|
None => return Err(MnmlHttpError::BadRequest.into()),
|
|
}
|
|
};
|
|
|
|
Ok(json_object(status::Ok, serde_json::to_string(&user).unwrap()))
|
|
}
|
|
|
|
#[derive(Debug,Clone,Deserialize)]
|
|
struct GetGame {
|
|
id: Uuid,
|
|
}
|
|
|
|
fn acp_game(req: &mut Request) -> IronResult<Response> {
|
|
let state = req.get::<Read<State>>().unwrap();
|
|
let params = match req.get::<bodyparser::Struct<GetGame>>() {
|
|
Ok(Some(b)) => b,
|
|
_ => return Err(MnmlHttpError::BadRequest.into()),
|
|
};
|
|
|
|
let db = state.pool.get().or(Err(MnmlHttpError::DbError))?;
|
|
|
|
let game = game::select(&db, params.id)
|
|
.or(Err(MnmlHttpError::NotFound))?;
|
|
|
|
Ok(json_object(status::Ok, serde_json::to_string(&game).unwrap()))
|
|
}
|
|
|
|
#[derive(Debug,Clone,Deserialize)]
|
|
struct GameList {
|
|
number: u32,
|
|
}
|
|
|
|
fn game_list(req: &mut Request) -> IronResult<Response> {
|
|
let state = req.get::<Read<State>>().unwrap();
|
|
let params = match req.get::<bodyparser::Struct<GameList>>() {
|
|
Ok(Some(b)) => b,
|
|
_ => return Err(MnmlHttpError::BadRequest.into()),
|
|
};
|
|
|
|
let db = state.pool.get().or(Err(MnmlHttpError::DbError))?;
|
|
|
|
let list = game::list(&db, params.number)
|
|
.or(Err(MnmlHttpError::ServerError))?;
|
|
|
|
Ok(json_object(status::Ok, serde_json::to_string(&list).unwrap()))
|
|
}
|
|
|
|
fn game_open(req: &mut Request) -> IronResult<Response> {
|
|
let state = req.get::<Read<State>>().unwrap();
|
|
let db = state.pool.get().or(Err(MnmlHttpError::DbError))?;
|
|
let mut tx = db.transaction().or(Err(MnmlHttpError::DbError))?;
|
|
|
|
let list = game::games_need_upkeep(&mut tx)
|
|
.or(Err(MnmlHttpError::ServerError))?;
|
|
|
|
tx.commit()
|
|
.or(Err(MnmlHttpError::ServerError))?;
|
|
|
|
Ok(json_object(status::Ok, serde_json::to_string(&list).unwrap()))
|
|
}
|
|
|
|
pub fn acp_mount() -> Chain {
|
|
let mut router = Router::new();
|
|
router.post("user", acp_user, "acp_user");
|
|
router.post("game", acp_game, "acp_game");
|
|
router.post("game/list", game_list, "acp_game_list");
|
|
router.post("game/open", game_open, "acp_game_open");
|
|
|
|
let mut chain = Chain::new(router);
|
|
chain.link_before(AcpMiddleware);
|
|
chain
|
|
}
|