2024-12-25 17:16:05 -03:00
|
|
|
use gb_core::{Error, Result};
|
|
|
|
use tracing::{info, error};
|
2024-12-25 21:28:16 -03:00
|
|
|
use axum::Router;
|
|
|
|
use std::net::SocketAddr;
|
2024-12-25 17:16:05 -03:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
// Initialize logging first
|
|
|
|
init_logging()?;
|
|
|
|
|
|
|
|
// Initialize core components
|
|
|
|
let app = initialize_bot_server().await?;
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
start_server(app).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn initialize_bot_server() -> Result<axum::Router> {
|
|
|
|
info!("Initializing General Bots...");
|
|
|
|
|
|
|
|
// Initialize database connections
|
|
|
|
let db_pool = initialize_database().await?;
|
|
|
|
|
|
|
|
// Initialize Redis
|
|
|
|
let redis_client = initialize_redis().await?;
|
|
|
|
|
|
|
|
// Build the Axum router with our routes
|
|
|
|
let app = axum::Router::new()
|
|
|
|
.with_state(AppState {
|
|
|
|
db: db_pool,
|
|
|
|
redis: redis_client,
|
|
|
|
})
|
|
|
|
// Add your route handlers here
|
|
|
|
.layer(tower_http::trace::TraceLayer::new_for_http());
|
|
|
|
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_logging() -> Result<()> {
|
2024-12-26 10:09:39 -03:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2024-12-25 17:16:05 -03:00
|
|
|
|
|
|
|
let env_filter = EnvFilter::try_from_default_env()
|
|
|
|
.unwrap_or_else(|_| EnvFilter::new("info"));
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(env_filter)
|
|
|
|
.with_file(true)
|
|
|
|
.with_line_number(true)
|
|
|
|
.with_thread_ids(true)
|
|
|
|
.init();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn initialize_database() -> Result<sqlx::PgPool> {
|
|
|
|
let database_url = std::env::var("DATABASE_URL")
|
2024-12-25 21:28:16 -03:00
|
|
|
.map_err(|_| Error::internal("DATABASE_URL not set".to_string()))?;
|
2024-12-25 17:16:05 -03:00
|
|
|
|
|
|
|
sqlx::PgPool::connect(&database_url)
|
|
|
|
.await
|
2024-12-25 21:28:16 -03:00
|
|
|
.map_err(|e| Error::internal(e.to_string()))
|
2024-12-25 17:16:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn initialize_redis() -> Result<redis::Client> {
|
|
|
|
let redis_url = std::env::var("REDIS_URL")
|
2024-12-25 21:28:16 -03:00
|
|
|
.map_err(|_| Error::internal("REDIS_URL not set".to_string()))?;
|
2024-12-25 17:16:05 -03:00
|
|
|
|
|
|
|
redis::Client::open(redis_url)
|
2024-12-25 21:28:16 -03:00
|
|
|
.map_err(|e| Error::internal(e.to_string()))
|
2024-12-25 17:16:05 -03:00
|
|
|
}
|
|
|
|
|
2024-12-26 10:09:39 -03:00
|
|
|
#[allow(dead_code)]
|
2024-12-25 17:16:05 -03:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
|
|
|
db: sqlx::PgPool,
|
|
|
|
redis: redis::Client,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-25 21:28:16 -03:00
|
|
|
async fn start_server(app: Router) -> Result<()> {
|
|
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
|
|
|
info!("Starting server on {}", addr);
|
2024-12-25 17:16:05 -03:00
|
|
|
|
2024-12-25 21:28:16 -03:00
|
|
|
match tokio::net::TcpListener::bind(addr).await {
|
|
|
|
Ok(listener) => {
|
|
|
|
info!("Listening on {}", addr);
|
|
|
|
axum::serve(listener, app)
|
|
|
|
.await
|
|
|
|
.map_err(|e| Error::internal(format!("Server error: {}", e)))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to bind to address: {}", e);
|
|
|
|
Err(Error::internal(format!("Failed to bind to address: {}", e)))
|
|
|
|
}
|
|
|
|
}
|
2024-12-25 17:16:05 -03:00
|
|
|
}
|