|
|
@ -1,19 +1,20 @@ |
|
|
|
use json_dotpath::DotPaths; |
|
|
|
use json_dotpath::DotPaths; |
|
|
|
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; |
|
|
|
use parking_lot::RwLock; |
|
|
|
use rand::Rng; |
|
|
|
use rand::Rng; |
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
use rocket::request::FromRequest; |
|
|
|
use rocket::request::FromRequest; |
|
|
|
use rocket::response::ResponseBuilder; |
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
use rocket::{ |
|
|
|
http::{Cookie, Cookies, Status}, |
|
|
|
http::{Cookie, Status}, |
|
|
|
Data, Outcome, Request, Response, Rocket, State, |
|
|
|
Outcome, Request, Response, Rocket, State, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use serde::Serialize; |
|
|
|
use serde_json::{Map, Value}; |
|
|
|
use serde_json::{Map, Value}; |
|
|
|
use std::borrow::Cow; |
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
use std::time::{Instant, Duration}; |
|
|
|
|
|
|
|
use std::ops::Add; |
|
|
|
|
|
|
|
|
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
|
|
|
|
|
|
|
@ -22,12 +23,13 @@ type SessionsMap = HashMap<String, SessionInstance>; |
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
struct SessionInstance { |
|
|
|
struct SessionInstance { |
|
|
|
data: serde_json::Map<String, Value>, |
|
|
|
data: serde_json::Map<String, Value>, |
|
|
|
// TODO expiration
|
|
|
|
expires: Instant, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Debug)] |
|
|
|
#[derive(Default, Debug)] |
|
|
|
pub struct SessionStore { |
|
|
|
pub struct SessionStore { |
|
|
|
inner: RwLock<SessionsMap>, |
|
|
|
inner: RwLock<SessionsMap>, |
|
|
|
|
|
|
|
lifespan: Duration, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(PartialEq, Hash, Clone, Debug)] |
|
|
|
#[derive(PartialEq, Hash, Clone, Debug)] |
|
|
@ -88,15 +90,34 @@ impl<'a> Session<'a> { |
|
|
|
SessionFairing |
|
|
|
SessionFairing |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
fn tap<T>(&self, func : impl FnOnce(&mut serde_json::Map<String, Value>) -> T) -> T { |
|
|
|
let rg = self.store.inner.read(); |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
if let Some(ses) = rg.get(&self.id.0) { |
|
|
|
if let Some(instance) = wg.get_mut(&self.id.0) { |
|
|
|
ses.data.dot_get(path) |
|
|
|
instance.expires = Instant::now().add(self.store.lifespan); |
|
|
|
|
|
|
|
func(&mut instance.data) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
None |
|
|
|
let mut data = Map::new(); |
|
|
|
|
|
|
|
let rv = func(&mut data); |
|
|
|
|
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { |
|
|
|
|
|
|
|
data: data, |
|
|
|
|
|
|
|
expires: Instant::now().add(self.store.lifespan), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
rv |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn renew(&self) { |
|
|
|
|
|
|
|
self.tap(|_| ()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn reset(&self) { |
|
|
|
|
|
|
|
self.tap(|data| data.clear()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
|
|
|
|
self.tap(|data| data.dot_get(path)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T { |
|
|
|
pub fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T { |
|
|
|
self.get(path).unwrap_or(def) |
|
|
|
self.get(path).unwrap_or(def) |
|
|
|
} |
|
|
|
} |
|
|
@ -110,39 +131,19 @@ impl<'a> Session<'a> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
pub fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
self.tap(|data| data.dot_take(path)) |
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
|
|
|
ses.data.dot_take(path) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
None |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O> { |
|
|
|
pub fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O> { |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
self.tap(|data| data.dot_replace(path, new)) |
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
|
|
|
ses.data.dot_replace(path, new) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
None |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set<T: Serialize>(&self, path: &str, value: T) { |
|
|
|
pub fn set<T: Serialize>(&self, path: &str, value: T) { |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
self.tap(|data| data.dot_set(path, value)); |
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
|
|
|
ses.data.dot_set(path, value); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let mut map = Map::new(); |
|
|
|
|
|
|
|
map.dot_set(path, value); |
|
|
|
|
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { data: map }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn remove(&self, path: &str) { |
|
|
|
pub fn remove(&self, path: &str) -> bool { |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
self.tap(|data| data.dot_remove(path)) |
|
|
|
if let Some(ses) = wg.get_mut(&self.id.0) { |
|
|
|
|
|
|
|
ses.data.dot_remove(path); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|