From 951cb293111db9ea0947cd65872da744bce92d31 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 20 Jun 2022 22:01:26 +0200 Subject: upgrade to axum 0.5 --- planetwars-server/Cargo.toml | 2 +- planetwars-server/src/db/matches.rs | 9 ++++++--- planetwars-server/src/lib.rs | 12 ++++++++---- planetwars-server/src/routes/bots.rs | 2 +- planetwars-server/src/routes/users.rs | 6 +++--- 5 files changed, 19 insertions(+), 12 deletions(-) (limited to 'planetwars-server') diff --git a/planetwars-server/Cargo.toml b/planetwars-server/Cargo.toml index d85a011..99161bb 100644 --- a/planetwars-server/Cargo.toml +++ b/planetwars-server/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" futures = "0.3" tokio = { version = "1.15", features = ["full"] } hyper = "0.14" -axum = { version = "0.4", features = ["json", "headers", "multipart"] } +axum = { version = "0.5", features = ["json", "headers", "multipart"] } diesel = { version = "1.4.4", features = ["postgres", "chrono"] } diesel-derive-enum = { version = "1.1", features = ["postgres"] } bb8 = "0.7" diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index ee25e85..dfff3cf 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -160,14 +160,17 @@ pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult { } pub enum MatchResult { - Finished { winner: Option } + Finished { winner: Option }, } pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> { let MatchResult::Finished { winner } = result; diesel::update(matches::table.find(id)) - .set((matches::winner.eq(winner), matches::state.eq(MatchState::Finished))) + .set(( + matches::winner.eq(winner), + matches::state.eq(MatchState::Finished), + )) .execute(conn)?; Ok(()) -} \ No newline at end of file +} diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index e50003c..9c9a03c 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -24,7 +24,7 @@ use axum::{ extract::{Extension, FromRequest, RequestParts}, http::StatusCode, routing::{get, post}, - AddExtensionLayer, Router, + Router, }; // TODO: make these configurable @@ -105,12 +105,16 @@ pub fn get_config() -> Result { .try_deserialize() } -async fn run_registry(_db_pool: DbPool) { +async fn run_registry(db_pool: DbPool) { // TODO: put in config let addr = SocketAddr::from(([127, 0, 0, 1], 9001)); axum::Server::bind(&addr) - .serve(registry_service().into_make_service()) + .serve( + registry_service() + .layer(Extension(db_pool)) + .into_make_service(), + ) .await .unwrap(); } @@ -124,7 +128,7 @@ pub async fn run_app() { let api_service = Router::new() .nest("/api", api()) - .layer(AddExtensionLayer::new(db_pool)) + .layer(Extension(db_pool)) .into_make_service(); // TODO: put in config diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 3bbaa1a..df0c4d0 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; use thiserror; use crate::db::bots::{self, CodeBundle}; -use crate::db::ratings::{RankedBot, self}; +use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_bundle; use crate::{DatabaseConnection, BOTS_DIR}; diff --git a/planetwars-server/src/routes/users.rs b/planetwars-server/src/routes/users.rs index 54ddd09..1989904 100644 --- a/planetwars-server/src/routes/users.rs +++ b/planetwars-server/src/routes/users.rs @@ -5,7 +5,7 @@ use axum::extract::{FromRequest, RequestParts, TypedHeader}; use axum::headers::authorization::Bearer; use axum::headers::Authorization; use axum::http::StatusCode; -use axum::response::{Headers, IntoResponse, Response}; +use axum::response::{IntoResponse, Response}; use axum::{async_trait, Json}; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -163,9 +163,9 @@ pub async fn login(conn: DatabaseConnection, params: Json) -> Respo Some(user) => { let session = sessions::create_session(&user, &conn); let user_data: UserData = user.into(); - let headers = Headers(vec![("Token", &session.token)]); + let headers = [("Token", &session.token)]; - (headers, Json(user_data)).into_response() + (StatusCode::OK, headers, Json(user_data)).into_response() } } } -- cgit v1.2.3