Skip to content

Commit

Permalink
Swapped DB providers, and added compose configs for dev.
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthmark committed Dec 25, 2024
1 parent 73a9156 commit 2c7477f
Show file tree
Hide file tree
Showing 22 changed files with 1,382 additions and 1,337 deletions.
1,696 changes: 850 additions & 846 deletions Cargo.lock

Large diffs are not rendered by default.

41 changes: 22 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rocket = { version = "*", features = ["json", "secrets", "uuid"] }
rocket_prometheus = "*"
rocket_db_pools = { version = "*", features = [
"diesel_postgres",
"deadpool_redis",
] }
thiserror = "*"
serde = { version = "*", features = ["derive"] }
serde_json = "*"
rust-embed = "*"
askama = { version = "*", features = ["with-axum"] }
axum = { version = "*", features = ["tower-log", "macros"] }
axum-prometheus = "*"
base64 = "*"
bb8 = "*"
bb8-redis = "*"
chrono = { version = "*", features = ["serde"] }
reqwest = { version = "*", features = ["tokio-rustls", "json"] }
diesel = { version = "*", features = ["postgres", "chrono"] }
diesel-async = { version = "*", features = ["postgres", "bb8"] }
figment = { version = "*", features = ["env", "toml"] }
image = { version = "*", features = ["libwebp", "rgb"] }
sha2 = "*"
rocket_dyn_templates = { version = "*", features = ["handlebars"] }
lazy_static = "*"
mediatype = { version = "*", features = ["serde"] }
metrics = "*"
metrics-exporter-prometheus = "*"
rand = "*"
base64 = "*"
reqwest = { version = "*", features = ["tokio-rustls", "json"] }
rust-embed = "*"
serde = { version = "*", features = ["derive"] }
serde_json = "*"
sha2 = "*"
thiserror = "*"
tokio = { version = "*", features = ["full"] }
tracing = "*"
tracing-subscriber = { version = "*", features = ["env-filter"] }
uuid = { version = "*", features = ["serde"] }
lazy_static = "*"
diesel = { version = "*", features = ["postgres", "chrono"] }
diesel-async = "*"
dotenvy = "*"
rocket_oauth2 = "0.5.0"
4 changes: 2 additions & 2 deletions diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
custom_type_derives = ["diesel::query_builder::QueryId", "diesel::sql_types::SqlType", "Clone", "std::fmt::Debug"]

[migrations_directory]
dir = "C:\\Users\\earth\\Source\\Imagefork\\migrations"
dir = "migrations"
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
token-store:
image: redis
ports:
- "6379:6379"
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: testpgpass
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_data:
7 changes: 7 additions & 0 deletions imagefork.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app_addr="localhost:8080"
monitoring_addr="localhost:8081"
core_pg_url="postgres://postgres:testpgpass@localhost/postgres"

[redirect]
coherency_token_redis_url="redis://localhost:6379"
coherency_token_keepalive_minutes=20
6 changes: 0 additions & 6 deletions migrations/2024-12-01-031642_initial/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ CREATE TABLE Creators (
id BIGSERIAL PRIMARY KEY,
creation_time TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc'),
email TEXT NOT NULL UNIQUE,
token TEXT NOT NULL UNIQUE DEFAULT '',
minting_time TIMESTAMP NOT NULL DEFAULT (now() at time zone 'utc'),
lockout BOOLEAN NOT NULL DEFAULT FALSE,
moderator BOOLEAN NOT NULL DEFAULT FALSE,
poster_limit INTEGER NOT NULL DEFAULT 3
Expand All @@ -23,7 +21,3 @@ CREATE TABLE Posters (
)
) STORED
);

INSERT INTO Creators (id, email, poster_limit) VALUES (0, 'SYSTEM', 0);
INSERT INTO Posters (id, creator, url, stopped) VALUES (0, 0, 'ERROR', TRUE);
INSERT INTO Posters (id, creator, url, stopped) VALUES (1, 0, 'SAFE', TRUE);
107 changes: 54 additions & 53 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
use crate::config::ConfigInfo;
use deadpool_redis::redis::cmd;
use lazy_static::lazy_static;
use rocket_db_pools::{deadpool_redis, Connection, Database};
use rocket_prometheus::prometheus::{register_int_counter_vec, IntCounterVec};
use serde::Deserialize;
use axum::{
extract::{FromRef, FromRequestParts},
http::request::Parts,
};
use bb8_redis::{redis::cmd, RedisConnectionManager};
use metrics::counter;
use sha2::{digest::Output, Digest, Sha256};
use std::future::Future;

