Skip to content

Commit 6bb909f

Browse files
committed
chore: testing database
1 parent 437cc5f commit 6bb909f

12 files changed

+190
-23
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter"

Cargo.lock

+122-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ name = "robust-rust"
1616
actix-web = "4"
1717
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
1818
serde = {version = "1.0.163", features = ["derive"]}
19+
config = "0.11"
1920

2021
[dependencies.sqlx]
2122
version = "0.6.3"

configuration.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
application_port: 8000
2+
database:
3+
host: "127.0.0.1"
4+
port: 5432
5+
username: "postgres"
6+
password: "password"
7+
database_name: "newsletter"

src/configuration.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[derive(serde::Deserialize)]
2+
pub struct Settings {
3+
pub database: DatabaseSettings,
4+
pub application_port: u16,
5+
}
6+
7+
#[derive(serde::Deserialize)]
8+
pub struct DatabaseSettings {
9+
pub username: String,
10+
pub password: String,
11+
pub host: String,
12+
pub port: u16,
13+
pub database_name: String,
14+
}
15+
16+
impl DatabaseSettings {
17+
pub fn connection_string(&self) -> String {
18+
format!(
19+
"postgres://{}:{}@{}:{}/{}",
20+
self.username, self.password, self.host, self.port, self.database_name
21+
)
22+
}
23+
}
24+
25+
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
26+
// Initialize our configuration reader
27+
let mut settings = config::Config::default();
28+
// Add configuration values from a file named `configuration`
29+
// with the `ini` file format
30+
settings.merge(config::File::with_name("configuration"))?;
31+
// Try to convert the configuration values it read into
32+
// our Settings type
33+
settings.try_into()
34+
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pub mod routes;
21
pub mod configuration;
2+
pub mod routes;
33
pub mod startup;

src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use robust_rust::configuration::get_configuration;
12
use robust_rust::startup::run;
23
use std::net::TcpListener;
34

45
#[tokio::main]
56
async fn main() -> std::io::Result<()> {
6-
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
7-
let port = listener.local_addr().unwrap().port();
8-
let endpoint = format!("Server running at http://127.0.0.1:{}", port);
9-
println!("{}", endpoint);
7+
let configuration = get_configuration().expect("Failed to read configuration.");
8+
let address = format!("127.0.0.1:{}", configuration.application_port);
9+
let listener = TcpListener::bind(address).expect("Failed to bind random port");
1010
run(listener)?.await
1111
}

src/routes/health_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ use actix_web::HttpResponse;
22

33
pub async fn health_check() -> HttpResponse {
44
HttpResponse::Ok().into()
5-
}
5+
}

src/routes/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ mod health_check;
22
mod subscriptions;
33

44
pub use health_check::*;
5-
pub use subscriptions::*;
5+
pub use subscriptions::*;

src/routes/subscriptions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ pub struct FormData {
99

1010
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
1111
HttpResponse::Ok().finish()
12-
}
12+
}

src/startup.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use crate::routes::{health_check, subscribe};
12
use actix_web::{dev::Server, web, App, HttpServer};
23
use std::net::TcpListener;
3-
use crate::routes::{health_check, subscribe};
4-
54

65
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
76
let server = HttpServer::new(|| {

0 commit comments

Comments
 (0)