-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rs
More file actions
104 lines (86 loc) · 3.74 KB
/
main.rs
File metadata and controls
104 lines (86 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use api::{create_router_with_cors, ApiDoc, AppState};
use services::{
auth::OAuthServiceImpl, conversation::service::ConversationServiceImpl,
response::service::OpenAIProxy, user::UserServiceImpl,
};
use std::sync::Arc;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Load .env file if it exists
if let Err(e) = dotenvy::dotenv() {
eprintln!("Warning: Could not load .env file: {e}");
eprintln!("Continuing with environment variables...");
}
// Initialize tracing
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "info,api=debug,services=debug,database=debug".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
tracing::info!("Starting API server...");
// Load configuration from environment
let config = config::Config::from_env();
tracing::info!(
"Database: {}:{}/{}",
config.database.host,
config.database.port,
config.database.database
);
tracing::info!("Server: {}:{}", config.server.host, config.server.port);
// Create database and run migrations
tracing::info!("Connecting to database...");
let db = database::Database::from_config(&config.database).await?;
tracing::info!("Running migrations...");
db.run_migrations().await?;
// Get repositories
let user_repo = db.user_repository();
let session_repo = db.session_repository();
let oauth_repo = db.oauth_repository();
let conversation_repo = db.conversation_repository();
// Create services
tracing::info!("Initializing services...");
let oauth_service = Arc::new(OAuthServiceImpl::new(
oauth_repo.clone(),
session_repo.clone(),
user_repo.clone(),
config.oauth.google_client_id.clone(),
config.oauth.google_client_secret.clone(),
config.oauth.github_client_id.clone(),
config.oauth.github_client_secret.clone(),
config.oauth.redirect_uri.clone(),
));
let user_service = Arc::new(UserServiceImpl::new(user_repo));
// Initialize OpenAI proxy service
let mut proxy_service = OpenAIProxy::new(config.openai.api_key.clone());
if let Some(base_url) = config.openai.base_url.clone() {
proxy_service = proxy_service.with_base_url(base_url);
}
let proxy_service = Arc::new(proxy_service);
// Initialize conversation service
let conversation_service = Arc::new(ConversationServiceImpl::new(conversation_repo));
// Create application state
let app_state = AppState {
oauth_service: oauth_service as Arc<dyn services::auth::ports::OAuthService>,
user_service: user_service as Arc<dyn services::user::ports::UserService>,
session_repository: session_repo,
proxy_service: proxy_service as Arc<dyn services::response::ports::OpenAIProxyService>,
conversation_service: conversation_service
as Arc<dyn services::conversation::ports::ConversationService>,
redirect_uri: config.oauth.redirect_uri.clone(),
};
// Create router with CORS support
let app = create_router_with_cors(app_state, config.cors.allowed_origins.clone())
.merge(SwaggerUi::new("/docs").url("/api-docs/openapi.json", ApiDoc::openapi()));
// Start server
let addr = format!("{}:{}", config.server.host, config.server.port);
let listener = tokio::net::TcpListener::bind(&addr).await?;
tracing::info!("🚀 Server listening on http://{}", addr);
tracing::info!("📚 Swagger UI available at http://{}/docs", addr);
axum::serve(listener, app).await?;
Ok(())
}