Skip to content

Commit da4cbb1

Browse files
new(all): Initial import.
1 parent 8acd51a commit da4cbb1

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"cwd": "${workspaceFolder}",
1717
"env": {
1818
"RUST_LOG": "debug",
19-
"DATABASE_URL": "postgresql://localhost:5432/gbdb",
19+
"DATABASE_URL": "postgres://gbuser:gbpassword@localhost:5432/generalbots",
2020
"REDIS_URL": "redis://localhost:6379"
2121
}
2222
},

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gb-api/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ gb-messaging = { path = "../gb-messaging" }
1111
gb-monitoring = { path = "../gb-monitoring" }
1212
tokio = { version = "1.0", features = ["full", "macros", "rt-multi-thread"] } # Add these features
1313
axum = { version = "0.7.9", features = ["ws", "multipart", "macros"] }
14-
tower= { workspace = true }
15-
tower-http = { version = "0.5", features = ["cors", "trace"] }
1614
serde= { workspace = true }
1715
serde_json= { workspace = true }
1816
uuid= { workspace = true }
@@ -24,6 +22,10 @@ chrono = { workspace = true, features = ["serde"] }
2422
tokio-stream = "0.1.17"
2523
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] }
2624
redis = { version = "0.23", features = ["tokio-comp"] }
25+
hyper = { version = "1.0", features = ["server"] }
26+
hyper-util = { version = "0.1" }
27+
tower = { workspace = true }
28+
tower-http = { version = "0.5", features = ["cors", "trace"] }
2729

2830
[dev-dependencies]
2931
rstest= { workspace = true }

gb-api/src/main.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use gb_core::{Error, Result};
22
use gb_core::models::Customer;
33
use tracing::{info, error};
4+
use axum::Router;
5+
use std::net::SocketAddr;
46

57
#[tokio::main]
68
async fn main() -> Result<()> {
@@ -53,19 +55,19 @@ fn init_logging() -> Result<()> {
5355

5456
async fn initialize_database() -> Result<sqlx::PgPool> {
5557
let database_url = std::env::var("DATABASE_URL")
56-
.map_err(|_| Error::Configuration("DATABASE_URL not set".into()))?;
58+
.map_err(|_| Error::internal("DATABASE_URL not set".to_string()))?;
5759

5860
sqlx::PgPool::connect(&database_url)
5961
.await
60-
.map_err(|e| Error::Database(e.to_string()))
62+
.map_err(|e| Error::internal(e.to_string()))
6163
}
6264

6365
async fn initialize_redis() -> Result<redis::Client> {
6466
let redis_url = std::env::var("REDIS_URL")
65-
.map_err(|_| Error::Configuration("REDIS_URL not set".into()))?;
67+
.map_err(|_| Error::internal("REDIS_URL not set".to_string()))?;
6668

6769
redis::Client::open(redis_url)
68-
.map_err(|e| Error::Cache(e.to_string()))
70+
.map_err(|e| Error::internal(e.to_string()))
6971
}
7072

7173
#[derive(Clone)]
@@ -74,14 +76,21 @@ struct AppState {
7476
redis: redis::Client,
7577
}
7678

77-
async fn start_server(app: axum::Router) -> Result<()> {
78-
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 3000));
79-
info!("Starting server on {}", addr);
8079

81-
axum::Server::bind(&addr)
82-
.serve(app.into_make_service())
83-
.await
84-
.map_err(|e| Error::Server(e.to_string()))?;
80+
async fn start_server(app: Router) -> Result<()> {
81+
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
82+
info!("Starting server on {}", addr);
8583

86-
Ok(())
84+
match tokio::net::TcpListener::bind(addr).await {
85+
Ok(listener) => {
86+
info!("Listening on {}", addr);
87+
axum::serve(listener, app)
88+
.await
89+
.map_err(|e| Error::internal(format!("Server error: {}", e)))
90+
}
91+
Err(e) => {
92+
error!("Failed to bind to address: {}", e);
93+
Err(Error::internal(format!("Failed to bind to address: {}", e)))
94+
}
95+
}
8796
}

gb-core/src/errors.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ use serde_json::json;
88

99
#[derive(Error, Debug)]
1010
pub enum ErrorKind {
11+
1112
#[error("Database error: {0}")]
1213
Database(String),
13-
14+
15+
#[error("Configuration error: {0}")]
16+
Configuration(String),
17+
1418
#[error("Redis error: {0}")]
1519
Redis(String),
1620

@@ -40,6 +44,10 @@ pub enum ErrorKind {
4044

4145
#[error("Messaging error: {0}")]
4246
Messaging(String),
47+
48+
#[error("API HTTP Server error: {0}")]
49+
Server(String),
50+
4351
}
4452

4553
#[derive(Debug)]

0 commit comments

Comments
 (0)