This commit is contained in:
2026-06-07 10:46:15 +02:00
parent bb422fdd8d
commit 781231e414
21 changed files with 443 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
mod model;
mod routes;
pub use model::*;
pub use routes::*;
+41
View File
@@ -0,0 +1,41 @@
use bs62;
use ring::rand::SecureRandom;
use serde::{Deserialize, Serialize};
use crate::config;
#[derive(Serialize, Deserialize, Clone)]
pub struct NoteV2Metadata {
pub views: Option<u32>,
pub expiration: Option<u32>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct NoteV2 {
pub metadata: NoteV2Metadata,
pub public: String,
pub content: String,
}
#[derive(Serialize)]
pub struct NoteInfoV2 {
pub public: String,
}
#[derive(Serialize)]
pub struct NotePublicV2 {
pub public: String,
pub content: String,
}
pub fn generate_id_v2() -> String {
let mut result = "".to_owned();
let mut id: [u8; 1] = [0; 1];
let sr = ring::rand::SystemRandom::new();
for _ in 0..*config::ID_LENGTH {
let _ = sr.fill(&mut id);
result.push_str(&bs62::encode_data(&id));
}
return result;
}
+126
View File
@@ -0,0 +1,126 @@
use axum::{
Json,
body::Bytes,
extract::Path,
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use std::{sync::Arc, time::SystemTime};
use crate::note_v2::{NoteV2, generate_id_v2};
use crate::store;
use crate::{config, lock::SharedState};
use super::NotePublicV2;
pub fn now() -> u32 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as u32
}
// #[derive(Deserialize)]
// pub struct OneNoteParams {
// id: String,
// }
// pub async fn preview(Path(OneNoteParams { id }): Path<OneNoteParams>) -> Response {
// let note = store::get(&id);
// match note {
// Ok(Some(n)) => (StatusCode::OK, Json(NoteInfoV2 { meta: n.meta })).into_response(),
// Ok(None) => (StatusCode::NOT_FOUND).into_response(),
// Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
// }
// }
#[derive(Serialize, Deserialize)]
struct CreateResponse {
id: String,
}
pub async fn create(body: Bytes) -> Response {
let id = generate_id_v2();
println!("{}", body.len());
(StatusCode::OK, Json(CreateResponse { id })).into_response()
// match store::set(&id.clone(), &n.clone()) {
// Ok(_) => (StatusCode::OK, Json(CreateResponse { id })).into_response(),
// Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
// }
}
// pub async fn delete(
// 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);
// match note {
// Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
// Ok(None) => (StatusCode::NOT_FOUND).into_response(),
// Ok(Some(note)) => {
// let mut changed = note.clone();
// if changed.views == None && changed.expiration == None {
// return (StatusCode::BAD_REQUEST).into_response();
// }
// match changed.views {
// Some(v) => {
// changed.views = Some(v - 1);
// let id = id.clone();
// if v <= 1 {
// match store::del(&id) {
// Err(e) => {
// return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
// .into_response();
// }
// _ => {}
// }
// } else {
// match store::set(&id, &changed.clone()) {
// Err(e) => {
// return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
// .into_response();
// }
// _ => {}
// }
// }
// }
// _ => {}
// }
// let n = now();
// match changed.expiration {
// Some(e) => {
// if e < n {
// match store::del(&id.clone()) {
// Ok(_) => return (StatusCode::BAD_REQUEST).into_response(),
// Err(e) => {
// return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
// .into_response();
// }
// }
// }
// }
// _ => {}
// }
// return (
// StatusCode::OK,
// Json(NotePublicV2 {
// content: changed.contents,
// public: changed.meta,
// }),
// )
// .into_response();
// }
// }
// }