Skip to content

Commit

Permalink
chore(room-api): Update rust and deps, related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
christianfosli committed Nov 9, 2024
1 parent 20e19e5 commit 8e60579
Show file tree
Hide file tree
Showing 8 changed files with 766 additions and 488 deletions.
1,212 changes: 748 additions & 464 deletions room-api/Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions room-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.6"
axum = "0.7"
bounded-integer = { version = "0.5", features = ["std", "types", "serde1"] }
futures = "0.3"
mongodb = { version = "2" }
once_cell = "1"
mongodb = { version = "3" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
tower-http = { version = "0.4", features = ["cors"] }
tower-http = { version = "0.6", features = ["cors"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
2 changes: 1 addition & 1 deletion room-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.75 AS builder
FROM rust:1.82 AS builder

WORKDIR /usr/src/room-api
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion room-api/src/create_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn create_room(
ratings: None,
};

collection.insert_one(&created, None).await.map_err(|e| {
collection.insert_one(&created).await.map_err(|e| {
tracing::error!(err = e.to_string(), "Error persisting room to db");
(
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
2 changes: 1 addition & 1 deletion room-api/src/delete_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn delete_room(
let collection = db.collection::<ChangingRoom>("rooms");

collection
.delete_one(doc! { "id": id }, None)
.delete_one(doc! { "id": id })
.await
.map_err(|e| {
tracing::error!(err = e.to_string(), "Error deleting changing room");
Expand Down
18 changes: 8 additions & 10 deletions room-api/src/get_rooms.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::LazyLock;

use axum::{
extract::{Path, State},
http::StatusCode,
Expand All @@ -8,11 +10,10 @@ use mongodb::{
bson::{doc, Uuid},
Database,
};
use once_cell::sync::Lazy;

use crate::models::ChangingRoom;

static GENERIC_DB_ERROR: Lazy<(StatusCode, String)> = Lazy::new(|| {
static GENERIC_DB_ERROR: LazyLock<(StatusCode, String)> = LazyLock::new(|| {
(
StatusCode::INTERNAL_SERVER_ERROR,
String::from("An unexpected error occured getting data from database"),
Expand All @@ -25,7 +26,7 @@ pub async fn get_all_rooms(
let collection = db.collection::<ChangingRoom>("rooms");

let rooms = collection
.find(None, None)
.find(doc! {})
.await
.map_err(|e| {
tracing::error!(err = e.to_string(), "Unable to get cursor for all rooms");
Expand Down Expand Up @@ -54,13 +55,10 @@ pub async fn get_room_by_id(
})?;

let collection = db.collection::<ChangingRoom>("rooms");
let result = collection
.find_one(doc! { "id": id }, None)
.await
.map_err(|e| {
tracing::error!(err = e.to_string(), "Unable to get room by id from db");
GENERIC_DB_ERROR.clone()
})?;
let result = collection.find_one(doc! { "id": id }).await.map_err(|e| {
tracing::error!(err = e.to_string(), "Unable to get room by id from db");
GENERIC_DB_ERROR.clone()
})?;

match result {
Some(room) => Ok(Json(room)),
Expand Down
7 changes: 4 additions & 3 deletions room-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::env;
use std::net::SocketAddr;

use axum::http::{self, Method};
use axum::Server;
use axum::{routing, Router};
use mongodb::options::ClientOptions;
use mongodb::{Client, Database};
use tokio::net::TcpListener;
use tower_http::cors::CorsLayer;

use crate::create_room::create_room;
Expand Down Expand Up @@ -49,9 +49,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_state(db);

let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::info!("Service started. Listening on {}", addr);
let listener = TcpListener::bind(addr).await?;
tracing::info!("Service started. Listening on {addr}");

Server::bind(&addr).serve(app.into_make_service()).await?;
axum::serve(listener, app).await?;

Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions room-api/src/update_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use axum::{
};
use mongodb::{
bson::{doc, Uuid},
options::{FindOneAndReplaceOptions, ReturnDocument},
Database,
};
use serde::Deserialize;
Expand Down Expand Up @@ -43,9 +42,6 @@ pub async fn update_room(
location: payload.location,
ratings: payload.ratings,
},
FindOneAndReplaceOptions::builder()
.return_document(Some(ReturnDocument::After))
.build(),
)
.await
.map_err(|e| {
Expand Down

0 comments on commit 8e60579

Please sign in to comment.