1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Setup cargo-deny

Also try to remove a bunch of duplicate crates
This commit is contained in:
Quentin Gliech
2023-08-14 10:23:33 +02:00
parent 4280045b24
commit 21964cbeab
21 changed files with 277 additions and 410 deletions

View File

@ -11,7 +11,7 @@ anyhow = "1.0.72"
axum = "0.6.20"
camino = "1.1.6"
clap = { version = "4.3.21", features = ["derive"] }
dotenv = "0.15.0"
dotenvy = "0.15.7"
httpdate = "1.0.2"
hyper = { version = "0.14.27", features = ["full"] }
itertools = "0.11.0"
@ -26,7 +26,6 @@ tokio = { version = "1.30.0", features = ["full"] }
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.4.3", features = ["fs"] }
url = "2.4.0"
watchman_client = "0.8.0"
zeroize = "1.6.0"
tracing = "0.1.37"

View File

@ -32,7 +32,7 @@ use tracing::{info, info_span, warn, Instrument};
use crate::util::{
database_from_config, mailer_from_config, password_manager_from_config,
policy_factory_from_config, templates_from_config, watch_templates,
policy_factory_from_config, register_sighup, templates_from_config,
};
#[derive(Parser, Debug, Default)]
@ -44,10 +44,6 @@ pub(super) struct Options {
/// Do not start the task worker
#[arg(long)]
no_worker: bool,
/// Watch for changes for templates on the filesystem
#[arg(short, long)]
watch: bool,
}
impl Options {
@ -134,10 +130,8 @@ impl Options {
// Explicitly the config to properly zeroize secret keys
drop(config);
// Watch for changes in templates if the --watch flag is present
if self.watch {
watch_templates(&templates).await?;
}
// Listen for SIGHUP
register_sighup(&templates)?;
let graphql_schema = mas_handlers::graphql_schema(&pool, conn);

View File

@ -48,7 +48,7 @@ async fn main() -> anyhow::Result<()> {
async fn try_main() -> anyhow::Result<()> {
// Load environment variables from .env files
// We keep the path to log it afterwards
let dotenv_path: Result<Option<_>, _> = dotenv::dotenv()
let dotenv_path: Result<Option<_>, _> = dotenvy::dotenv()
.map(Some)
// Display the error if it is something other than the .env file not existing
.or_else(|e| if e.not_found() { Ok(None) } else { Err(e) });

View File

@ -179,60 +179,27 @@ pub async fn database_from_config(config: &DatabaseConfig) -> Result<PgPool, any
.context("could not connect to the database")
}
/// Watch for changes in the templates folders
pub async fn watch_templates(templates: &Templates) -> anyhow::Result<()> {
use watchman_client::{prelude::*, SubscriptionData};
let client = Connector::new()
.connect()
.await
.context("could not connect to watchman")?;
let templates = templates.clone();
// Find which root we're supposed to watch
let root = templates.watch_root();
// Create a subscription on the root
let resolved = client
.resolve_root(CanonicalPath::canonicalize(root)?)
.await?;
// Only look for *.txt, *.html and *.subject files
let request = SubscribeRequest {
expression: Some(Expr::Suffix(vec![
"txt".into(),
"html".into(),
"subject".into(),
])),
..SubscribeRequest::default()
};
let (mut subscription, _) = client.subscribe::<NameOnly>(&resolved, request).await?;
tokio::spawn(async move {
loop {
let event = match subscription.next().await {
Ok(event) => event,
Err(error) => {
error!(%error, "Stopped watching templates because of an error in the watchman subscription");
/// Reload templates on SIGHUP
pub fn register_sighup(templates: &Templates) -> anyhow::Result<()> {
#[cfg(unix)]
{
let mut signal = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;
let templates = templates.clone();
tokio::spawn(async move {
loop {
if signal.recv().await.is_none() {
// No more signals will be received, breaking
break;
}
};
};
if let SubscriptionData::FilesChanged(QueryResult {
files: Some(files), ..
}) = event
{
let files: Vec<_> = files.into_iter().map(|f| f.name.into_inner()).collect();
info!(?files, "Files changed, reloading templates");
info!("SIGHUP received, reloading templates");
templates.clone().reload().await.unwrap_or_else(|err| {
error!(?err, "Error while reloading templates");
});
}
}
});
});
}
Ok(())
}