diff --git a/migrations/20211001071311_survey_users.sql b/migrations/20211001071311_survey_users.sql index 0bdc223..7be1d78 100644 --- a/migrations/20211001071311_survey_users.sql +++ b/migrations/20211001071311_survey_users.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS survey_users ( ID UUID PRIMARY KEY NOT NULL UNIQUE, - created_at TIMESTAMPTZ NOT NULL - device VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + device VARCHAR(100) NOT NULL ) diff --git a/migrations/20211005151526_survey_surveys.sql b/migrations/20211005151526_survey_surveys.sql new file mode 100644 index 0000000..bbf59ae --- /dev/null +++ b/migrations/20211005151526_survey_surveys.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS survey_surveys ( + ID UUID PRIMARY KEY NOT NULL UNIQUE, + name VARCHAR(100) NOT NULL, + challenges serial[] NOT NULL +) diff --git a/sqlx-data.json b/sqlx-data.json new file mode 100644 index 0000000..b726be8 --- /dev/null +++ b/sqlx-data.json @@ -0,0 +1,23 @@ +{ + "db": "PostgreSQL", + "f7c3faa9860de77ac29bb99afd4ed02112cf6c9432c094a3646fe64384f33866": { + "query": "SELECT challenges FROM survey_surveys WHERE id = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "challenges", + "type_info": "Int4Array" + } + ], + "parameters": { + "Left": [ + "Uuid" + ] + }, + "nullable": [ + false + ] + } + } +} \ No newline at end of file diff --git a/src/api/v1/cache.rs b/src/api/v1/cache.rs new file mode 100644 index 0000000..bd51c62 --- /dev/null +++ b/src/api/v1/cache.rs @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::collections::HashMap; +use std::sync::Arc; + +use sqlx::PgPool; +use uuid::Uuid; + +#[derive(Clone, Debug)] +pub struct ChallengeCache { + store: HashMap>>, + db: PgPool, +} + +impl ChallengeCache { + pub fn new(db: PgPool) -> Self { + let store = HashMap::default(); + Self { db, store } + } + + async fn get_db(&self, id: &Uuid) -> Vec { + struct Foo { + challenges: Vec, + } + let res = sqlx::query_as!( + Foo, + "SELECT challenges FROM survey_surveys WHERE id = $1", + &id + ) + .fetch_one(&self.db) + .await + .unwrap(); + + res.challenges + } + + pub async fn get(&mut self, k: &Uuid) -> Arc> { + match self.store.get(k) { + Some(val) => val.clone(), + None => { + let resp = self.get_db(k).await; + let resp = Arc::new(resp); + self.store.insert(k.clone(), resp.clone()); + resp + } + } + } +} diff --git a/src/api/v1/mod.rs b/src/api/v1/mod.rs index 49727a0..47f9e34 100644 --- a/src/api/v1/mod.rs +++ b/src/api/v1/mod.rs @@ -18,6 +18,7 @@ use actix_web::web::ServiceConfig; use uuid::Uuid; pub mod auth; +pub mod cache; mod meta; pub mod routes; pub use routes::ROUTES; diff --git a/src/main.rs b/src/main.rs index 00d11b5..de27168 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,7 +128,7 @@ pub fn get_identity_service() -> IdentityService { let cookie_secret = &SETTINGS.server.cookie_secret; IdentityService::new( CookieIdentityPolicy::new(cookie_secret.as_bytes()) - .name("Authorization") + .name("survey-id") //TODO change cookie age .max_age_secs(216000) .domain(&SETTINGS.server.domain)