You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-08-09 04:22:45 +03:00
Remove the dependency on sqlx in the config crate
This commit is contained in:
@@ -17,6 +17,8 @@ use clap::Parser;
|
||||
use mas_config::DatabaseConfig;
|
||||
use mas_storage::MIGRATOR;
|
||||
|
||||
use crate::util::database_from_config;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub(super) struct Options {
|
||||
#[command(subcommand)]
|
||||
@@ -32,7 +34,7 @@ enum Subcommand {
|
||||
impl Options {
|
||||
pub async fn run(&self, root: &super::Options) -> anyhow::Result<()> {
|
||||
let config: DatabaseConfig = root.load_config()?;
|
||||
let pool = config.connect().await?;
|
||||
let pool = database_from_config(&config).await?;
|
||||
|
||||
// Run pending migrations
|
||||
MIGRATOR
|
||||
|
@@ -28,7 +28,7 @@ use oauth2_types::scope::Scope;
|
||||
use rand::SeedableRng;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::util::password_manager_from_config;
|
||||
use crate::util::{database_from_config, password_manager_from_config};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub(super) struct Options {
|
||||
@@ -197,7 +197,7 @@ impl Options {
|
||||
let database_config: DatabaseConfig = root.load_config()?;
|
||||
let passwords_config: PasswordsConfig = root.load_config()?;
|
||||
|
||||
let pool = database_config.connect().await?;
|
||||
let pool = database_from_config(&database_config).await?;
|
||||
let password_manager = password_manager_from_config(&passwords_config).await?;
|
||||
|
||||
let mut txn = pool.begin().await?;
|
||||
@@ -228,7 +228,7 @@ impl Options {
|
||||
|
||||
SC::VerifyEmail { username, email } => {
|
||||
let config: DatabaseConfig = root.load_config()?;
|
||||
let pool = config.connect().await?;
|
||||
let pool = database_from_config(&config).await?;
|
||||
let mut txn = pool.begin().await?;
|
||||
|
||||
let user = lookup_user_by_username(&mut txn, username)
|
||||
@@ -247,7 +247,7 @@ impl Options {
|
||||
|
||||
SC::ImportClients { truncate } => {
|
||||
let config: RootConfig = root.load_config()?;
|
||||
let pool = config.database.connect().await?;
|
||||
let pool = database_from_config(&config.database).await?;
|
||||
let encrypter = config.secrets.encrypter();
|
||||
|
||||
let mut txn = pool.begin().await?;
|
||||
@@ -306,7 +306,7 @@ impl Options {
|
||||
} => {
|
||||
let config: RootConfig = root.load_config()?;
|
||||
let encrypter = config.secrets.encrypter();
|
||||
let pool = config.database.connect().await?;
|
||||
let pool = database_from_config(&config.database).await?;
|
||||
let url_builder = UrlBuilder::new(config.http.public_base);
|
||||
let mut conn = pool.acquire().await?;
|
||||
|
||||
|
@@ -28,7 +28,10 @@ use mas_templates::Templates;
|
||||
use tokio::signal::unix::SignalKind;
|
||||
use tracing::{error, info, log::warn};
|
||||
|
||||
use crate::util::{mailer_from_config, password_manager_from_config, policy_factory_from_config};
|
||||
use crate::util::{
|
||||
database_from_config, mailer_from_config, password_manager_from_config,
|
||||
policy_factory_from_config, templates_from_config,
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug, Default)]
|
||||
pub(super) struct Options {
|
||||
@@ -105,7 +108,7 @@ impl Options {
|
||||
let config: RootConfig = root.load_config()?;
|
||||
|
||||
// Connect to the database
|
||||
let pool = config.database.connect().await?;
|
||||
let pool = database_from_config(&config.database).await?;
|
||||
|
||||
if self.migrate {
|
||||
info!("Running pending migrations");
|
||||
@@ -120,7 +123,6 @@ impl Options {
|
||||
queue.recuring(Duration::from_secs(15), mas_tasks::cleanup_expired(&pool));
|
||||
queue.start();
|
||||
|
||||
// TODO: task queue, key store, encrypter, url builder, http client
|
||||
// Initialize the key store
|
||||
let key_store = config
|
||||
.secrets
|
||||
@@ -138,9 +140,7 @@ impl Options {
|
||||
let url_builder = UrlBuilder::new(config.http.public_base.clone());
|
||||
|
||||
// Load and compile the templates
|
||||
let templates = Templates::load(config.templates.path.clone(), url_builder.clone())
|
||||
.await
|
||||
.context("could not load templates")?;
|
||||
let templates = templates_from_config(&config.templates, &url_builder).await?;
|
||||
|
||||
let mailer = mailer_from_config(&config.email, &templates).await?;
|
||||
mailer.test_connection().await?;
|
||||
|
@@ -12,12 +12,23 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
use mas_config::{EmailConfig, EmailSmtpMode, EmailTransportConfig, PasswordsConfig, PolicyConfig};
|
||||
use mas_config::{
|
||||
DatabaseConfig, DatabaseConnectConfig, EmailConfig, EmailSmtpMode, EmailTransportConfig,
|
||||
PasswordsConfig, PolicyConfig, TemplatesConfig,
|
||||
};
|
||||
use mas_email::{MailTransport, Mailer};
|
||||
use mas_handlers::passwords::PasswordManager;
|
||||
use mas_policy::PolicyFactory;
|
||||
use mas_templates::Templates;
|
||||
use mas_router::UrlBuilder;
|
||||
use mas_templates::{TemplateLoadingError, Templates};
|
||||
use sqlx::{
|
||||
postgres::{PgConnectOptions, PgPoolOptions},
|
||||
ConnectOptions, PgPool,
|
||||
};
|
||||
use tracing::log::LevelFilter;
|
||||
|
||||
pub async fn password_manager_from_config(
|
||||
config: &PasswordsConfig,
|
||||
@@ -91,3 +102,69 @@ pub async fn policy_factory_from_config(
|
||||
.await
|
||||
.context("failed to load the policy")
|
||||
}
|
||||
|
||||
pub async fn templates_from_config(
|
||||
config: &TemplatesConfig,
|
||||
url_builder: &UrlBuilder,
|
||||
) -> Result<Templates, TemplateLoadingError> {
|
||||
Templates::load(config.path.clone(), url_builder.clone()).await
|
||||
}
|
||||
|
||||
pub async fn database_from_config(config: &DatabaseConfig) -> Result<PgPool, anyhow::Error> {
|
||||
let mut options = match &config.options {
|
||||
DatabaseConnectConfig::Uri { uri } => uri
|
||||
.parse()
|
||||
.context("could not parse database connection string")?,
|
||||
DatabaseConnectConfig::Options {
|
||||
host,
|
||||
port,
|
||||
socket,
|
||||
username,
|
||||
password,
|
||||
database,
|
||||
} => {
|
||||
let mut opts =
|
||||
PgConnectOptions::new().application_name("matrix-authentication-service");
|
||||
|
||||
if let Some(host) = host {
|
||||
opts = opts.host(host);
|
||||
}
|
||||
|
||||
if let Some(port) = port {
|
||||
opts = opts.port(*port);
|
||||
}
|
||||
|
||||
if let Some(socket) = socket {
|
||||
opts = opts.socket(socket);
|
||||
}
|
||||
|
||||
if let Some(username) = username {
|
||||
opts = opts.username(username);
|
||||
}
|
||||
|
||||
if let Some(password) = password {
|
||||
opts = opts.password(password);
|
||||
}
|
||||
|
||||
if let Some(database) = database {
|
||||
opts = opts.database(database);
|
||||
}
|
||||
|
||||
opts
|
||||
}
|
||||
};
|
||||
|
||||
options
|
||||
.log_statements(LevelFilter::Debug)
|
||||
.log_slow_statements(LevelFilter::Warn, Duration::from_millis(100));
|
||||
|
||||
PgPoolOptions::new()
|
||||
.max_connections(config.max_connections.into())
|
||||
.min_connections(config.min_connections)
|
||||
.acquire_timeout(config.connect_timeout)
|
||||
.idle_timeout(config.idle_timeout)
|
||||
.max_lifetime(config.max_lifetime)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.context("could not connect to the database")
|
||||
}
|
||||
|
Reference in New Issue
Block a user