feat: extra debug logging and chore: fix unused

This commit is contained in:
hexlocation 2025-07-29 12:53:21 +02:00
parent a6b2127b0c
commit e2d9789f9a
6 changed files with 80 additions and 47 deletions

View file

@ -45,45 +45,16 @@ impl Client {
.nth(0)
.unwrap();
return bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap();
bcrypt::verify(secret, client.hashed_secret.as_str()).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Host {
pub hostname: String,
pub address: String,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Config {
pub db: Db,
pub database: String,
pub proxy: Proxy,
pub api: Api,
pub clients: Vec<Client>,
pub hosts: Vec<Host>,
}
impl Db {
pub async fn to_string(&self) -> String {
let mut builder = String::new();
builder += format!(
"host={} port={} user={} dbname={}",
self.host,
self.port.unwrap_or(5432),
self.user,
self.database.clone().unwrap_or(self.user.clone()),
)
.as_str();
match &self.password {
Some(x) => builder += format!(" password={}", x).as_str(),
None => {}
}
builder
}
}
impl Config {

View file

@ -3,12 +3,12 @@ mod db;
mod server;
mod services;
use std::{env, sync::Arc};
use std::{env, process::exit, sync::Arc};
use bcrypt::DEFAULT_COST;
use config::Config;
use db::BoxyDatabase;
use log::{error, info};
use log::{debug, error, info};
use server::Server;
use services::{api::ApiService, controller::ControllerService};
use tokio::sync::Mutex;
@ -49,20 +49,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let config = Config::get().await.unwrap();
let db_string = config.db.to_string().await;
debug!("Database URI: {}", config.database);
info!("Database string: {}", db_string);
let (client, conn) = tokio_postgres::connect(db_string.as_str(), NoTls)
let (client, conn) = tokio_postgres::connect(config.database.as_str(), NoTls)
.await
.unwrap();
tokio::spawn(async move {
if let Err(e) = conn.await {
error!("Error while connecting to database: {}", e);
exit(1);
}
});
info!("Connected to database.");
let database = Box::new(BoxyDatabase::new(client).await.unwrap());
let database_shared = Arc::new(Mutex::new(Box::leak(database)));
@ -86,10 +87,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.unwrap();
tokio::task::spawn(async move {
info!("Starting API server...");
api_server.handle().await;
});
// We don't put this on a separate thread because we'd be wasting the main thread.
info!("Starting proxy server...");
proxy_server.handle().await;
Ok(())

View file

@ -1,4 +1,4 @@
use std::error::Error;
use std::{any::type_name_of_val, error::Error};
use http_body_util::{Either, Full};
use hyper::{
@ -8,7 +8,7 @@ use hyper::{
service::{HttpService, Service},
};
use hyper_util::rt::TokioIo;
use log::error;
use log::{error, info};
use tokio::net::{TcpListener, TcpStream};
pub type GeneralResponse = Response<GeneralBody>;
@ -39,6 +39,12 @@ where
<S as HttpService<Incoming>>::Future: Send,
{
pub async fn handle(&self) {
info!(
"Server started at http://{} for service: {}",
self.listener.local_addr().unwrap(),
type_name_of_val(&self.service)
);
loop {
let (stream, _) = self.listener.accept().await.unwrap();
@ -46,6 +52,7 @@ where
svc_clone.stream(&stream);
let io = TokioIo::new(stream);
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.writev(false)