Skip to content

Commit

Permalink
feat: API and UI to edit a request
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr3 committed Jan 13, 2024
1 parent 2444e41 commit b18a857
Show file tree
Hide file tree
Showing 12 changed files with 768 additions and 47 deletions.
102 changes: 102 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions crates/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
name = "soldr"
version = "0.1.0"
edition = "2021"
default-run = "soldr"

[dependencies]
anyhow = "1.0"
axum = "0.6.18"
clap = { version = "4.3.8", features = ["derive"] }
hyper = { version = "0.14", features = ["full"] }
lettre = { version = "0.10.4", default-features = false, features = ["smtp-transport", "tokio1", "tokio1-rustls-tls", "builder"] }
parking_lot = "0.12.1"
rand = "0.8.5"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
shared_types = { version = "0.0.0", path = "../shared_types" }
sqlx = { version = "0.7.1", features = ["sqlite", "runtime-tokio-rustls"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio = { version = "1.0", features = ["full"] }
toml = "0.7.5"
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.4.0", features = ["trace"] }
clap = { version = "4.3.8", features = ["derive"] }
toml = "0.7.5"
parking_lot = "0.12.1"
rand = "0.8.5"
lettre = { version = "0.10.4", default-features = false, features = ["smtp-transport", "tokio1", "tokio1-rustls-tls", "builder"] }

[dev-dependencies]
criterion = {version = "0.4", features = ["async_tokio"]}
Expand Down
2 changes: 1 addition & 1 deletion crates/proxy/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;

use crate::db::Origin;
use crate::error::AppError;
use shared_types::Origin;

#[derive(Debug)]
pub struct OriginCache(pub(crate) Arc<OriginCacheInner>);
Expand Down
106 changes: 69 additions & 37 deletions crates/proxy/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqlitePool;
use sqlx::sqlite::{SqlitePool, SqliteQueryResult};

use shared_types::{NewOrigin, Origin};

use crate::request::HttpRequest;
use crate::retry::backoff;

#[derive(Debug, Deserialize, Serialize, sqlx::FromRow, Clone)]
pub struct Origin {
pub id: i64,
pub domain: String,
pub origin_uri: String,
pub timeout: u32,
pub alert_threshold: Option<u16>,
pub alert_email: Option<String>,
pub smtp_host: Option<String>,
pub smtp_username: Option<String>,
pub smtp_password: Option<String>,
pub smtp_port: Option<u16>,
pub smtp_tls: bool,
}

#[derive(Debug)]
pub struct QueuedRequest {
pub id: i64,
Expand Down Expand Up @@ -323,27 +310,6 @@ pub async fn list_attempts(pool: &SqlitePool) -> Result<Vec<Attempt>> {
Ok(attempts)
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct NewOrigin {
pub domain: String,
pub origin_uri: String,
pub timeout: u32,
#[serde(default)]
pub alert_threshold: Option<u16>,
#[serde(default)]
pub alert_email: Option<String>,
#[serde(default)]
pub smtp_host: Option<String>,
#[serde(default)]
pub smtp_username: Option<String>,
#[serde(default)]
pub smtp_password: Option<String>,
#[serde(default)]
pub smtp_port: Option<u16>,
#[serde(default)]
pub smtp_tls: bool,
}

pub async fn insert_origin(pool: &SqlitePool, origin: NewOrigin) -> Result<Origin> {
tracing::trace!("insert_origin");
let mut conn = pool.acquire().await?;
Expand Down Expand Up @@ -398,6 +364,46 @@ pub async fn insert_origin(pool: &SqlitePool, origin: NewOrigin) -> Result<Origi
Ok(created_origin)
}

pub async fn update_origin(pool: &SqlitePool, id: i64, origin: NewOrigin) -> Result<Origin> {
tracing::trace!("update_origin");
let mut conn = pool.acquire().await?;

let query = r#"
UPDATE origins
SET
domain = ?,
origin_uri = ?,
timeout = ?,
alert_threshold = ?,
alert_email = ?,
smtp_host = ?,
smtp_username = ?,
smtp_password = ?,
smtp_port = ?,
smtp_tls = ?,
updated_at = strftime('%s','now')
WHERE id = ?
RETURNING *
"#;

let updated_origin = sqlx::query_as::<_, Origin>(query)
.bind(origin.domain)
.bind(origin.origin_uri)
.bind(origin.timeout)
.bind(origin.alert_threshold)
.bind(origin.alert_email)
.bind(origin.smtp_host)
.bind(origin.smtp_username)
.bind(origin.smtp_password)
.bind(origin.smtp_port)
.bind(origin.smtp_tls)
.bind(id)
.fetch_one(&mut *conn)
.await?;

Ok(updated_origin)
}

pub async fn list_origins(pool: &SqlitePool) -> Result<Vec<Origin>> {
tracing::trace!("list_origins");
let mut conn = pool.acquire().await?;
Expand All @@ -409,6 +415,32 @@ pub async fn list_origins(pool: &SqlitePool) -> Result<Vec<Origin>> {
Ok(origins)
}

pub async fn get_origin(pool: &SqlitePool, id: i64) -> Result<Origin> {
tracing::trace!("get_origin");
let mut conn = pool.acquire().await?;

let origin = sqlx::query_as::<_, Origin>("SELECT * FROM origins WHERE id = ?;")
.bind(id)
.fetch_one(&mut *conn)
.await?;

Ok(origin)
}

pub async fn delete_origin(pool: &SqlitePool, id: i64) -> Result<bool> {
tracing::trace!("delete origin");
let mut conn = pool.acquire().await?;

let query = r#"
DELETE FROM origins
WHERE id = ?;
"#;

let result: SqliteQueryResult = sqlx::query(query).bind(id).execute(&mut *conn).await?;

Ok(result.rows_affected() > 0)
}

pub async fn purge_completed_requests(pool: &SqlitePool, days: u32) -> Result<()> {
tracing::trace!("purge_completed_requests");
let mut conn = pool.acquire().await?;
Expand Down
Loading

0 comments on commit b18a857

Please sign in to comment.