aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/routes
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-10-12 22:52:15 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-10-12 22:52:15 +0200
commitae57359353cf31ff374a8932999742920878bf00 (patch)
tree0db27d394a2a61a5cc94e73014c82954829c1338 /planetwars-server/src/routes
parented016773b112460ebbf0ff023b0915545229ed41 (diff)
downloadplanetwars.dev-ae57359353cf31ff374a8932999742920878bf00.tar.xz
planetwars.dev-ae57359353cf31ff374a8932999742920878bf00.zip
upgrade to diesel 2.0
Diffstat (limited to 'planetwars-server/src/routes')
-rw-r--r--planetwars-server/src/routes/bots.rs56
-rw-r--r--planetwars-server/src/routes/demo.rs9
-rw-r--r--planetwars-server/src/routes/maps.rs4
-rw-r--r--planetwars-server/src/routes/matches.rs16
-rw-r--r--planetwars-server/src/routes/users.rs20
5 files changed, 54 insertions, 51 deletions
diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs
index f8087fd..f0ff9bf 100644
--- a/planetwars-server/src/routes/bots.rs
+++ b/planetwars-server/src/routes/bots.rs
@@ -100,10 +100,10 @@ pub fn validate_bot_name(bot_name: &str) -> Result<(), SaveBotError> {
pub async fn save_bot(
Json(params): Json<SaveBotParams>,
user: User,
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Json<Bot>, SaveBotError> {
- let res = bots::find_bot_by_name(&params.bot_name, &conn)
+ let res = bots::find_bot_by_name(&params.bot_name, &mut conn)
.optional()
.expect("could not run query");
@@ -122,10 +122,10 @@ pub async fn save_bot(
name: &params.bot_name,
};
- bots::create_bot(&new_bot, &conn).expect("could not create bot")
+ bots::create_bot(&new_bot, &mut conn).expect("could not create bot")
}
};
- let _code_bundle = save_code_string(&params.code, Some(bot.id), &conn, &config)
+ let _code_bundle = save_code_string(&params.code, Some(bot.id), &mut conn, &config)
.expect("failed to save code bundle");
Ok(Json(bot))
}
@@ -137,12 +137,12 @@ pub struct BotParams {
// TODO: can we unify this with save_bot?
pub async fn create_bot(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
user: User,
params: Json<BotParams>,
) -> Result<(StatusCode, Json<Bot>), SaveBotError> {
validate_bot_name(&params.name)?;
- let existing_bot = bots::find_bot_by_name(&params.name, &conn)
+ let existing_bot = bots::find_bot_by_name(&params.name, &mut conn)
.optional()
.expect("could not run query");
if existing_bot.is_some() {
@@ -152,26 +152,27 @@ pub async fn create_bot(
owner_id: Some(user.id),
name: &params.name,
};
- let bot = bots::create_bot(&bot_params, &conn).unwrap();
+ let bot = bots::create_bot(&bot_params, &mut conn).unwrap();
Ok((StatusCode::CREATED, Json(bot)))
}
// TODO: handle errors
pub async fn get_bot(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
Path(bot_name): Path<String>,
) -> Result<Json<JsonValue>, StatusCode> {
- let bot = db::bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ let bot =
+ db::bots::find_bot_by_name(&bot_name, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
let owner: Option<UserData> = match bot.owner_id {
Some(user_id) => {
- let user = db::users::find_user(user_id, &conn)
+ let user = db::users::find_user(user_id, &mut conn)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Some(user.into())
}
None => None,
};
- let versions =
- bots::find_bot_versions(bot.id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+ let versions = bots::find_bot_versions(bot.id, &mut conn)
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(json!({
"bot": bot,
"owner": owner,
@@ -180,32 +181,32 @@ pub async fn get_bot(
}
pub async fn get_user_bots(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
Path(user_name): Path<String>,
) -> Result<Json<Vec<Bot>>, StatusCode> {
let user =
- db::users::find_user_by_name(&user_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
- db::bots::find_bots_by_owner(user.id, &conn)
+ db::users::find_user_by_name(&user_name, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ db::bots::find_bots_by_owner(user.id, &mut conn)
.map(Json)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
/// List all active bots
-pub async fn list_bots(conn: DatabaseConnection) -> Result<Json<Vec<Bot>>, StatusCode> {
- bots::find_active_bots(&conn)
+pub async fn list_bots(mut conn: DatabaseConnection) -> Result<Json<Vec<Bot>>, StatusCode> {
+ bots::find_active_bots(&mut conn)
.map(Json)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
-pub async fn get_ranking(conn: DatabaseConnection) -> Result<Json<Vec<RankedBot>>, StatusCode> {
- ratings::get_bot_ranking(&conn)
+pub async fn get_ranking(mut conn: DatabaseConnection) -> Result<Json<Vec<RankedBot>>, StatusCode> {
+ ratings::get_bot_ranking(&mut conn)
.map(Json)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
// TODO: currently this only implements the happy flow
pub async fn upload_code_multipart(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
user: User,
Path(bot_name): Path<String>,
mut multipart: Multipart,
@@ -213,7 +214,7 @@ pub async fn upload_code_multipart(
) -> Result<Json<BotVersion>, StatusCode> {
let bots_dir = PathBuf::from(&config.bots_directory);
- let bot = bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ let bot = bots::find_bot_by_name(&bot_name, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
if Some(user.id) != bot.owner_id {
return Err(StatusCode::FORBIDDEN);
@@ -246,21 +247,22 @@ pub async fn upload_code_multipart(
container_digest: None,
};
let code_bundle =
- bots::create_bot_version(&bot_version, &conn).expect("Failed to create code bundle");
+ bots::create_bot_version(&bot_version, &mut conn).expect("Failed to create code bundle");
Ok(Json(code_bundle))
}
pub async fn get_code(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
user: User,
Path(bundle_id): Path<i32>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Vec<u8>, StatusCode> {
let version =
- db::bots::find_bot_version(bundle_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ db::bots::find_bot_version(bundle_id, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
let bot_id = version.bot_id.ok_or(StatusCode::FORBIDDEN)?;
- let bot = db::bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+ let bot =
+ db::bots::find_bot(bot_id, &mut conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if bot.owner_id != Some(user.id) {
return Err(StatusCode::FORBIDDEN);
@@ -297,10 +299,10 @@ impl MatchupStats {
type BotStats = HashMap<String, HashMap<String, MatchupStats>>;
pub async fn get_bot_stats(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
Path(bot_name): Path<String>,
) -> Result<Json<BotStats>, StatusCode> {
- let stats_records = db::matches::fetch_bot_stats(&bot_name, &conn)
+ let stats_records = db::matches::fetch_bot_stats(&bot_name, &mut conn)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let mut bot_stats: BotStats = HashMap::new();
for record in stats_records {
diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs
index 1ec8825..cd490ef 100644
--- a/planetwars-server/src/routes/demo.rs
+++ b/planetwars-server/src/routes/demo.rs
@@ -35,7 +35,7 @@ pub async fn submit_bot(
Extension(pool): Extension<ConnectionPool>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Json<SubmitBotResponse>, StatusCode> {
- let conn = pool.get().await.expect("could not get database connection");
+ let mut conn = pool.get().await.expect("could not get database connection");
let opponent_name = params
.opponent_name
@@ -46,12 +46,13 @@ pub async fn submit_bot(
.unwrap_or_else(|| DEFAULT_MAP_NAME.to_string());
let (opponent_bot, opponent_bot_version) =
- db::bots::find_bot_with_version_by_name(&opponent_name, &conn)
+ db::bots::find_bot_with_version_by_name(&opponent_name, &mut conn)
.map_err(|_| StatusCode::BAD_REQUEST)?;
- let map = db::maps::find_map_by_name(&map_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?;
+ let map =
+ db::maps::find_map_by_name(&map_name, &mut conn).map_err(|_| StatusCode::BAD_REQUEST)?;
- let player_bot_version = save_code_string(&params.code, None, &conn, &config)
+ let player_bot_version = save_code_string(&params.code, None, &mut conn, &config)
// TODO: can we recover from this?
.expect("could not save bot code");
diff --git a/planetwars-server/src/routes/maps.rs b/planetwars-server/src/routes/maps.rs
index 689b11e..188089f 100644
--- a/planetwars-server/src/routes/maps.rs
+++ b/planetwars-server/src/routes/maps.rs
@@ -8,8 +8,8 @@ pub struct ApiMap {
pub name: String,
}
-pub async fn list_maps(conn: DatabaseConnection) -> Result<Json<Vec<ApiMap>>, StatusCode> {
- let maps = db::maps::list_maps(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+pub async fn list_maps(mut conn: DatabaseConnection) -> Result<Json<Vec<ApiMap>>, StatusCode> {
+ let maps = db::maps::list_maps(&mut conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let api_maps = maps
.into_iter()
diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs
index 1d7403c..3ad10cf 100644
--- a/planetwars-server/src/routes/matches.rs
+++ b/planetwars-server/src/routes/matches.rs
@@ -56,7 +56,7 @@ pub struct ListMatchesResponse {
pub async fn list_recent_matches(
Query(params): Query<ListRecentMatchesParams>,
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
) -> Result<Json<ListMatchesResponse>, StatusCode> {
let requested_count = std::cmp::min(
params.count.unwrap_or(DEFAULT_NUM_RETURNED_MATCHES),
@@ -68,7 +68,7 @@ pub async fn list_recent_matches(
let matches_result = match params.bot {
Some(bot_name) => {
- let bot = db::bots::find_bot_by_name(&bot_name, &conn)
+ let bot = db::bots::find_bot_by_name(&bot_name, &mut conn)
.map_err(|_| StatusCode::BAD_REQUEST)?;
matches::list_bot_matches(
bot.id,
@@ -76,10 +76,10 @@ pub async fn list_recent_matches(
count,
params.before,
params.after,
- &conn,
+ &mut conn,
)
}
- None => matches::list_public_matches(count, params.before, params.after, &conn),
+ None => matches::list_public_matches(count, params.before, params.after, &mut conn),
};
let mut matches = matches_result.map_err(|_| StatusCode::BAD_REQUEST)?;
@@ -119,9 +119,9 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
pub async fn get_match_data(
Path(match_id): Path<i32>,
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
) -> Result<Json<ApiMatch>, StatusCode> {
- let match_data = matches::find_match(match_id, &conn)
+ let match_data = matches::find_match(match_id, &mut conn)
.map_err(|_| StatusCode::NOT_FOUND)
.map(match_data_to_api)?;
Ok(Json(match_data))
@@ -129,11 +129,11 @@ pub async fn get_match_data(
pub async fn get_match_log(
Path(match_id): Path<i32>,
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Vec<u8>, StatusCode> {
let match_base =
- matches::find_match_base(match_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
+ matches::find_match_base(match_id, &mut conn).map_err(|_| StatusCode::NOT_FOUND)?;
let log_path = PathBuf::from(&config.match_logs_directory).join(&match_base.log_path);
let log_contents = std::fs::read(log_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(log_contents)
diff --git a/planetwars-server/src/routes/users.rs b/planetwars-server/src/routes/users.rs
index 264e5b9..d072d0a 100644
--- a/planetwars-server/src/routes/users.rs
+++ b/planetwars-server/src/routes/users.rs
@@ -23,13 +23,13 @@ where
type Rejection = (StatusCode, String);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
- let conn = DatabaseConnection::from_request(req).await?;
+ let mut conn = DatabaseConnection::from_request(req).await?;
let TypedHeader(Authorization(bearer)) = AuthorizationHeader::from_request(req)
.await
.map_err(|_| (StatusCode::UNAUTHORIZED, "".to_string()))?;
- let (_session, user) = sessions::find_user_by_session(bearer.token(), &conn)
+ let (_session, user) = sessions::find_user_by_session(bearer.token(), &mut conn)
.map_err(|_| (StatusCode::UNAUTHORIZED, "".to_string()))?;
Ok(user)
@@ -66,7 +66,7 @@ pub enum RegistrationError {
}
impl RegistrationParams {
- fn validate(&self, conn: &DatabaseConnection) -> Result<(), RegistrationError> {
+ fn validate(&self, conn: &mut DatabaseConnection) -> Result<(), RegistrationError> {
let mut errors = Vec::new();
// TODO: do we want to support cased usernames?
@@ -95,7 +95,7 @@ impl RegistrationParams {
errors.push("that username is not allowed".to_string());
}
- if users::find_user_by_name(&self.username, &conn).is_ok() {
+ if users::find_user_by_name(&self.username, conn).is_ok() {
errors.push("username is already taken".to_string());
}
@@ -137,16 +137,16 @@ impl IntoResponse for RegistrationError {
}
pub async fn register(
- conn: DatabaseConnection,
+ mut conn: DatabaseConnection,
params: Json<RegistrationParams>,
) -> Result<Json<UserData>, RegistrationError> {
- params.validate(&conn)?;
+ params.validate(&mut conn)?;
let credentials = Credentials {
username: &params.username,
password: &params.password,
};
- let user = users::create_user(&credentials, &conn)?;
+ let user = users::create_user(&credentials, &mut conn)?;
Ok(Json(user.into()))
}
@@ -156,18 +156,18 @@ pub struct LoginParams {
pub password: String,
}
-pub async fn login(conn: DatabaseConnection, params: Json<LoginParams>) -> Response {
+pub async fn login(mut conn: DatabaseConnection, params: Json<LoginParams>) -> Response {
let credentials = Credentials {
username: &params.username,
password: &params.password,
};
// TODO: handle failures
- let authenticated = users::authenticate_user(&credentials, &conn);
+ let authenticated = users::authenticate_user(&credentials, &mut conn);
match authenticated {
None => StatusCode::FORBIDDEN.into_response(),
Some(user) => {
- let session = sessions::create_session(&user, &conn);
+ let session = sessions::create_session(&user, &mut conn);
let user_data: UserData = user.into();
let headers = [("Token", &session.token)];