time based fix

This commit is contained in:
cupcakearmy 2021-12-22 14:46:06 +01:00
parent 8cee6579e2
commit 33829768eb
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
5 changed files with 28 additions and 13 deletions

View File

@ -16,4 +16,4 @@ services:
depends_on: depends_on:
- memcached - memcached
ports: ports:
- 5000:5000 - 80:5000

View File

@ -1,11 +1,19 @@
import http from 'http' import http from 'http'
import httpProxy from 'http-proxy' import httpProxy from 'http-proxy'
const proxy = httpProxy.createProxyServer({}) function start() {
try {
const proxy = httpProxy.createProxyServer({})
const server = http.createServer(function (req, res) {
const target = req.url.startsWith('/api/') ? 'http://localhost:5000' : 'http://localhost:3000'
proxy.web(req, res, { target })
})
server.listen(1234)
var server = http.createServer(function (req, res) { server.on('error', () => start())
const target = req.url.startsWith('/api/') ? 'http://localhost:5000' : 'http://localhost:3000' } catch (e) {
proxy.web(req, res, { target }) start()
}) }
}
server.listen(1234) start()

View File

@ -7,7 +7,7 @@ pub struct Note {
pub meta: String, pub meta: String,
pub contents: String, pub contents: String,
pub views: Option<u8>, pub views: Option<u8>,
pub expiration: Option<u64>, pub expiration: Option<u32>,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]

View File

@ -6,11 +6,11 @@ use crate::note::{generate_id, Note, NoteInfo, NotePublic};
use crate::size::LIMIT; use crate::size::LIMIT;
use crate::store; use crate::store;
fn now() -> u64 { pub fn now() -> u32 {
SystemTime::now() SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs() .as_secs() as u32
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -86,10 +86,12 @@ async fn delete(path: web::Path<NotePath>) -> impl Responder {
} }
_ => {} _ => {}
} }
let n = now();
match changed.expiration { match changed.expiration {
Some(e) => { Some(e) => {
store::del(&p.id.clone()); if e < n {
if e < now() { store::del(&p.id.clone());
return HttpResponse::BadRequest().finish(); return HttpResponse::BadRequest().finish();
} }
} }

View File

@ -1,5 +1,6 @@
use memcache; use memcache;
use crate::note::now;
use crate::note::Note; use crate::note::Note;
lazy_static! { lazy_static! {
@ -12,7 +13,11 @@ lazy_static! {
pub fn set(id: &String, note: &Note) { pub fn set(id: &String, note: &Note) {
let serialized = serde_json::to_string(&note.clone()).unwrap(); let serialized = serde_json::to_string(&note.clone()).unwrap();
CLIENT.set(id, serialized, 0).unwrap(); let expiration: u32 = match note.expiration {
Some(e) => e - now(),
None => 0,
};
CLIENT.set(id, serialized, expiration).unwrap();
} }
pub fn get(id: &String) -> Option<Note> { pub fn get(id: &String) -> Option<Note> {