From e64b551aa7f94207c3ec4a67f1f52525e56d7672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mi=C5=82ek?= <105422839+m-milek@users.noreply.github.com> Date: Wed, 24 Jul 2024 20:17:23 +0200 Subject: [PATCH] Connect to our Postgres, add connection pool to AppState (#60) * Connect to our Postgres, add connection pool to AppState * Improve compile time --- backend/Cargo.toml | 9 +++++---- backend/migrations/20240722212621_initial.sql | 5 +++++ backend/src/main.rs | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 backend/migrations/20240722212621_initial.sql diff --git a/backend/Cargo.toml b/backend/Cargo.toml index afb8d59..8f6721e 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -3,6 +3,9 @@ name = "rmoods-backend" version = "0.1.0" edition = "2021" +[profile.dev] +opt-level = 1 + [dependencies] tokio = { version = "1.37.0", features = ["full"] } axum = "0.7.5" @@ -11,18 +14,16 @@ log = "0.4.21" serde = "1.0.203" serde_json = "1.0.117" tower-http = {version = "0.5.2", features = ["full"]} - -# OpenAPI Docs generation utoipa = {version = "4.2.3", features = ["axum_extras", "chrono"]} utoipa-swagger-ui = { version = "7", features = ["axum"] } lazy_static = "1.5.0" anyhow = "1.0.86" reqwest = { version = "0.12.5", features = ["json"] } -project-root = "0.2.2" derive-getters = "0.4.0" lipsum = "0.9.1" -futures = "0.3.30" thiserror = "1.0.62" serde_with = "3.8.3" log-derive = "0.4.1" dotenvy = "0.15.7" +sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres", "macros" ] } + diff --git a/backend/migrations/20240722212621_initial.sql b/backend/migrations/20240722212621_initial.sql new file mode 100644 index 0000000..072293b --- /dev/null +++ b/backend/migrations/20240722212621_initial.sql @@ -0,0 +1,5 @@ +-- Add migration script here +CREATE TABLE TEST ( + id SERIAL PRIMARY KEY, + val VARCHAR(50) +); diff --git a/backend/src/main.rs b/backend/src/main.rs index b2a6412..0f7aba2 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,8 +1,9 @@ use axum::{http::Method, routing::get, Json, Router}; use log::{info, warn}; -use reddit::{RedditApp, RedditConnection}; -use reqwest::{Client, StatusCode}; +use reddit::RedditConnection; +use reqwest::Client; use serde_json::{json, Value}; +use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; use tower_http::{ cors::{Any, CorsLayer}, trace::TraceLayer, @@ -43,6 +44,7 @@ async fn hello() -> Json { #[derive(Clone)] pub struct AppState { pub reddit: RedditConnection, + pub pool: Pool, } /// Entry point of the RMoods server. @@ -54,8 +56,15 @@ async fn main() -> anyhow::Result<()> { env_logger::init(); if let Err(_) = dotenvy::dotenv() { - warn!(".env not found. Environment variables will have to be defined outside of .env"); - } + warn!(".env not found. Environment variables will have to be defined outside of .env"); + } + + let url = std::env::var("DATABASE_URL").expect("DB_URL is set"); + let pool = PgPoolOptions::new() + .max_connections(10) + .connect(&url) + .await?; + info!("Connected to the database"); // Allow browsers to use GET and PUT from any origin let cors = CorsLayer::new() @@ -67,6 +76,7 @@ async fn main() -> anyhow::Result<()> { let state = AppState { reddit: RedditConnection::new(REQWEST_CLIENT.clone()).await?, + pool, }; // Routes after the layers won't have the layers applied