Răsfoiți Sursa

Add database connection code

Lee Morgan 1 lună în urmă
părinte
comite
a7e420f34a
3 a modificat fișierele cu 734 adăugiri și 19 ștergeri
  1. 719 19
      Cargo.lock
  2. 7 0
      Cargo.toml
  3. 8 0
      src/main.rs

Fișier diff suprimat deoarece este prea mare
+ 719 - 19
Cargo.lock


+ 7 - 0
Cargo.toml

@@ -8,4 +8,11 @@ actix-cors = "0.7.1"
 actix-web = "4.13.0"
 dotenvy = "0.15.7"
 serde = { version = "1.0.228", features = ["derive"]}
+sqlx = { version = "0.8.6", features = [
+    "postgres",
+    "runtime-tokio",
+    "tls-native-tls",
+    "chrono",
+    "uuid"
+]}
 thiserror = "2.0.18"

+ 8 - 0
src/main.rs

@@ -1,6 +1,7 @@
 use actix_web::{HttpServer, web, App, middleware};
 use actix_cors::Cors;
 use http_error::HttpError;
+use sqlx::PgPool;
 use std::env;
 
 mod http_error;
@@ -14,6 +15,12 @@ async fn main() -> std::io::Result<()> {
 
     let port: u16 = env_var("PORT");
     let app_env: String = env_var("APP_ENV");
+    let database_url: String = env_var("DATABASE_URL");
+
+    //Database
+    let pool = PgPool::connect(&database_url)
+        .await
+        .expect("Failed to start Postgres");
 
     //Server
     HttpServer::new(move || {
@@ -41,6 +48,7 @@ async fn main() -> std::io::Result<()> {
                     HttpError::JsonDeserializationError(err.to_string()).into()
                 })
             )
+            .app_data(web::Data::new(pool.clone()))
             .configure(routes::user::config)
     })
         .bind(("0.0.0.0", port))

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff