Improve logging and set .cfg config file ending

This commit is contained in:
Tobias Reisinger 2026-08-31 20:44:08 +02:00
commit 6a6bb2a4ce
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
6 changed files with 137 additions and 65 deletions

View file

@ -31,7 +31,7 @@ use crate::errors::{DatabaseError, EmgauwaError};
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"
pub async fn run_migrations(pool: &Pool<Sqlite>) -> Result<(), EmgauwaError> {
log::info!("Running migrations");
log::debug!("Running migrations");
MIGRATOR.run(pool).await.map_err(DatabaseError::from)?;
Ok(())
}
@ -41,9 +41,12 @@ pub async fn init(db: &str, pool_size: u32) -> Result<Pool<Sqlite>, EmgauwaError
.create_if_missing(true)
.log_statements(log::LevelFilter::Trace);
log::debug!("Initializing DB pool");
let pool: Pool<Sqlite> = SqlitePoolOptions::new()
.acquire_timeout(std::time::Duration::from_secs(1))
.max_connections(pool_size)
.idle_timeout(std::time::Duration::from_secs(5))
.max_lifetime(std::time::Duration::from_secs(20))
.connect_with(options)
.await?;
@ -54,5 +57,7 @@ pub async fn init(db: &str, pool_size: u32) -> Result<Pool<Sqlite>, EmgauwaError
DbSchedule::get_on(&mut pool_conn).await?;
DbSchedule::get_off(&mut pool_conn).await?;
pool_conn.close().await?;
Ok(pool)
}

View file

@ -1,6 +1,5 @@
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::io::ErrorKind;
use actix::MailboxError;
use actix_web::http::StatusCode;
@ -8,7 +7,8 @@ use actix_web::HttpResponse;
use config::ConfigError;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use tokio::sync::mpsc::error::SendError;
use tokio::sync::oneshot::error::RecvError;
use crate::errors::{ApiError, DatabaseError};
use crate::types::EmgauwaUid;
@ -45,13 +45,13 @@ impl From<&EmgauwaError> for String {
fn from(err: &EmgauwaError) -> Self {
match err {
EmgauwaError::Api(err) => String::from(err),
EmgauwaError::Unauthorized(_) => format!("unauthorized request: {}", err),
EmgauwaError::Unauthorized(err) => format!("unauthorized request: {}", err),
EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"),
EmgauwaError::Database(err) => String::from(err),
EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),
EmgauwaError::Internal(_) => String::from("internal error"),
EmgauwaError::Connection(uid) => {
format!("unable to connect to controller with uid: {}", uid)
format!("unable to connect to the controller with uid: {}", uid)
}
EmgauwaError::Other(err) => format!("other error: {}", err),
EmgauwaError::Hardware(err) => format!("hardware error: {}", err),
@ -101,6 +101,18 @@ impl From<ConfigError> for EmgauwaError {
}
}
impl<T> From<SendError<T>> for EmgauwaError {
fn from(value: SendError<T>) -> Self {
Self::Internal(value.to_string())
}
}
impl From<RecvError> for EmgauwaError {
fn from(value: RecvError) -> Self {
Self::Internal(value.to_string())
}
}
impl From<&EmgauwaError> for HttpResponse {
fn from(err: &EmgauwaError) -> Self {
HttpResponse::build(err.get_code()).json(err)
@ -111,7 +123,7 @@ impl Error for EmgauwaError {}
impl From<EmgauwaError> for std::io::Error {
fn from(value: EmgauwaError) -> Self {
std::io::Error::new(ErrorKind::Other, value)
std::io::Error::other(value)
}
}

View file

@ -50,8 +50,8 @@ where
for<'de> T: serde::Deserialize<'de>,
{
let etc_file =
config::File::with_name(&format!("/etc/emgauwa/{}", config_name)).required(false);
let local_file = config::File::with_name(&format!("./emgauwa-{}", config_name)).required(false);
config::File::with_name(&format!("/etc/emgauwa/{}.cfg", config_name)).required(false);
let local_file = config::File::with_name(&format!("./emgauwa-{}.cfg", config_name)).required(false);
config::Config::builder()
.add_source(etc_file)

View file

@ -18,7 +18,7 @@ use crate::models::{Controller, Relay};
#[derive(Debug, Serialize, Deserialize, Message)]
#[rtype(result = "Result<(), EmgauwaError>")]
pub enum ControllerWsAction {
pub enum ControllersWsAction {
Register(Controller),
Disconnect,
Schedules(Vec<DbSchedule>),