Browse Source

Initial upload support, use RwLock

Amber 1 year ago
parent
commit
07e944394b
  1. 12
      src/down.rs
  2. 21
      src/main.rs
  3. 41
      src/up.rs

12
src/down.rs

@ -8,8 +8,8 @@ use axum::{ @@ -8,8 +8,8 @@ use axum::{
use crate::{AppState, Error};
pub(crate) fn routes(router: Router<AppState>) -> Router<AppState> {
router
pub(crate) fn routes() -> Router<AppState> {
Router::new()
.route("/t/:id", get(get_text_page))
.route("/t/:id/raw", get(get_text_raw))
.route("/f/:id", get(get_file_page))
@ -23,14 +23,14 @@ pub(crate) fn routes(router: Router<AppState>) -> Router<AppState> { @@ -23,14 +23,14 @@ pub(crate) fn routes(router: Router<AppState>) -> Router<AppState> {
}
async fn get_redirect(State(state): State<AppState>, Path(id): Path<String>) -> impl IntoResponse {
match state.cache.get_redirect(id) {
match state.cache.read().unwrap().get_redirect(id) {
Ok(target) => (StatusCode::FOUND, [(header::LOCATION, target)]).into_response(),
Err(e) => e.into_response(),
}
}
async fn get_text_raw(State(state): State<AppState>, Path(id): Path<String>) -> impl IntoResponse {
match state.cache.get_text(id) {
match state.cache.read().unwrap().get_text(id) {
Ok(text) => text.text.into_response(),
Err(e) => e.into_response(),
}
@ -40,7 +40,7 @@ async fn get_text_page( @@ -40,7 +40,7 @@ async fn get_text_page(
State(state): State<AppState>,
Path(id): Path<String>,
) -> Result<Html<String>, Error> {
let text = state.cache.get_text(id)?;
let text = state.cache.read().unwrap().get_text(id)?;
let mut ctx = tera::Context::new();
@ -53,7 +53,7 @@ async fn get_file_page( @@ -53,7 +53,7 @@ async fn get_file_page(
State(state): State<AppState>,
Path(id): Path<String>,
) -> Result<Html<String>, Error> {
let file_meta = state.cache.get_file_meta(id)?;
let file_meta = state.cache.read().unwrap().get_file_meta(id)?;
let mut ctx = tera::Context::new();

21
src/main.rs

@ -2,7 +2,8 @@ @@ -2,7 +2,8 @@
mod data;
mod down;
//mod up;
mod up;
use std::sync::RwLock;
//mod user;
//mod admin;
@ -36,7 +37,7 @@ impl IntoResponse for Error { @@ -36,7 +37,7 @@ impl IntoResponse for Error {
struct AppState {
alphabet: Vec<char>,
id_length: usize,
cache: Arc<DataStorage>,
cache: Arc<RwLock<DataStorage>>,
tera: Tera,
}
@ -65,7 +66,7 @@ impl AppState { @@ -65,7 +66,7 @@ impl AppState {
Ok(Self {
alphabet,
id_length,
cache: Arc::new(cache),
cache: Arc::new(RwLock::new(cache)),
tera,
})
}
@ -73,18 +74,20 @@ impl AppState { @@ -73,18 +74,20 @@ impl AppState {
fn routes() -> Result<Router, Error> {
let state = AppState::new()?;
// let up = up::routes();
// let down = down::routes();
let up = up::routes();
let down = down::routes();
// let user = user::routes();
// let admin = admin::routes();
let router = Router::new().route("/extrac", get(test_extrac));
// let router = Router::new().route("/extrac", get(test_extrac));
// .nest("/up", up)
// .merge(down)
// .nest("/up", up)
// .nest("/user", user)
// .nest("/admin", admin)
let meow = down::routes(router);
//let meow = down::routes();
//let meow2 = up::routes(router);
let router: Router<AppState> = Router::new().nest("/down", down).nest("/up", up);
Ok(meow.with_state(state))
Ok(router.with_state(state))
}
async fn test_extrac(State(state): State<AppState>) -> String {

41
src/up.rs

@ -1,11 +1,34 @@ @@ -1,11 +1,34 @@
use axum::{routing::{post, get}, Router};
use axum::{
extract::{Path, State},
response::{Html, IntoResponse},
routing::{get, post},
Router,
};
pub(crate) fn routes() -> Router {
Router::new()
.route("/text", post(todo!()))
.route("/file", post(todo!()))
.route("/link", post(todo!()))
.route("/text/:id", get(todo!()).post(todo!()))
.route("/file/:id", get(todo!()).post(todo!()))
.route("/link/:id", get(todo!()).post(todo!()))
use crate::{data::TextMeta, AppState, Error};
pub(crate) fn routes() -> Router<AppState> {
Router::new().route("/text", post(post_text))
/*Router::new()
.route("/text", post(todo!()))
.route("/file", post(todo!()))
.route("/link", post(todo!()))
.route("/text/:id", get(todo!()).post(todo!()))
.route("/file/:id", get(todo!()).post(todo!()))
.route("/link/:id", get(todo!()).post(todo!()))*/
}
async fn post_text(State(state): State<AppState>, body: String) -> impl IntoResponse {
println!("post {}", body);
state
.cache
.write()
.unwrap()
.create_text(
"yea".to_string(),
"meoww :3".to_string(),
TextMeta::parse("language: catperson").unwrap(),
)
.unwrap();
return "a".into_response();
}

Loading…
Cancel
Save