Browse Source

Initial upload support, use RwLock

Amber 2 years 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::{
use crate::{AppState, Error}; use crate::{AppState, Error};
pub(crate) fn routes(router: Router<AppState>) -> Router<AppState> { pub(crate) fn routes() -> Router<AppState> {
router Router::new()
.route("/t/:id", get(get_text_page)) .route("/t/:id", get(get_text_page))
.route("/t/:id/raw", get(get_text_raw)) .route("/t/:id/raw", get(get_text_raw))
.route("/f/:id", get(get_file_page)) .route("/f/:id", get(get_file_page))
@ -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 { 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(), Ok(target) => (StatusCode::FOUND, [(header::LOCATION, target)]).into_response(),
Err(e) => e.into_response(), Err(e) => e.into_response(),
} }
} }
async fn get_text_raw(State(state): State<AppState>, Path(id): Path<String>) -> impl IntoResponse { 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(), Ok(text) => text.text.into_response(),
Err(e) => e.into_response(), Err(e) => e.into_response(),
} }
@ -40,7 +40,7 @@ async fn get_text_page(
State(state): State<AppState>, State(state): State<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Html<String>, Error> { ) -> 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(); let mut ctx = tera::Context::new();
@ -53,7 +53,7 @@ async fn get_file_page(
State(state): State<AppState>, State(state): State<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> Result<Html<String>, Error> { ) -> 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(); let mut ctx = tera::Context::new();

21
src/main.rs

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

41
src/up.rs

@ -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 { use crate::{data::TextMeta, AppState, Error};
Router::new()
.route("/text", post(todo!())) pub(crate) fn routes() -> Router<AppState> {
.route("/file", post(todo!())) Router::new().route("/text", post(post_text))
.route("/link", post(todo!())) /*Router::new()
.route("/text/:id", get(todo!()).post(todo!())) .route("/text", post(todo!()))
.route("/file/:id", get(todo!()).post(todo!())) .route("/file", post(todo!()))
.route("/link/:id", get(todo!()).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