renamed data to system, hashcache is not 'static

This commit is contained in:
Aravinth Manivannan 2021-03-09 15:35:33 +05:30
parent 049bdd9eaa
commit 8e065f8051
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 61 additions and 15 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
tarpaulin-report.html
.env

View file

@ -105,8 +105,8 @@ pub mod message {
/// message datatypes to interact with [MCaptcha] actor
pub mod cache;
pub mod data;
pub mod pow;
pub mod system;
mod utils;
pub use crate::cache::hashcache::HashCache;

View file

@ -28,11 +28,11 @@ use crate::mcaptcha::MCaptcha;
/// varying [Defense][crate::defense::Defense] configurations
/// so a "master" actor is needed to manage them all
#[derive(Clone)]
pub struct Master<'a> {
sites: BTreeMap<&'a str, Addr<MCaptcha>>,
pub struct Master {
sites: BTreeMap<String, Addr<MCaptcha>>,
}
impl Master<'static> {
impl Master {
/// add [MCaptcha] actor to [Master]
pub fn add_site(&mut self, details: AddSite) {
self.sites.insert(details.id, details.addr.to_owned());
@ -51,7 +51,7 @@ impl Master<'static> {
}
}
impl Actor for Master<'static> {
impl Actor for Master {
type Context = Context<Self>;
}
@ -60,7 +60,7 @@ impl Actor for Master<'static> {
#[rtype(result = "Option<Addr<MCaptcha>>")]
pub struct GetSite(pub String);
impl Handler<GetSite> for Master<'static> {
impl Handler<GetSite> for Master {
type Result = MessageResult<GetSite>;
fn handle(&mut self, m: GetSite, _ctx: &mut Self::Context) -> Self::Result {
@ -77,11 +77,11 @@ impl Handler<GetSite> for Master<'static> {
#[derive(Message, Builder)]
#[rtype(result = "()")]
pub struct AddSite {
pub id: &'static str,
pub id: String,
pub addr: Addr<MCaptcha>,
}
impl Handler<AddSite> for Master<'static> {
impl Handler<AddSite> for Master {
type Result = ();
fn handle(&mut self, m: AddSite, _ctx: &mut Self::Context) -> Self::Result {
@ -101,7 +101,7 @@ mod tests {
let id = "yo";
let mcaptcha = get_counter().start();
let msg = AddSiteBuilder::default()
.id(id)
.id(id.into())
.addr(mcaptcha.clone())
.build()
.unwrap();

View file

@ -15,7 +15,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! module describing various bits of data required for an mCaptcha system
//! module describing mCaptcha system
use actix::dev::*;
use derive_builder::Builder;
use pow_sha256::{Config, PoW};
@ -24,17 +24,19 @@ use crate::cache::messages;
use crate::cache::Save;
use crate::errors::*;
use crate::master::Master;
//use crate::models::*;
use crate::pow::*;
/// struct describing various bits of data required for an mCaptcha system
#[derive(Clone, Builder)]
pub struct Data<T: Save> {
master: Addr<Master<'static>>,
pub struct System<T: Save> {
pub master: Addr<Master>,
cache: Addr<T>,
pow: Config,
// db: PgPool,
}
impl<T> Data<T>
impl<T> System<T>
where
T: Save,
<T as actix::Actor>::Context: ToEnvelope<T, messages::Cache> + ToEnvelope<T, messages::Retrive>,
@ -77,6 +79,48 @@ where
Err(_) => Err(CaptchaError::Default),
}
}
// pub async fn register(&self, u: &Users) {
// sqlx::query!("INSERT INTO mcaptcha_users (name) VALUES ($1)", u.name)
// .execute(&self.db)
// .await
// .unwrap();
// }
//
// pub async fn levels(&self, l: &Levels) {
// sqlx::query!(
// "INSERT INTO mcaptcha_levels (id, difficulty_factor, visitor_threshold) VALUES ($1, $2, $3)",
// l.id,
// l.difficulty_factor,
// l.visitor_threshold
// )
// .execute(&self.db)
// .await
// .unwrap();
// }
//
// pub async fn add_mcaptcha(&self, m: &MCaptchaSystem) {
// sqlx::query!(
// "INSERT INTO mcaptcha_config (id, name, duration) VALUES ($1, $2, $3)",
// m.id,
// m.name,
// m.duration
// )
// .execute(&self.db)
// .await
// .unwrap();
// }
//
// async fn init_mcaptcha(&self, m: &MCaptchaSystem) {
// let id = sqlx::query_as!(
// Duration,
// "SELECT duration FROM mcaptcha_config WHERE id = ($1)",
// m.id,
// )
// .fetch_one(&self.db)
// .await
// .unwrap();
// }
}
#[cfg(test)]
@ -84,6 +128,7 @@ mod tests {
use pow_sha256::ConfigBuilder;
use super::System;
use super::*;
use crate::cache::HashCache;
use crate::master::*;
@ -91,7 +136,7 @@ mod tests {
const MCAPTCHA_NAME: &str = "batsense.net";
async fn boostrap_system() -> Data<HashCache> {
async fn boostrap_system() -> System<HashCache> {
let master = Master::new().start();
let mcaptcha = get_counter().start();
let pow = get_config();
@ -105,7 +150,7 @@ mod tests {
master.send(msg).await.unwrap();
DataBuilder::default()
SystemBuilder::default()
.master(master)
.cache(cache)
.pow(pow)