Files
kakeibo/backend/src/main.rs
T
suphicakir 5550c7c43d feat(backend): migration mekanizması, indeksler ve ortak model yardımcıları
- db/migrations.rs: schema_migrations ile sürümlü migration (v1: koleksiyonlar)
- db/indexes.rs: DATA_MODEL indeksleri + senkronizasyon indeksi, açılışta otomatik
- models/common.rs: UUID v7, ortak alanlar, hane kapsamlı filtre
- Birim testleri (4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:25:55 +03:00

78 lines
2.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
mod config;
mod db;
#[allow(dead_code)] // AppError, Faz 1 – Adım 3 itibarıyla route'larda kullanılacak
mod error;
mod models;
mod routes;
mod state;
use std::{net::SocketAddr, process::ExitCode, sync::Arc};
use tracing_subscriber::EnvFilter;
use crate::{config::Config, state::AppState};
#[tokio::main]
async fn main() -> ExitCode {
// .env dosyasını yükle (proje kökünde; üst klasörler de aranır). Yoksa sorun değil.
dotenvy::dotenv().ok();
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("kakeibo=info")),
)
.init();
let config = match Config::from_env() {
Ok(c) => c,
Err(e) => {
tracing::error!("Ayar hatası: {e}");
return ExitCode::FAILURE;
}
};
let db = match db::connect(&config.mongodb_uri).await {
Ok(db) => {
tracing::info!("MongoDB bağlantısı kuruldu (veritabanı: {})", db.name());
db
}
Err(e) => {
tracing::error!(
"MongoDB'ye bağlanılamadı. MONGODB_URI doğru mu, veritabanı çalışıyor mu? \
(yerelde: npm run db:up)\nDetay: {e}"
);
return ExitCode::FAILURE;
}
};
if let Err(e) = db::prepare(&db).await {
tracing::error!("Veritabanı hazırlanamadı (migration/indeks): {e}");
return ExitCode::FAILURE;
}
let port = config.port;
let state = AppState {
db,
config: Arc::new(config),
};
let app = routes::router(state);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let listener = match tokio::net::TcpListener::bind(addr).await {
Ok(l) => l,
Err(e) => {
tracing::error!(
"{port} portu dinlenemedi (başka bir uygulama kullanıyor olabilir): {e}"
);
return ExitCode::FAILURE;
}
};
tracing::info!("Kakeibo API çalışıyor: http://localhost:{port}");
if let Err(e) = axum::serve(listener, app).await {
tracing::error!("Sunucu hatası: {e}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}