Skip to content

Commit

Permalink
Connect to our Postgres, add connection pool to AppState (#60)
Browse files Browse the repository at this point in the history
* Connect to our Postgres, add connection pool to AppState

* Improve compile time
  • Loading branch information
m-milek authored Jul 24, 2024
1 parent e7c36c7 commit e64b551
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
9 changes: 5 additions & 4 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" ] }

5 changes: 5 additions & 0 deletions backend/migrations/20240722212621_initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Add migration script here
CREATE TABLE TEST (
id SERIAL PRIMARY KEY,
val VARCHAR(50)
);
18 changes: 14 additions & 4 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -43,6 +44,7 @@ async fn hello() -> Json<Value> {
#[derive(Clone)]
pub struct AppState {
pub reddit: RedditConnection,
pub pool: Pool<Postgres>,
}

/// Entry point of the RMoods server.
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit e64b551

Please sign in to comment.