From da4cbb1da75166a45e9dc95ea5986fac0f88d711 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 25 Dec 2024 21:28:16 -0300 Subject: [PATCH] new(all): Initial import. --- .vscode/launch.json | 2 +- Cargo.lock | 2 ++ gb-api/Cargo.toml | 6 ++++-- gb-api/src/main.rs | 33 +++++++++++++++++++++------------ gb-core/src/errors.rs | 10 +++++++++- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4e4a8a7..6bc908a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "cwd": "${workspaceFolder}", "env": { "RUST_LOG": "debug", - "DATABASE_URL": "postgresql://localhost:5432/gbdb", + "DATABASE_URL": "postgres://gbuser:gbpassword@localhost:5432/generalbots", "REDIS_URL": "redis://localhost:6379" } }, diff --git a/Cargo.lock b/Cargo.lock index ed085ab..1ef6c18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2354,6 +2354,8 @@ dependencies = [ "gb-core", "gb-messaging", "gb-monitoring", + "hyper 1.5.2", + "hyper-util", "redis 0.23.3", "rstest", "serde", diff --git a/gb-api/Cargo.toml b/gb-api/Cargo.toml index fde0c38..8d67a7f 100644 --- a/gb-api/Cargo.toml +++ b/gb-api/Cargo.toml @@ -11,8 +11,6 @@ gb-messaging = { path = "../gb-messaging" } gb-monitoring = { path = "../gb-monitoring" } 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"] } serde= { workspace = true } serde_json= { workspace = true } uuid= { workspace = true } @@ -24,6 +22,10 @@ chrono = { workspace = true, features = ["serde"] } tokio-stream = "0.1.17" sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] } redis = { version = "0.23", features = ["tokio-comp"] } +hyper = { version = "1.0", features = ["server"] } +hyper-util = { version = "0.1" } +tower = { workspace = true } +tower-http = { version = "0.5", features = ["cors", "trace"] } [dev-dependencies] rstest= { workspace = true } diff --git a/gb-api/src/main.rs b/gb-api/src/main.rs index 003465e..109de7e 100644 --- a/gb-api/src/main.rs +++ b/gb-api/src/main.rs @@ -1,6 +1,8 @@ use gb_core::{Error, Result}; use gb_core::models::Customer; use tracing::{info, error}; +use axum::Router; +use std::net::SocketAddr; #[tokio::main] async fn main() -> Result<()> { @@ -53,19 +55,19 @@ fn init_logging() -> Result<()> { async fn initialize_database() -> Result { let database_url = std::env::var("DATABASE_URL") - .map_err(|_| Error::Configuration("DATABASE_URL not set".into()))?; + .map_err(|_| Error::internal("DATABASE_URL not set".to_string()))?; sqlx::PgPool::connect(&database_url) .await - .map_err(|e| Error::Database(e.to_string())) + .map_err(|e| Error::internal(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()))?; + .map_err(|_| Error::internal("REDIS_URL not set".to_string()))?; redis::Client::open(redis_url) - .map_err(|e| Error::Cache(e.to_string())) + .map_err(|e| Error::internal(e.to_string())) } #[derive(Clone)] @@ -74,14 +76,21 @@ struct AppState { redis: redis::Client, } -async fn start_server(app: axum::Router) -> Result<()> { - let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 3000)); + +async fn start_server(app: Router) -> Result<()> { + let addr = 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(()) + 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))) + } + } } \ No newline at end of file diff --git a/gb-core/src/errors.rs b/gb-core/src/errors.rs index cdde33d..aca874d 100644 --- a/gb-core/src/errors.rs +++ b/gb-core/src/errors.rs @@ -8,9 +8,13 @@ use serde_json::json; #[derive(Error, Debug)] pub enum ErrorKind { + #[error("Database error: {0}")] Database(String), - + + #[error("Configuration error: {0}")] + Configuration(String), + #[error("Redis error: {0}")] Redis(String), @@ -40,6 +44,10 @@ pub enum ErrorKind { #[error("Messaging error: {0}")] Messaging(String), + + #[error("API HTTP Server error: {0}")] + Server(String), + } #[derive(Debug)]