Merge pull request #15 from cupcakearmy/size-limit

Size limit
This commit is contained in:
Nicco 2021-12-20 17:45:05 +01:00 committed by GitHub
commit 0c01866344
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 328 additions and 214 deletions

503
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -18,4 +18,6 @@ serde_json = "1"
lazy_static = "1"
ring = "0.16"
bs62 = "0.1"
memcache = "0.15"
memcache = "0.16"
byte-unit = "4"
mime = "0.3"

View File

@ -36,6 +36,13 @@ each note has a 512bit generated <i>id</i> that is used to retrieve the note. da
![screenshot](./design/Screens.png)
## Environment Variables
| Variable | Default | Description |
| ------------ | ----------------- | --------------------------------------------------------------------------------------- |
| `MEMCACHE` | `memcached:11211` | Memcached URL to connect to. |
| `SIZE_LIMIT` | `1 KiB` | Max size for body. Accepted values according to [byte-unit](https://docs.rs/byte-unit/) |
## Deployment
`https` is required otherwise browsers will not support the cryptographic functions.
@ -115,7 +122,7 @@ Running `npm run dev` in the root folder will start the following things
- rust backend with hot reload
- client with hot reload
You can see the app under [localhost:3000](http://localhost:3000).
You can see the app under [localhost:5000](http://localhost:5000).
###### Attributions

View File

@ -5,19 +5,28 @@ extern crate lazy_static;
mod client;
mod note;
mod size;
mod store;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
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)
.configure(note::init)
.configure(client::init)
.default_service(web::resource("").route(web::get().to(client::fallback_fn)))
})
.bind("0.0.0.0:5000")?
.run()
.await
.await;
}

View File

@ -37,9 +37,6 @@ async fn create(note: web::Json<Note>) -> impl Responder {
let mut n = note.into_inner();
let id = generate_id();
let bad_req = HttpResponse::BadRequest().finish();
if n.contents.chars().count() > 8192 {
return bad_req;
}
if n.views == None && n.expiration == None {
return bad_req;
}

10
src/size.rs Normal file
View File

@ -0,0 +1,10 @@
use actix_web::web;
use byte_unit::Byte;
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.clone()).unwrap().get_bytes() as usize;
println!("SIZE_LIMIT: {}, {}", limit_string, limit);
let config = web::JsonConfig::default().limit(limit);
cfg.data(config);
}