mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-14 13:08:27 +03:00
feat: conditionally init postgres/mariadb connection
This commit is contained in:
parent
f9efb062e0
commit
912b342e0e
6 changed files with 241 additions and 21 deletions
157
Cargo.lock
generated
157
Cargo.lock
generated
|
@ -493,6 +493,12 @@ version = "0.13.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||
|
||||
[[package]]
|
||||
name = "base64ct"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bdca834647821e0b13d9539a8634eb62d3501b6b6c2cec1722786ee6671b851"
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.3.3"
|
||||
|
@ -654,6 +660,12 @@ dependencies = [
|
|||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-oid"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3"
|
||||
|
||||
[[package]]
|
||||
name = "const_fn"
|
||||
version = "0.4.9"
|
||||
|
@ -775,6 +787,16 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-bigint"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03c6a1d5fa1de37e071642dfa44ec552ca5b299adb128fab16138e24b548fd21"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
|
@ -847,6 +869,18 @@ dependencies = [
|
|||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "db-sqlx-maria"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-rt",
|
||||
"async-trait",
|
||||
"db-core",
|
||||
"futures",
|
||||
"sqlx",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "db-sqlx-postgres"
|
||||
version = "0.1.0"
|
||||
|
@ -859,6 +893,17 @@ dependencies = [
|
|||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6919815d73839e7ad218de758883aae3a257ba6759ce7a9992501efbb53d705c"
|
||||
dependencies = [
|
||||
"const-oid",
|
||||
"crypto-bigint",
|
||||
"pem-rfc7468",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_builder"
|
||||
version = "0.10.2"
|
||||
|
@ -1503,6 +1548,9 @@ name = "lazy_static"
|
|||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
dependencies = [
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lettre"
|
||||
|
@ -1549,6 +1597,12 @@ version = "0.2.126"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db"
|
||||
|
||||
[[package]]
|
||||
name = "libmcaptcha"
|
||||
version = "0.1.4"
|
||||
|
@ -1667,6 +1721,7 @@ dependencies = [
|
|||
"cache-buster",
|
||||
"config",
|
||||
"db-core",
|
||||
"db-sqlx-maria",
|
||||
"db-sqlx-postgres",
|
||||
"derive_builder 0.11.1",
|
||||
"derive_more",
|
||||
|
@ -1801,7 +1856,7 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-bigint 0.4.3",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
|
@ -1809,6 +1864,17 @@ dependencies = [
|
|||
"num-traits 0.2.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits 0.2.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
|
@ -1821,6 +1887,23 @@ dependencies = [
|
|||
"serde 1.0.140",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint-dig"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "566d173b2f9406afbc5510a90925d5a2cd80cae4605631f1212303df265de011"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"lazy_static",
|
||||
"libm",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-traits 0.2.15",
|
||||
"rand",
|
||||
"smallvec",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.2"
|
||||
|
@ -1880,6 +1963,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2022,6 +2106,15 @@ version = "1.0.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
|
||||
|
||||
[[package]]
|
||||
name = "pem-rfc7468"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01de5d978f34aa4b2296576379fcc416034702fd94117c56ffd8a1a767cefb30"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
|
@ -2078,6 +2171,28 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
|
||||
[[package]]
|
||||
name = "pkcs1"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a78f66c04ccc83dd4486fd46c33896f4e17b24a7a3a6400dedc48ed0ddd72320"
|
||||
dependencies = [
|
||||
"der",
|
||||
"pkcs8",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkcs8"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cabda3fb821068a9a4fab19a683eac3af12edf0f34b94a8be53c4972b8149d0"
|
||||
dependencies = [
|
||||
"der",
|
||||
"spki",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.25"
|
||||
|
@ -2303,6 +2418,26 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rsa"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4cf22754c49613d2b3b119f0e5d46e34a2c628a937e3024b8762de4e7d8c710b"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"digest 0.10.3",
|
||||
"num-bigint-dig",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-traits 0.2.15",
|
||||
"pkcs1",
|
||||
"pkcs8",
|
||||
"rand_core",
|
||||
"smallvec",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-argon2"
|
||||
version = "1.0.0"
|
||||
|
@ -2677,6 +2812,16 @@ version = "0.5.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spki"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d01ac02a6ccf3e07db148d2be087da624fea0221a16152ed01f0496a6b0a27"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"der",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sqlformat"
|
||||
version = "0.1.8"
|
||||
|
@ -2712,6 +2857,7 @@ dependencies = [
|
|||
"bytes",
|
||||
"crc",
|
||||
"crossbeam-queue",
|
||||
"digest 0.10.3",
|
||||
"dirs",
|
||||
"either",
|
||||
"event-listener",
|
||||
|
@ -2719,6 +2865,7 @@ dependencies = [
|
|||
"futures-core",
|
||||
"futures-intrusive",
|
||||
"futures-util",
|
||||
"generic-array",
|
||||
"hashlink",
|
||||
"hex",
|
||||
"hkdf",
|
||||
|
@ -2729,10 +2876,12 @@ dependencies = [
|
|||
"log",
|
||||
"md-5",
|
||||
"memchr",
|
||||
"num-bigint 0.3.3",
|
||||
"once_cell",
|
||||
"paste",
|
||||
"percent-encoding",
|
||||
"rand",
|
||||
"rsa",
|
||||
"rustls",
|
||||
"serde 1.0.140",
|
||||
"serde_json",
|
||||
|
@ -3479,6 +3628,12 @@ dependencies = [
|
|||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20b578acffd8516a6c3f2a1bdefc1ec37e547bb4e0fb8b6b01a4cafc886b4442"
|
||||
|
||||
[[package]]
|
||||
name = "zstd"
|
||||
version = "0.11.2+zstd.1.5.2"
|
||||
|
|
|
@ -16,7 +16,7 @@ build = "build.rs"
|
|||
|
||||
[workspace]
|
||||
exclude = ["db/db-migrations", "utils/cache-bust"]
|
||||
memebers = [".", "db/db-core", "db/db-sqlx-postgres"]
|
||||
memebers = [".", "db/db-core", "db/db-sqlx-postgres", "db/db-sqlx-maria"]
|
||||
|
||||
[[bin]]
|
||||
name = "mcaptcha"
|
||||
|
@ -38,7 +38,7 @@ cache-buster = { git = "https://github.com/realaravinth/cache-buster" }
|
|||
futures = "0.3.15"
|
||||
tokio = { version = "1.14", features = ["sync"]}
|
||||
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] }
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline", "mysql"] }
|
||||
argon2-creds = { branch = "master", git = "https://github.com/realaravinth/argon2-creds"}
|
||||
#argon2-creds = { version="*", path = "../../argon2-creds/" }
|
||||
config = "0.11"
|
||||
|
@ -84,6 +84,9 @@ path = "./db/db-core"
|
|||
[dependencies.db-sqlx-postgres]
|
||||
path = "./db/db-sqlx-postgres"
|
||||
|
||||
[dependencies.db-sqlx-maria]
|
||||
path = "./db/db-sqlx-maria"
|
||||
|
||||
[dependencies.my-codegen]
|
||||
git = "https://github.com/realaravinth/actix-web"
|
||||
package = "actix-web-codegen"
|
||||
|
@ -95,7 +98,7 @@ features = ["actix_identity_backend"]
|
|||
|
||||
[build-dependencies]
|
||||
serde_json = "1"
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] }
|
||||
sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline", "mysql" ] }
|
||||
|
||||
[dev-dependencies]
|
||||
pow_sha256 = { version = "0.2.1", git = "https://github.com/mcaptcha/pow_sha256" }
|
||||
|
|
12
Makefile
12
Makefile
|
@ -22,6 +22,9 @@ check: ## Check for syntax errors on all workspaces
|
|||
cd db/db-sqlx-postgres &&\
|
||||
DATABASE_URL=${POSTGRES_DATABASE_URL}\
|
||||
cargo check
|
||||
cd db/db-sqlx-maria &&\
|
||||
DATABASE_URL=${MARIA_DATABASE_URL}\
|
||||
cargo check
|
||||
cd db/db-core/ && cargo check
|
||||
|
||||
cache-bust: ## Run cache buster on static assets
|
||||
|
@ -105,6 +108,9 @@ sqlx-offline-data: ## prepare sqlx offline data
|
|||
cd db/db-sqlx-postgres && cargo sqlx prepare \
|
||||
--database-url=${POSTGRES_DATABASE_URL} -- \
|
||||
--all-features
|
||||
cd db/db-sqlx-maria && cargo sqlx prepare \
|
||||
--database-url=${MARIA_DATABASE_URL} -- \
|
||||
--all-features
|
||||
# cd db/db-sqlx-sqlite/ \
|
||||
# && DATABASE_URL=${SQLITE_DATABASE_URL} cargo sqlx prepare
|
||||
|
||||
|
@ -117,7 +123,11 @@ test: frontend-test frontend ## Run all available tests
|
|||
cd db/db-sqlx-postgres &&\
|
||||
DATABASE_URL=${POSTGRES_DATABASE_URL}\
|
||||
cargo test --no-fail-fast
|
||||
cargo test --all-features --no-fail-fast
|
||||
cd db/db-sqlx-maria &&\
|
||||
DATABASE_URL=${MARIA_DATABASE_URL}\
|
||||
cargo test --no-fail-fast
|
||||
DATABASE_URL=${MARIA_DATABASE_URL} cargo test --no-fail-fast
|
||||
DATABASE_URL=${POSTGRES_DATABASE_URL} cargo test --no-fail-fast
|
||||
# ./scripts/tests.sh
|
||||
|
||||
xml-test-coverage: migrate ## Generate code coverage report in XML format
|
||||
|
|
23
src/data.rs
23
src/data.rs
|
@ -19,8 +19,6 @@ use std::thread;
|
|||
|
||||
use actix::prelude::*;
|
||||
use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
|
||||
use db_core::prelude::*;
|
||||
use db_sqlx_postgres::{ConnectionOptions, Fresh};
|
||||
use lettre::transport::smtp::authentication::Mechanism;
|
||||
use lettre::{
|
||||
transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor,
|
||||
|
@ -40,10 +38,8 @@ use libmcaptcha::{
|
|||
pow::Work,
|
||||
system::{System, SystemBuilder},
|
||||
};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
|
||||
use db_core::MCDatabase;
|
||||
|
||||
use crate::db::{self, BoxDB};
|
||||
use crate::errors::ServiceResult;
|
||||
use crate::settings::Settings;
|
||||
use crate::stats::{Dummy, Real, Stats};
|
||||
|
@ -149,7 +145,7 @@ impl SystemGroup {
|
|||
/// App data
|
||||
pub struct Data {
|
||||
/// database ops defined by db crates
|
||||
pub db: Box<dyn MCDatabase>,
|
||||
pub db: BoxDB,
|
||||
/// credential management configuration
|
||||
pub creds: Config,
|
||||
/// mCaptcha system: Redis cache, etc.
|
||||
|
@ -185,15 +181,10 @@ impl Data {
|
|||
log::info!("Initialized credential manager");
|
||||
});
|
||||
|
||||
let pool = s.database.pool;
|
||||
let pool_options = PgPoolOptions::new().max_connections(pool);
|
||||
let connection_options = ConnectionOptions::Fresh(Fresh {
|
||||
pool_options,
|
||||
url: s.database.url.clone(),
|
||||
disable_logging: !s.debug,
|
||||
});
|
||||
let db = connection_options.connect().await.unwrap();
|
||||
db.migrate().await.unwrap();
|
||||
let db = match s.database.database_type {
|
||||
crate::settings::DBType::Maria => db::maria::get_data(Some(s.clone())).await,
|
||||
crate::settings::DBType::Postgres => db::pg::get_data(Some(s.clone())).await,
|
||||
};
|
||||
|
||||
let stats: Box<dyn Stats> = if s.captcha.enable_stats {
|
||||
Box::new(Real::default())
|
||||
|
@ -203,7 +194,7 @@ impl Data {
|
|||
|
||||
let data = Data {
|
||||
creds,
|
||||
db: Box::new(db),
|
||||
db,
|
||||
captcha: SystemGroup::new(s).await,
|
||||
mailer: Self::get_mailer(s),
|
||||
settings: s.clone(),
|
||||
|
|
60
src/db.rs
Normal file
60
src/db.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
use crate::settings::Settings;
|
||||
use db_core::prelude::*;
|
||||
|
||||
pub type BoxDB = Box<dyn MCDatabase>;
|
||||
|
||||
pub mod pg {
|
||||
use super::*;
|
||||
use db_sqlx_postgres::{ConnectionOptions, Fresh};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
|
||||
pub async fn get_data(settings: Option<Settings>) -> BoxDB {
|
||||
let settings = settings.unwrap_or_else(|| Settings::new().unwrap());
|
||||
let pool = settings.database.pool;
|
||||
let pool_options = PgPoolOptions::new().max_connections(pool);
|
||||
let connection_options = ConnectionOptions::Fresh(Fresh {
|
||||
pool_options,
|
||||
url: settings.database.url.clone(),
|
||||
disable_logging: !settings.debug,
|
||||
});
|
||||
let db = connection_options.connect().await.unwrap();
|
||||
db.migrate().await.unwrap();
|
||||
Box::new(db)
|
||||
}
|
||||
}
|
||||
|
||||
pub mod maria {
|
||||
use super::*;
|
||||
use db_sqlx_maria::{ConnectionOptions, Fresh};
|
||||
use sqlx::mysql::MySqlPoolOptions;
|
||||
|
||||
pub async fn get_data(settings: Option<Settings>) -> BoxDB {
|
||||
let settings = settings.unwrap_or_else(|| Settings::new().unwrap());
|
||||
let pool = settings.database.pool;
|
||||
let pool_options = MySqlPoolOptions::new().max_connections(pool);
|
||||
let connection_options = ConnectionOptions::Fresh(Fresh {
|
||||
pool_options,
|
||||
url: settings.database.url.clone(),
|
||||
disable_logging: !settings.debug,
|
||||
});
|
||||
let db = connection_options.connect().await.unwrap();
|
||||
db.migrate().await.unwrap();
|
||||
Box::new(db)
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ use log::info;
|
|||
mod api;
|
||||
mod data;
|
||||
mod date;
|
||||
mod db;
|
||||
mod demo;
|
||||
mod docs;
|
||||
mod email;
|
||||
|
|
Loading…
Add table
Reference in a new issue