file upload

This commit is contained in:
2021-12-22 13:10:08 +01:00
parent 8eeb2a8de7
commit 8cee6579e2
14 changed files with 126 additions and 68 deletions

View File

@@ -14,13 +14,6 @@ async fn main() -> std::io::Result<()> {
dotenv().ok();
return HttpServer::new(|| {
App::new()
// .configure(|cfg: &mut web::ServiceConfig| {
// let limit_string = std::env::var("SIZE_LIMIT").unwrap_or("0.1 KiB".to_string());
// let limit = Byte::from_str(limit_string.clone()).unwrap().get_bytes() as usize;
// println!("SIZE_LIMIT: {}, {}", limit_string, limit);
// let config = web::JsonConfig::default().limit(limit);
// cfg.data(config);
// })
.wrap(middleware::Compress::default())
.wrap(middleware::DefaultHeaders::default())
.configure(size::init)

View File

@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::time::SystemTime;
use crate::note::{generate_id, Note, NoteInfo, NotePublic};
use crate::size::LIMIT;
use crate::store;
fn now() -> u64 {
@@ -17,7 +18,7 @@ struct NotePath {
id: String,
}
#[get("/notes/{id}")]
#[get("/{id}")]
async fn one(path: web::Path<NotePath>) -> impl Responder {
let p = path.into_inner();
let note = store::get(&p.id);
@@ -32,7 +33,7 @@ struct CreateResponse {
id: String,
}
#[post("/notes")]
#[post("/")]
async fn create(note: web::Json<Note>) -> impl Responder {
let mut n = note.into_inner();
let id = generate_id();
@@ -62,7 +63,7 @@ async fn create(note: web::Json<Note>) -> impl Responder {
return HttpResponse::Ok().json(CreateResponse { id: id });
}
#[delete("/notes/{id}")]
#[delete("/{id}")]
async fn delete(path: web::Path<NotePath>) -> impl Responder {
let p = path.into_inner();
let note = store::get(&p.id);
@@ -102,11 +103,27 @@ async fn delete(path: web::Path<NotePath>) -> impl Responder {
}
}
#[derive(Serialize, Deserialize)]
struct Status {
max_size: usize,
}
#[get("/status")]
async fn status() -> impl Responder {
println!("Limit: {}", *LIMIT);
return HttpResponse::Ok().json(Status { max_size: *LIMIT });
}
pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/api")
.service(create)
.service(delete)
.service(one),
.service(
web::scope("/notes")
.service(one)
.service(create)
.service(delete)
.service(status),
)
.service(status),
);
}

View File

@@ -1,9 +1,20 @@
use actix_web::web;
use byte_unit::Byte;
use mime;
lazy_static! {
pub static ref LIMIT: usize =
Byte::from_str(std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string()))
.unwrap()
.get_bytes() as usize;
}
pub fn init(cfg: &mut web::ServiceConfig) {
let limit_string = std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string());
let limit = Byte::from_str(limit_string).unwrap().get_bytes() as usize;
let config = web::JsonConfig::default().limit(limit);
cfg.data(config);
println!("Limit: {}", *LIMIT);
let json = web::JsonConfig::default().limit(*LIMIT);
let plain = web::PayloadConfig::default()
.limit(*LIMIT)
.mimetype(mime::STAR_STAR);
cfg.data(json);
cfg.data(plain);
}