add tracing, env config, dotenvy

bootstrap: tracing-subscriber with RUST_LOG env filter, ServerConfig
from env vars (KFRAME_DATABASE_URL, KFRAME_TCP_ADDR, etc.), dotenvy
for .env file loading. Replaced all println with tracing macros.

tcp-server: replaced println with tracing::info/warn.

Added .env.example and .gitignore for db files.
This commit is contained in:
2026-06-18 23:14:43 +02:00
parent 15b75d860c
commit 21c08911df
10 changed files with 188 additions and 20 deletions

View File

@@ -1,3 +1,4 @@
mod config;
mod polling;
use std::sync::Arc;
@@ -7,34 +8,50 @@ use config_sqlite::SqliteConfigStore;
use tcp_server::{TcpBroadcaster, TcpEventBus, run_tcp_server};
use http_api::AppState;
use tokio::sync::Mutex;
const DB_PATH: &str = "sqlite:kframe.db?mode=rwc";
const TCP_ADDR: &str = "0.0.0.0:2699";
const HTTP_ADDR: &str = "0.0.0.0:3000";
use tracing::{info, error};
#[tokio::main]
async fn main() -> Result<()> {
let config_store = Arc::new(SqliteConfigStore::new(DB_PATH).await?);
dotenvy::dotenv().ok();
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "info,sqlx=warn".into()),
)
.init();
let cfg = config::ServerConfig::from_env();
info!(db = %cfg.database_url, "connecting to database");
let config_store = Arc::new(SqliteConfigStore::new(&cfg.database_url).await?);
let event_bus = Arc::new(TcpEventBus::new(64));
let broadcaster = Arc::new(TcpBroadcaster::new(64));
let projection = Arc::new(Mutex::new(DataProjection::new()));
let tcp_addr = cfg.tcp_addr.clone();
let tcp_bc = broadcaster.clone();
tokio::spawn(async move {
run_tcp_server(TCP_ADDR, tcp_bc).await.unwrap();
if let Err(e) = run_tcp_server(&tcp_addr, tcp_bc).await {
error!(error = %e, "tcp server failed");
}
});
println!("TCP server on {TCP_ADDR}");
info!(addr = %cfg.tcp_addr, "TCP server started");
let http_addr = cfg.http_addr.clone();
let http_state = AppState {
config: config_store.clone(),
events: event_bus.clone(),
};
tokio::spawn(async move {
http_api::serve(HTTP_ADDR, http_state).await.unwrap();
if let Err(e) = http_api::serve(&http_addr, http_state).await {
error!(error = %e, "HTTP API failed");
}
});
println!("HTTP API on {HTTP_ADDR}");
info!(addr = %cfg.http_addr, "HTTP API started");
println!("K-Frame server running");
info!("K-Frame server running");
polling::run(config_store, broadcaster, projection).await
polling::run(config_store, broadcaster, projection, cfg.poll_interval_secs).await
}