lazy_static! {
pub static ref CACHE_RESOLUTION: IntCounterVec = register_int_counter_vec!(
"redirect_cache_status",
"Cache hit status from the redirect cache.",
&["hit_status"]
)
.unwrap();
}

#[derive(Database)]
#[database("tokens")]
pub struct Cache(deadpool_redis::Pool);
use crate::Error;

#[derive(Deserialize)]
pub struct TokenCacheConfig {
pub token_keepalive_minutes: i32,
}
pub type CoherencyTokenManager = RedisConnectionManager;
pub type CoherencyTokenPool = bb8::Pool<CoherencyTokenManager>;

impl ConfigInfo for TokenCacheConfig {
fn field() -> &'static str {
"tokens"
}

fn name() -> &'static str {
"Config for tokens redis db"
}
}
pub struct CoherencyTokenConn(bb8::PooledConnection<'static, CoherencyTokenManager>);

fn hash_token(token: &str) -> Output<Sha256> {
let mut hasher = Sha256::new();
hasher.update(token);
hasher.finalize()
}

impl Cache {
#[axum::async_trait]
impl<S> FromRequestParts<S> for CoherencyTokenConn
where
S: Send + Sync,
CoherencyTokenPool: FromRef<S>,
{
type Rejection = Error;

async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let pool = CoherencyTokenPool::from_ref(state);

let conn = pool.get_owned().await?;

Ok(Self(conn))
}
}

impl CoherencyTokenConn {
pub async fn get_or_create(
db: &mut Connection<Self>,
&mut self,
token: &str,
token_keepalive_seconds: i32,
init: impl Future<Output = i64>,
) -> Result<i64, crate::Error> {
token_keepalive_seconds: u32,
init: impl Future<Output = crate::Result<Option<i64>>>,
) -> crate::Result<Option<i64>> {
let hash = hash_token(token);

if let Some(target) = cmd("GETEX")
.arg(hash.as_slice())
.arg("EX")
.arg(token_keepalive_seconds)
.query_async(&mut **db)
.query_async(&mut *self.0)
.await?
{
CACHE_RESOLUTION.with_label_values(&["hit"]);
counter!("/coherency_token/hit").increment(1);
Ok(target)
} else {
let target = init.await;
let try_set: Option<i64> = cmd("SET")
.arg(hash.as_slice())
.arg(target)
.arg("NX")
.arg("GET")
.arg("EX")
.arg(token_keepalive_seconds)
.query_async(&mut **db)
.await?;
if let Some(target) = try_set {
CACHE_RESOLUTION.with_label_values(&["discard_update"]);
Ok(target)
if let Some(target) = init.await? {
let try_set: Option<i64> = cmd("SET")
.arg(hash.as_slice())
.arg(target)
.arg("NX")
.arg("GET")
.arg("EX")
.arg(token_keepalive_seconds)
.query_async(&mut *self.0)
.await?;
if let Some(target) = try_set {
counter!("/coherency_token/update_discarded").increment(1);
Ok(Some(target))
} else {
counter!("/coherency_token/update").increment(1);
Ok(Some(target))
}
} else {
CACHE_RESOLUTION.with_label_values(&["update"]);
Ok(target)
Ok(None)
}
}
}
}

/*
#[cfg(test)]
mod test {
use std::time::Duration;
Expand Down Expand Up @@ -157,3 +157,4 @@ mod test {
client.get(uri!(force_delete(token = "D")));
}
}
*/
86 changes: 0 additions & 86 deletions src/config.rs

This file was deleted.

9 changes: 6 additions & 3 deletions src/db/creator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::Imagefork;
use super::DbConn;
use crate::schema::creators::dsl::*;
use chrono::NaiveDateTime;
use rocket_db_pools::{diesel::prelude::*, Connection};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use serde::{Deserialize, Serialize};

#[derive(Queryable, Selectable, Deserialize, Serialize)]
Expand All @@ -18,7 +19,7 @@ pub struct Creator {

impl Creator {
pub async fn get(
db: &mut Connection<Imagefork>,
db: &mut DbConn,
creator_id: i64,
) -> crate::error::Result<Option<Self>> {
Ok(creators
Expand All @@ -30,6 +31,7 @@ impl Creator {
}
}

/*
#[cfg(test)]
pub mod test {
use super::{
Expand Down Expand Up @@ -66,3 +68,4 @@ pub mod test {
assert!(creator.poster_limit < 20);
}
}
*/
Loading

0 comments on commit 2c7477f

Please sign in to comment.