Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Use configuration to connect to db (#11)
Browse files Browse the repository at this point in the history
* Add configuration yaml file

* Add config dependency

* Add configuration model and postgres connect string method

* Use configuration to connect to db
  • Loading branch information
adrianEffe authored Jul 23, 2023
1 parent 51f270a commit bfd1a2b
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 7 deletions.
178 changes: 173 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ path = "src/lib.rs"
axum = "0.6.1"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0.171", features = ["derive"] }
config = "0.13.3"

[dependencies.sqlx]
version = "0.7.1"
Expand Down
8 changes: 8 additions & 0 deletions Tests/full_url.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
pub mod shared;

use briefly::configuration::get_configuration;
use shared::test_app::TestApp;
use sqlx::{Connection, PgConnection};

#[tokio::test]
async fn full_url_returns_200_for_valid_form_data() {
let app = TestApp::new().await;
let configuration = get_configuration().expect("Failed to read configuration");
let connection_string = configuration.database.connection_string();

let connection = PgConnection::connect(&connection_string)
.await
.expect("Failed to connect to Postgres");

let body = String::from("{\"url\":\"www.google.com\",\"extension\":\"google\"}");
let response = app.post("full_url", body).send().await.unwrap();
Expand Down
7 changes: 7 additions & 0 deletions configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
application_port: 8000
database:
host: "127.0.0.1"
port: 5432
username: "postgres"
password: "password"
database_name: "briefly"
31 changes: 31 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use config::Config;

#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
}

#[derive(serde::Deserialize)]
pub struct DatabaseSettings {
pub username: String,
pub password: String,
pub port: u16,
pub host: String,
pub database_name: String,
}

pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let builder = Config::builder().add_source(config::File::with_name("configuration"));
let config = builder.build()?;
config.try_deserialize()
}

impl DatabaseSettings {
pub fn connection_string(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}",
self.username, self.password, self.host, self.port, self.database_name
)
}
}
Loading

0 comments on commit bfd1a2b

Please sign in to comment.