diff --git a/Cargo.lock b/Cargo.lock index 0045a8c..5586ee3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,6 +524,7 @@ dependencies = [ "dotenv", "lazy_static", "memcache", + "mime", "ring", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index c525408..339eb49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ bs62 = "0.1" memcache = "0.16" byte-unit = "4" dotenv = "0.15" +mime = "0.3" diff --git a/README.md b/README.md index 3517dda..657cb91 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ version: '3.7' services: memcached: image: memcached:1-alpine - entrypoint: memcached -m 128 # Limit to 128 MB Ram, customize at free will. + entrypoint: memcached -m 128M -I 4M # Limit to 128 MB Ram, 4M per entry, customize at free will. app: image: cupcakearmy/cryptgeon:latest diff --git a/client/src/app.css b/client/src/app.css index eeaff16..6630c5e 100644 --- a/client/src/app.css +++ b/client/src/app.css @@ -100,7 +100,7 @@ fieldset { .box { width: 100%; - min-height: min(calc(100vh - 30rem), 30rem); + min-height: min(calc(100vh - 30rem), 20rem); margin: 0; border: 2px solid var(--ui-bg-1); resize: vertical; @@ -118,3 +118,7 @@ fieldset { .box:focus { border-color: var(--ui-clr-primary); } + +.tr { + text-align: right; +} diff --git a/client/src/lib/api.ts b/client/src/lib/api.ts index d585e9e..11d3afc 100644 --- a/client/src/lib/api.ts +++ b/client/src/lib/api.ts @@ -22,7 +22,7 @@ type CallOptions = { export class PayloadToLargeError extends Error {} -async function call(options: CallOptions) { +export async function call(options: CallOptions) { const response = await fetch('/api/' + options.url, { method: options.method, body: options.body === undefined ? undefined : JSON.stringify(options.body), @@ -46,7 +46,7 @@ export async function create(note: Note) { meta: JSON.stringify(meta), } const data = await call({ - url: 'notes', + url: 'notes/', method: 'post', body, }) diff --git a/client/src/lib/stores/status.ts b/client/src/lib/stores/status.ts new file mode 100644 index 0000000..49ba919 --- /dev/null +++ b/client/src/lib/stores/status.ts @@ -0,0 +1,17 @@ +import { call } from '$lib/api' +import { onMount } from 'svelte' +import { writable } from 'svelte/store' + +export type Status = { + max_size: number +} + +export const status = writable(null) + +export async function init() { + const data = await call({ + url: 'status', + method: 'get', + }) + status.set(data) +} diff --git a/client/src/lib/ui/FileUpload.svelte b/client/src/lib/ui/FileUpload.svelte index d6194c9..fb8a9b2 100644 --- a/client/src/lib/ui/FileUpload.svelte +++ b/client/src/lib/ui/FileUpload.svelte @@ -2,6 +2,7 @@ import type { FileDTO } from '$lib/api' import { Files } from '$lib/files' import { createEventDispatcher } from 'svelte' + import MaxSize from './MaxSize.svelte' export let label: string = '' let files: File[] = [] @@ -49,6 +50,8 @@ {:else}
No Files Selected +
+ max:
{/if} diff --git a/client/src/lib/ui/MaxSize.svelte b/client/src/lib/ui/MaxSize.svelte new file mode 100644 index 0000000..4a88ed6 --- /dev/null +++ b/client/src/lib/ui/MaxSize.svelte @@ -0,0 +1,12 @@ + + + + {#if $status !== null} + {prettyBytes($status.max_size, { binary: true })} + {:else} + loading... + {/if} + diff --git a/client/src/lib/views/Create.svelte b/client/src/lib/views/Create.svelte index 72a15ea..bb1133c 100644 --- a/client/src/lib/views/Create.svelte +++ b/client/src/lib/views/Create.svelte @@ -1,13 +1,13 @@ diff --git a/proxy.mjs b/proxy.mjs index d119655..52c6254 100644 --- a/proxy.mjs +++ b/proxy.mjs @@ -1,5 +1,5 @@ -import httpProxy from 'http-proxy' import http from 'http' +import httpProxy from 'http-proxy' const proxy = httpProxy.createProxyServer({}) diff --git a/src/main.rs b/src/main.rs index e3e6a55..26d3ca0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) diff --git a/src/note/routes.rs b/src/note/routes.rs index 46d5e65..7e8fd58 100644 --- a/src/note/routes.rs +++ b/src/note/routes.rs @@ -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) -> 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) -> impl Responder { let mut n = note.into_inner(); let id = generate_id(); @@ -62,7 +63,7 @@ async fn create(note: web::Json) -> impl Responder { return HttpResponse::Ok().json(CreateResponse { id: id }); } -#[delete("/notes/{id}")] +#[delete("/{id}")] async fn delete(path: web::Path) -> impl Responder { let p = path.into_inner(); let note = store::get(&p.id); @@ -102,11 +103,27 @@ async fn delete(path: web::Path) -> 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), ); } diff --git a/src/size.rs b/src/size.rs index 941a883..8d9296e 100644 --- a/src/size.rs +++ b/src/size.rs @@ -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); }