upgrade to axum 0.5

This commit is contained in:
Ilion Beyst 2022-06-20 22:01:26 +02:00
parent a2a8a41689
commit 951cb29311
5 changed files with 19 additions and 12 deletions

View file

@ -9,7 +9,7 @@ edition = "2021"
futures = "0.3" futures = "0.3"
tokio = { version = "1.15", features = ["full"] } tokio = { version = "1.15", features = ["full"] }
hyper = "0.14" 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 = { version = "1.4.4", features = ["postgres", "chrono"] }
diesel-derive-enum = { version = "1.1", features = ["postgres"] } diesel-derive-enum = { version = "1.1", features = ["postgres"] }
bb8 = "0.7" bb8 = "0.7"

View file

@ -160,14 +160,17 @@ pub fn find_match_base(id: i32, conn: &PgConnection) -> QueryResult<MatchBase> {
} }
pub enum MatchResult { pub enum MatchResult {
Finished { winner: Option<i32> } Finished { winner: Option<i32> },
} }
pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> { pub fn save_match_result(id: i32, result: MatchResult, conn: &PgConnection) -> QueryResult<()> {
let MatchResult::Finished { winner } = result; let MatchResult::Finished { winner } = result;
diesel::update(matches::table.find(id)) 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)?; .execute(conn)?;
Ok(()) Ok(())
} }

View file

@ -24,7 +24,7 @@ use axum::{
extract::{Extension, FromRequest, RequestParts}, extract::{Extension, FromRequest, RequestParts},
http::StatusCode, http::StatusCode,
routing::{get, post}, routing::{get, post},
AddExtensionLayer, Router, Router,
}; };
// TODO: make these configurable // TODO: make these configurable
@ -105,12 +105,16 @@ pub fn get_config() -> Result<Configuration, ConfigError> {
.try_deserialize() .try_deserialize()
} }
async fn run_registry(_db_pool: DbPool) { async fn run_registry(db_pool: DbPool) {
// TODO: put in config // TODO: put in config
let addr = SocketAddr::from(([127, 0, 0, 1], 9001)); let addr = SocketAddr::from(([127, 0, 0, 1], 9001));
axum::Server::bind(&addr) axum::Server::bind(&addr)
.serve(registry_service().into_make_service()) .serve(
registry_service()
.layer(Extension(db_pool))
.into_make_service(),
)
.await .await
.unwrap(); .unwrap();
} }
@ -124,7 +128,7 @@ pub async fn run_app() {
let api_service = Router::new() let api_service = Router::new()
.nest("/api", api()) .nest("/api", api())
.layer(AddExtensionLayer::new(db_pool)) .layer(Extension(db_pool))
.into_make_service(); .into_make_service();
// TODO: put in config // TODO: put in config

View file

@ -12,7 +12,7 @@ use std::path::PathBuf;
use thiserror; use thiserror;
use crate::db::bots::{self, CodeBundle}; 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::db::users::User;
use crate::modules::bots::save_code_bundle; use crate::modules::bots::save_code_bundle;
use crate::{DatabaseConnection, BOTS_DIR}; use crate::{DatabaseConnection, BOTS_DIR};

View file

@ -5,7 +5,7 @@ use axum::extract::{FromRequest, RequestParts, TypedHeader};
use axum::headers::authorization::Bearer; use axum::headers::authorization::Bearer;
use axum::headers::Authorization; use axum::headers::Authorization;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::{Headers, IntoResponse, Response}; use axum::response::{IntoResponse, Response};
use axum::{async_trait, Json}; use axum::{async_trait, Json};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
@ -163,9 +163,9 @@ pub async fn login(conn: DatabaseConnection, params: Json<LoginParams>) -> Respo
Some(user) => { Some(user) => {
let session = sessions::create_session(&user, &conn); let session = sessions::create_session(&user, &conn);
let user_data: UserData = user.into(); 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()
} }
} }
} }