diff options
Diffstat (limited to 'planetwars-server')
-rw-r--r-- | planetwars-server/src/db/sessions.rs | 9 | ||||
-rw-r--r-- | planetwars-server/src/db/users.rs | 12 | ||||
-rw-r--r-- | planetwars-server/src/lib.rs | 8 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs | 4 | ||||
-rw-r--r-- | planetwars-server/src/routes/demo.rs | 4 | ||||
-rw-r--r-- | planetwars-server/src/routes/matches.rs | 5 |
6 files changed, 19 insertions, 23 deletions
diff --git a/planetwars-server/src/db/sessions.rs b/planetwars-server/src/db/sessions.rs index 96f3926..a91d954 100644 --- a/planetwars-server/src/db/sessions.rs +++ b/planetwars-server/src/db/sessions.rs @@ -24,12 +24,11 @@ pub fn create_session(user: &User, conn: &PgConnection) -> Session { token: gen_session_token(), user_id: user.id, }; - let session = insert_into(sessions::table) + + insert_into(sessions::table) .values(&new_session) .get_result::<Session>(conn) - .unwrap(); - - return session; + .unwrap() } pub fn find_user_by_session(token: &str, conn: &PgConnection) -> QueryResult<(Session, User)> { @@ -42,5 +41,5 @@ pub fn find_user_by_session(token: &str, conn: &PgConnection) -> QueryResult<(Se pub fn gen_session_token() -> String { let mut rng = rand::thread_rng(); let token: [u8; 32] = rng.gen(); - return base64::encode(&token); + base64::encode(&token) } diff --git a/planetwars-server/src/db/users.rs b/planetwars-server/src/db/users.rs index a97ade5..3c071de 100644 --- a/planetwars-server/src/db/users.rs +++ b/planetwars-server/src/db/users.rs @@ -48,7 +48,7 @@ pub fn create_user(credentials: &Credentials, conn: &PgConnection) -> QueryResul let salt: [u8; 32] = rand::thread_rng().gen(); let hash = argon2::hash_raw(credentials.password.as_bytes(), &salt, &argon_config).unwrap(); let new_user = NewUser { - username: &credentials.username, + username: credentials.username, password_salt: &salt, password_hash: &hash, }; @@ -73,9 +73,9 @@ pub fn authenticate_user(credentials: &Credentials, db_conn: &PgConnection) -> O .unwrap(); if password_matches { - return Some(user); + Some(user) } else { - return None; + None } }) } @@ -91,15 +91,15 @@ fn test_argon() { let salt: [u8; 32] = rand::thread_rng().gen(); let hash = argon2::hash_raw(credentials.password.as_bytes(), &salt, &argon_config).unwrap(); let new_user = NewUser { - username: &credentials.username, + username: credentials.username, password_hash: &hash, password_salt: &salt, }; let password_matches = argon2::verify_raw( credentials.password.as_bytes(), - &new_user.password_salt, - &new_user.password_hash, + new_user.password_salt, + new_user.password_hash, &argon2_config(), ) .unwrap(); diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 6cc2824..51d6613 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -10,7 +10,6 @@ pub mod util; use std::ops::Deref; -use axum; use bb8::{Pool, PooledConnection}; use bb8_diesel::{self, DieselConnectionManager}; use diesel::{Connection, PgConnection}; @@ -60,13 +59,13 @@ pub async fn prepare_db(database_url: &str) -> Pool<DieselConnectionManager<PgCo let manager = DieselConnectionManager::<PgConnection>::new(database_url); let pool = bb8::Pool::builder().build(manager).await.unwrap(); seed_simplebot(&pool).await; - return pool; + pool } pub async fn api(configuration: Configuration) -> Router { let db_pool = prepare_db(&configuration.database_url).await; - let api = Router::new() + Router::new() .route("/register", post(routes::users::register)) .route("/login", post(routes::users::login)) .route("/users/me", get(routes::users::current_user)) @@ -91,8 +90,7 @@ pub async fn api(configuration: Configuration) -> Router { ) .route("/submit_bot", post(routes::demo::submit_bot)) .route("/save_bot", post(routes::bots::save_bot)) - .layer(AddExtensionLayer::new(db_pool)); - api + .layer(AddExtensionLayer::new(db_pool)) } pub async fn app() -> Router { diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index c9bf8ce..f7f99cb 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -59,8 +59,8 @@ pub async fn save_bot( owner_id: None, name: ¶ms.bot_name, }; - let bot = bots::create_bot(&new_bot, &conn).expect("could not create bot"); - bot + + bots::create_bot(&new_bot, &conn).expect("could not create bot") } }; let _code_bundle = diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 749c0ca..eb7f61f 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -13,8 +13,8 @@ use std::path::PathBuf; use super::matches::ApiMatch; -const PYTHON_IMAGE: &'static str = "python:3.10-slim-buster"; -const OPPONENT_NAME: &'static str = "simplebot"; +const PYTHON_IMAGE: &str = "python:3.10-slim-buster"; +const OPPONENT_NAME: &str = "simplebot"; #[derive(Serialize, Deserialize, Debug)] pub struct SubmitBotParams { diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 9fa532e..991a4b5 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -57,8 +57,7 @@ pub async fn play_match( code_path: PathBuf::from(BOTS_DIR).join(code_bundle.path), image: "python:3.10-slim-buster".to_string(), argv: shlex::split(&bot_config.run_command) - // TODO: this is an user error, should ideally be handled before we get here - .ok_or_else(|| StatusCode::INTERNAL_SERVER_ERROR)?, + .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?, }), }); @@ -152,7 +151,7 @@ pub async fn get_match_data( ) -> Result<Json<ApiMatch>, StatusCode> { let match_data = matches::find_match(match_id, &conn) .map_err(|_| StatusCode::NOT_FOUND) - .map(|data| match_data_to_api(data))?; + .map(match_data_to_api)?; Ok(Json(match_data)) } |