Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features and fixes to get towards a complete working example #69

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
shell: bash
run: |
cd target/${{ matrix.platform.target }}/release
tar czvf ../../../${{ matrix.platform.target }}.tar.gz soldr{,-ui}
tar czvf ../../../soldr-${{ matrix.platform.target }}.tar.gz soldr{,-ui}
cd -
- name: Publish release artifacts
uses: actions/upload-artifact@v3
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
target
soldr.db
# vite installs files into this direcotry at build time
crates/ui/static
# generated by mdbook
index.html
# example db used for developement
soldr.db
target
99 changes: 91 additions & 8 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ default-run = "soldr"
[dependencies]
anyhow = "1.0"
axum = "0.7"
axum-auth = { version = "0.7.0", features = ["auth-basic"], default-features = false }
axum-server = { version = "0.6", features = ["tls-rustls"] }
clap = { version = "4.3.8", features = ["derive"] }
http = "1.0.0"
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"
Expand All @@ -22,10 +24,11 @@ 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.5", features = ["trace", "cors"] }
tower-http = { version = "0.5", features = ["cors", "fs", "trace"] }

[dev-dependencies]
criterion = {version = "0.4", features = ["async_tokio"]}
http-auth-basic = "0.3.3"
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false }

[[bench]]
Expand Down
32 changes: 32 additions & 0 deletions crates/proxy/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Database {
pub url: String,
}

#[derive(Debug, Deserialize)]
pub struct Proxy {
pub listen: String,
}

#[derive(Debug, Deserialize)]
pub struct Management {
pub listen: String,
pub secret: String,
}

#[derive(Debug, Deserialize)]
pub struct Tls {
pub enable: bool,
pub cert_path: Option<String>,
pub key_path: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Config {
pub database: Database,
pub management: Management,
pub proxy: Proxy,
pub tls: Tls,
}
46 changes: 9 additions & 37 deletions crates/proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod alert;
pub mod cache;
pub mod config;
pub mod db;
pub mod error;
pub mod mgmt;
Expand All @@ -19,62 +20,33 @@ use axum::http::{HeaderMap, Request, StatusCode};
use axum::response::IntoResponse;
use axum::{routing::any, Router};
use queue::RetryQueue;
use serde::Deserialize;
use sqlx::sqlite::SqlitePool;
use tower_http::services::ServeDir;

use crate::cache::OriginCache;
use crate::config::Config;
use crate::db::ensure_schema;
use crate::error::AppError;
use crate::mgmt::update_origin_cache;
use crate::proxy::{proxy, Client};
use crate::request::HttpRequest;
use crate::request::State as RequestState;

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct Tls {
pub enable: bool,
pub cert_path: Option<String>,
pub key_path: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(default)]
pub struct Config {
pub database_url: String,
pub management_listener: String,
pub ingest_listener: String,
pub tls: Tls,
}

impl Default for Config {
fn default() -> Self {
Self {
// TODO change to a file location
// maybe $XDG_DATA_DIR ?
database_url: "sqlite::memory:".to_string(),
management_listener: "0.0.0.0:3443".to_string(),
ingest_listener: "0.0.0.0:3000".to_string(),
tls: Tls {
enable: false,
cert_path: None,
key_path: None,
},
}
}
}

pub async fn app(config: &Config) -> Result<(Router, Router, RetryQueue)> {
let pool = SqlitePool::connect(&config.database_url).await?;
let pool = SqlitePool::connect(&config.database.url).await?;
ensure_schema(&pool).await?;

let origin_cache = OriginCache::new();
update_origin_cache(&pool, &origin_cache).await?;

let mgmt_router = mgmt::router(pool.clone(), origin_cache.clone());
if config.management.secret.len() < 32 {
anyhow::bail!("Management secret must be at least 32 characters long");
}
let mgmt_router = mgmt::router(pool.clone(), origin_cache.clone(), config);

let client = Client::new();
let router = Router::new()
.nest_service("/.well-known", ServeDir::new("public/.well-known"))
.route("/", any(handler))
.route("/*path", any(handler))
.layer(Extension(pool.clone()))
Expand Down
Loading
Loading