From df7c776edbe4a2318feacb5b92b6cf4435ab7987 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 25 Dec 2024 17:16:05 -0300 Subject: [PATCH] new(all): Initial import. --- .vscode/launch.json | 58 ++++++++++++++++++++++++++++++ Cargo.lock | 1 + gb-api/Cargo.toml | 3 +- gb-api/src/main.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json create mode 100644 gb-api/src/main.rs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4e4a8a7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,58 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug GB API Server", + "cargo": { + "args": ["build", "--bin=gb-api"], + "filter": { + "name": "gb-api", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}", + "env": { + "RUST_LOG": "debug", + "DATABASE_URL": "postgresql://localhost:5432/gbdb", + "REDIS_URL": "redis://localhost:6379" + } + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug GB API Tests", + "cargo": { + "args": ["test", "--package=gb-api", "--lib"], + "filter": { + "name": "gb-api", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug Integration Tests", + "cargo": { + "args": ["test", "--package=gb-api", "--test=integration"], + "filter": { + "name": "integration", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ], + "compounds": [ + { + "name": "API Server + Debug", + "configurations": ["Debug GB API Server"] + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c692234..ce6d2df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2363,6 +2363,7 @@ dependencies = [ "tower 0.4.13", "tower-http 0.5.2", "tracing", + "tracing-subscriber", "uuid", ] diff --git a/gb-api/Cargo.toml b/gb-api/Cargo.toml index 458a412..9c2c608 100644 --- a/gb-api/Cargo.toml +++ b/gb-api/Cargo.toml @@ -9,7 +9,7 @@ license = { workspace = true } gb-core = { path = "../gb-core" } gb-messaging = { path = "../gb-messaging" } gb-monitoring = { path = "../gb-monitoring" } -tokio= { workspace = true } +tokio = { version = "1.0", features = ["full", "macros", "rt-multi-thread"] } # Add these features axum = { version = "0.7.9", features = ["ws", "multipart", "macros"] } tower= { workspace = true } tower-http = { version = "0.5", features = ["cors", "trace"] } @@ -17,6 +17,7 @@ serde= { workspace = true } serde_json= { workspace = true } uuid= { workspace = true } tracing= { workspace = true } +tracing-subscriber = { version = "0.3", features = ["env-filter"] } async-trait= { workspace = true } futures-util = { version = "0.3", features = ["sink"] } chrono = { workspace = true, features = ["serde"] } diff --git a/gb-api/src/main.rs b/gb-api/src/main.rs new file mode 100644 index 0000000..003465e --- /dev/null +++ b/gb-api/src/main.rs @@ -0,0 +1,87 @@ +use gb_core::{Error, Result}; +use gb_core::models::Customer; +use tracing::{info, error}; + +#[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 { + 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<()> { + use tracing_subscriber::{EnvFilter, fmt}; + + 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 { + let database_url = std::env::var("DATABASE_URL") + .map_err(|_| Error::Configuration("DATABASE_URL not set".into()))?; + + sqlx::PgPool::connect(&database_url) + .await + .map_err(|e| Error::Database(e.to_string())) +} + +async fn initialize_redis() -> Result { + let redis_url = std::env::var("REDIS_URL") + .map_err(|_| Error::Configuration("REDIS_URL not set".into()))?; + + redis::Client::open(redis_url) + .map_err(|e| Error::Cache(e.to_string())) +} + +#[derive(Clone)] +struct AppState { + db: sqlx::PgPool, + redis: redis::Client, +} + +async fn start_server(app: axum::Router) -> Result<()> { + let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 3000)); + info!("Starting server on {}", addr); + + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .map_err(|e| Error::Server(e.to_string()))?; + + Ok(()) +} \ No newline at end of file