mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 23:31:24 +01:00
distinct error types for running server
This is in preparation for the config change to allow specifying multiple listeners, which will add several other possible error conditions.
This commit is contained in:
parent
76b060aa84
commit
b7ad00ef6e
2 changed files with 32 additions and 8 deletions
20
src/error.rs
20
src/error.rs
|
|
@ -48,7 +48,7 @@ pub(crate) enum Main {
|
|||
DatabaseError(#[source] crate::utils::error::Error),
|
||||
|
||||
#[error("failed to serve requests")]
|
||||
Serve(#[source] std::io::Error),
|
||||
Serve(#[from] Serve),
|
||||
}
|
||||
|
||||
/// Observability initialization errors
|
||||
|
|
@ -97,3 +97,21 @@ pub(crate) enum ConfigSearch {
|
|||
#[error("no relevant configuration files found in XDG Base Directories")]
|
||||
NotFound,
|
||||
}
|
||||
|
||||
/// Errors serving traffic
|
||||
// Missing docs are allowed here since that kind of information should be
|
||||
// encoded in the error messages themselves anyway.
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Error, Debug)]
|
||||
pub(crate) enum Serve {
|
||||
#[error("failed to read TLS cert and key files at {certs:?} and {key:?}")]
|
||||
LoadCerts {
|
||||
certs: String,
|
||||
key: String,
|
||||
#[source]
|
||||
err: std::io::Error,
|
||||
},
|
||||
|
||||
#[error("failed to run request listener")]
|
||||
Listen(#[source] std::io::Error),
|
||||
}
|
||||
|
|
|
|||
20
src/main.rs
20
src/main.rs
|
|
@ -1,6 +1,5 @@
|
|||
use std::{
|
||||
future::Future,
|
||||
io,
|
||||
net::SocketAddr,
|
||||
process::ExitCode,
|
||||
sync::{atomic, RwLock},
|
||||
|
|
@ -127,12 +126,14 @@ async fn try_main() -> Result<(), error::Main> {
|
|||
.map_err(Error::DatabaseError)?;
|
||||
|
||||
info!("Starting server");
|
||||
run_server().await.map_err(Error::Serve)?;
|
||||
run_server().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_server() -> io::Result<()> {
|
||||
async fn run_server() -> Result<(), error::Serve> {
|
||||
use error::Serve as Error;
|
||||
|
||||
let config = &services().globals.config;
|
||||
let addr = SocketAddr::from((config.address, config.port));
|
||||
|
||||
|
|
@ -189,15 +190,20 @@ async fn run_server() -> io::Result<()> {
|
|||
|
||||
match &config.tls {
|
||||
Some(tls) => {
|
||||
let conf =
|
||||
RustlsConfig::from_pem_file(&tls.certs, &tls.key).await?;
|
||||
let conf = RustlsConfig::from_pem_file(&tls.certs, &tls.key)
|
||||
.await
|
||||
.map_err(|err| Error::LoadCerts {
|
||||
certs: tls.certs.clone(),
|
||||
key: tls.key.clone(),
|
||||
err,
|
||||
})?;
|
||||
let server = bind_rustls(addr, conf).handle(handle).serve(app);
|
||||
|
||||
#[cfg(feature = "systemd")]
|
||||
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
|
||||
.expect("should be able to notify systemd");
|
||||
|
||||
server.await?;
|
||||
server.await.map_err(Error::Listen)?;
|
||||
}
|
||||
None => {
|
||||
let server = bind(addr).handle(handle).serve(app);
|
||||
|
|
@ -206,7 +212,7 @@ async fn run_server() -> io::Result<()> {
|
|||
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
|
||||
.expect("should be able to notify systemd");
|
||||
|
||||
server.await?;
|
||||
server.await.map_err(Error::Listen)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue