start work on postgres support

main
Volkor 2023-01-25 01:47:34 +11:00
parent 7bd48be829
commit f8e29c7f3e
Signed by: Volkor
GPG Key ID: BAD7CA8A81CC2DA5
5 changed files with 120 additions and 6 deletions

85
Cargo.lock generated
View File

@ -425,6 +425,27 @@ checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
@ -765,6 +786,24 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hkdf"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791a029f6b9fc27657f6f188ec6e5e43f6911f6f878e0dc5501396e09809d437"
dependencies = [
"hmac",
]
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "http"
version = "0.2.8"
@ -1036,6 +1075,15 @@ dependencies = [
"regex-automata",
]
[[package]]
name = "md-5"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
dependencies = [
"digest",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -1549,6 +1597,17 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom 0.2.8",
"redox_syscall",
"thiserror",
]
[[package]]
name = "regex"
version = "1.7.0"
@ -2038,11 +2097,13 @@ checksum = "dcbc16ddba161afc99e14d1713a453747a2b07fc097d2009f4c300ec99286105"
dependencies = [
"ahash",
"atoi",
"base64 0.13.1",
"bitflags",
"byteorder",
"bytes",
"crc",
"crossbeam-queue",
"dirs",
"dotenvy",
"either",
"event-listener",
@ -2054,17 +2115,24 @@ dependencies = [
"futures-util",
"hashlink",
"hex",
"hkdf",
"hmac",
"indexmap",
"itoa",
"libc",
"libsqlite3-sys",
"log",
"md-5",
"memchr",
"once_cell",
"paste",
"percent-encoding",
"rand 0.8.5",
"rustls",
"rustls-pemfile",
"serde",
"serde_json",
"sha1",
"sha2",
"smallvec",
"sqlformat",
@ -2074,6 +2142,7 @@ dependencies = [
"tokio-stream",
"url",
"webpki-roots",
"whoami",
]
[[package]]
@ -2128,6 +2197,12 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "syn"
version = "1.0.105"
@ -2656,6 +2731,16 @@ dependencies = [
"webpki",
]
[[package]]
name = "whoami"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45dbc71f0cdca27dc261a9bd37ddec174e4a0af2b900b890f378460f745426e3"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -12,7 +12,7 @@ edition = "2021"
[dependencies]
ramhorns = "0.14.0"
salvo = { version = "*", features = ["logging", "serve-static"] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "sqlite" ] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "sqlite", "postgres" ] }
once_cell = "1.16.0"
tokio = { version = "1", features = ["macros"] }
tracing = "0.1"

View File

@ -29,7 +29,6 @@ Eventually we'll have 2 different environments, development and production, each
1.1. If you want to speed up sqlite a little bit more, run `sqlite3 ephemeral.db` and then `PRAGMA synchronous = OFF;` and then Ctrl+D
2. Compile and run ephemeral with `LOG=debug cargo run` for the debug build. `LOG=info cargo run -r` will run the release build.
### Configuration Options
Configuration is done by settings environment variables in the launch command.
@ -44,7 +43,7 @@ Configuration is done by settings environment variables in the launch command.
| CLEANER_INTERVAL | 1800 | How long the cleaner task runs, in seconds. |
| FILE_EXPIRY_MIN | 7 | (Depends on engine setting) The Minimum a file /should/ exist on the server for. |
| FILE_EXPIRY_MAX | 365 | (Depends on engine setting) The Longest a file /should/ exist on the server for. |
| MAX_FILESIZE* | 1073741824 | The 'cap' for calculating expiry. |
| MAX_FILESIZE* | 1073741824 | The 'cap' for calculating expiry. |
\*MAX_FILESIZE doesn't actually set the maximum allowed filesize, it's only used for calculating the expiry.

View File

@ -7,6 +7,15 @@ port = 8282
# TODO: benchmarks
nginx_sendfile = false
[database] # DATABASE DATABASE JUST LIVING IN THE DATABASE WOOAH
# What SQL Backend to use
## Available SQL backends: sqlite, postgres
sql_backend = "sqlite"
# URL for postgres, see below for more information
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
postgres_url = "postgres://ephemeral:changeme@localhost:5432/ephemeral"
[logging]
# Available logging levels
# info - only show critical errors, and usage information

View File

@ -1,3 +1,4 @@
use sqlx::PgPool;
use once_cell::sync::OnceCell;
use ramhorns::{Content, Ramhorns};
use salvo::fs::NamedFile;
@ -23,8 +24,12 @@ use tracing_subscriber::prelude::*;
mod db;
mod engine;
// I /don't think/ there's any harm to adding multiple of thse for each sql backend.
// Setup the global sqlite db
static SQLITE: OnceCell<SqlitePool> = OnceCell::new();
// Setup the config globally because I can't figure out how to pass it to functions I don't call directly.
// This is evaluated at runtime, and not compilation. \o/
lazy_static! {
@ -469,9 +474,25 @@ async fn main() {
.init();
// Set up DB Pool!
let pool = SqlitePool::connect("sqlite:ephemeral.db").await.unwrap();
// Sets the db pool to the static thingy, so we can access it /anywhere!/
SQLITE.set(pool).unwrap();
// Determine what SQL engine we're running.
let sql = CONFIG
.get_string("database.sql_backend")
.expect("Couldn't find 'sql_backend' in config. :(");
// This /might be able to be a match statement, but I couldn't figure it out and want to go to bed.
if sql == "sqlite" {
tracing::info!("Using SQLite backend");
let pool = SqlitePool::connect("sqlite:ephemeral.db").await.unwrap();
SQLITE.set(pool).unwrap();
} else if sql == "postgres" {
tracing::info!("Using Postgres backend");
// We're gonna need to load /all/ the postgres stuff up here
let psql = CONFIG
.get_string("operations.postgres_url")
.expect("Couldn't find 'postgres_url' in config. :(");
let pool = PgPool::connect(&psql).await;
// TODO: figure out how to use sqlx::Pool struct https://docs.rs/sqlx/latest/sqlx/struct.Pool.html
}
// Initialise the cleaner task
let interval = CONFIG