Compare commits

..

No commits in common. "63c16a797be4881516c87063992c780b2c859967" and "b22c3122d7783c89693a5f361f72922702c4d55c" have entirely different histories.

4 changed files with 227 additions and 464 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +0,0 @@
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct SharedState {
pub locks: LockMap,
}
pub type LockMap = Arc<Mutex<HashMap<String, Arc<Mutex<()>>>>>;

View File

@ -1,13 +1,9 @@
use std::{collections::HashMap, sync::Arc};
use axum::{ use axum::{
extract::{DefaultBodyLimit, Request}, extract::{DefaultBodyLimit, Request},
routing::{delete, get, post}, routing::{delete, get, post},
Router, ServiceExt, Router, ServiceExt,
}; };
use dotenv::dotenv; use dotenv::dotenv;
use lock::SharedState;
use tokio::sync::Mutex;
use tower::Layer; use tower::Layer;
use tower_http::{ use tower_http::{
compression::CompressionLayer, compression::CompressionLayer,
@ -20,7 +16,6 @@ extern crate lazy_static;
mod config; mod config;
mod health; mod health;
mod lock;
mod note; mod note;
mod status; mod status;
mod store; mod store;
@ -29,13 +24,9 @@ mod store;
async fn main() { async fn main() {
dotenv().ok(); dotenv().ok();
let shared_state = SharedState {
locks: Arc::new(Mutex::new(HashMap::new())),
};
if !store::can_reach_redis() { if !store::can_reach_redis() {
println!("cannot reach redis"); println!("cannot reach redis");
panic!("cannot reach redis"); panic!("canont reach redis");
} }
let notes_routes = Router::new() let notes_routes = Router::new()
@ -62,8 +53,7 @@ async fn main() {
.deflate(true) .deflate(true)
.gzip(true) .gzip(true)
.zstd(true), .zstd(true),
) );
.with_state(shared_state);
let app = NormalizePathLayer::trim_trailing_slash().layer(app); let app = NormalizePathLayer::trim_trailing_slash().layer(app);

View File

@ -5,12 +5,11 @@ use axum::{
Json, Json,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{sync::Arc, time::SystemTime}; use std::time::SystemTime;
use tokio::sync::Mutex;
use crate::config;
use crate::note::{generate_id, Note, NoteInfo}; use crate::note::{generate_id, Note, NoteInfo};
use crate::store; use crate::store;
use crate::{config, lock::SharedState};
use super::NotePublic; use super::NotePublic;
@ -81,20 +80,11 @@ pub async fn create(Json(mut n): Json<Note>) -> Response {
} }
} }
pub async fn delete( pub async fn delete(Path(OneNoteParams { id }): Path<OneNoteParams>) -> Response {
Path(OneNoteParams { id }): Path<OneNoteParams>,
state: axum::extract::State<SharedState>,
) -> Response {
let mut locks_map = state.locks.lock().await;
let lock = locks_map
.entry(id.clone())
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone();
drop(locks_map);
let _guard = lock.lock().await;
let note = store::get(&id); let note = store::get(&id);
match note { match note {
// Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
// Ok(None) => return HttpResponse::NotFound().finish(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
Ok(None) => (StatusCode::NOT_FOUND).into_response(), Ok(None) => (StatusCode::NOT_FOUND).into_response(),
Ok(Some(note)) => { Ok(Some(note)) => {