new(all): Initial import.

This commit is contained in:
Rodrigo Rodriguez 2024-12-25 21:28:16 -03:00
parent 8acd51aa0f
commit da4cbb1da7
5 changed files with 37 additions and 16 deletions

2
.vscode/launch.json vendored
View file

@ -16,7 +16,7 @@
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"env": { "env": {
"RUST_LOG": "debug", "RUST_LOG": "debug",
"DATABASE_URL": "postgresql://localhost:5432/gbdb", "DATABASE_URL": "postgres://gbuser:gbpassword@localhost:5432/generalbots",
"REDIS_URL": "redis://localhost:6379" "REDIS_URL": "redis://localhost:6379"
} }
}, },

2
Cargo.lock generated
View file

@ -2354,6 +2354,8 @@ dependencies = [
"gb-core", "gb-core",
"gb-messaging", "gb-messaging",
"gb-monitoring", "gb-monitoring",
"hyper 1.5.2",
"hyper-util",
"redis 0.23.3", "redis 0.23.3",
"rstest", "rstest",
"serde", "serde",

View file

@ -11,8 +11,6 @@ gb-messaging = { path = "../gb-messaging" }
gb-monitoring = { path = "../gb-monitoring" } gb-monitoring = { path = "../gb-monitoring" }
tokio = { version = "1.0", features = ["full", "macros", "rt-multi-thread"] } # Add these features tokio = { version = "1.0", features = ["full", "macros", "rt-multi-thread"] } # Add these features
axum = { version = "0.7.9", features = ["ws", "multipart", "macros"] } axum = { version = "0.7.9", features = ["ws", "multipart", "macros"] }
tower= { workspace = true }
tower-http = { version = "0.5", features = ["cors", "trace"] }
serde= { workspace = true } serde= { workspace = true }
serde_json= { workspace = true } serde_json= { workspace = true }
uuid= { workspace = true } uuid= { workspace = true }
@ -24,6 +22,10 @@ chrono = { workspace = true, features = ["serde"] }
tokio-stream = "0.1.17" tokio-stream = "0.1.17"
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] } sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] }
redis = { version = "0.23", features = ["tokio-comp"] } 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] [dev-dependencies]
rstest= { workspace = true } rstest= { workspace = true }

View file

@ -1,6 +1,8 @@
use gb_core::{Error, Result}; use gb_core::{Error, Result};
use gb_core::models::Customer; use gb_core::models::Customer;
use tracing::{info, error}; use tracing::{info, error};
use axum::Router;
use std::net::SocketAddr;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -53,19 +55,19 @@ fn init_logging() -> Result<()> {
async fn initialize_database() -> Result<sqlx::PgPool> { async fn initialize_database() -> Result<sqlx::PgPool> {
let database_url = std::env::var("DATABASE_URL") 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) sqlx::PgPool::connect(&database_url)
.await .await
.map_err(|e| Error::Database(e.to_string())) .map_err(|e| Error::internal(e.to_string()))
} }
async fn initialize_redis() -> Result<redis::Client> { async fn initialize_redis() -> Result<redis::Client> {
let redis_url = std::env::var("REDIS_URL") 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) redis::Client::open(redis_url)
.map_err(|e| Error::Cache(e.to_string())) .map_err(|e| Error::internal(e.to_string()))
} }
#[derive(Clone)] #[derive(Clone)]
@ -74,14 +76,21 @@ struct AppState {
redis: redis::Client, 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); info!("Starting server on {}", addr);
axum::Server::bind(&addr) match tokio::net::TcpListener::bind(addr).await {
.serve(app.into_make_service()) Ok(listener) => {
.await info!("Listening on {}", addr);
.map_err(|e| Error::Server(e.to_string()))?; axum::serve(listener, app)
.await
Ok(()) .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)))
}
}
} }

View file

@ -8,9 +8,13 @@ use serde_json::json;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ErrorKind { pub enum ErrorKind {
#[error("Database error: {0}")] #[error("Database error: {0}")]
Database(String), Database(String),
#[error("Configuration error: {0}")]
Configuration(String),
#[error("Redis error: {0}")] #[error("Redis error: {0}")]
Redis(String), Redis(String),
@ -40,6 +44,10 @@ pub enum ErrorKind {
#[error("Messaging error: {0}")] #[error("Messaging error: {0}")]
Messaging(String), Messaging(String),
#[error("API HTTP Server error: {0}")]
Server(String),
} }
#[derive(Debug)] #[derive(Debug)]