restructured, added db connection pool
This commit is contained in:
38
src/configuration.rs
Normal file
38
src/configuration.rs
Normal file
@ -0,0 +1,38 @@
|
||||
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,
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
// let mut settings = config::Config::default();
|
||||
|
||||
// settings.merge(config::File::with_name("configuration"))?;
|
||||
|
||||
// settings.try_into()
|
||||
let settings = Config::builder()
|
||||
.add_source(config::File::with_name("configuration"))
|
||||
.build()?;
|
||||
|
||||
Ok(settings.try_deserialize()?)
|
||||
}
|
||||
21
src/lib.rs
21
src/lib.rs
@ -1,18 +1,3 @@
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::dev::Server;
|
||||
use std::net::TcpListener;
|
||||
|
||||
async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().into()
|
||||
}
|
||||
|
||||
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||
let server = HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/health_check", web::get().to(health_check))
|
||||
})
|
||||
.listen(listener)?
|
||||
.run();
|
||||
|
||||
Ok(server)
|
||||
}
|
||||
pub mod configuration;
|
||||
pub mod routes;
|
||||
pub mod startup;
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@ -1,9 +1,15 @@
|
||||
use std::net::TcpListener;
|
||||
|
||||
use zero2prod::run;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use zero2prod::configuration::get_configuration;
|
||||
use zero2prod::startup::run;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind random port");
|
||||
run(listener)?.await
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection = PgPoolOptions::new()
|
||||
.max_connections(10).connect(&config.database.connection_string()).await.expect("Failed to connect to Postgres.");
|
||||
|
||||
let address = format!("127.0.0.1:{}", config.application_port);
|
||||
let listener = TcpListener::bind(address).expect("Failed to bind random port");
|
||||
run(listener, connection)?.await
|
||||
}
|
||||
|
||||
5
src/routes/health_check.rs
Normal file
5
src/routes/health_check.rs
Normal file
@ -0,0 +1,5 @@
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().into()
|
||||
}
|
||||
5
src/routes/mod.rs
Normal file
5
src/routes/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
mod health_check;
|
||||
mod subscriptions;
|
||||
|
||||
pub use health_check::*;
|
||||
pub use subscriptions::*;
|
||||
12
src/routes/subscriptions.rs
Normal file
12
src/routes/subscriptions.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use actix_web::{web, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FormData {
|
||||
pub email: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
20
src/startup.rs
Normal file
20
src/startup.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use actix_web::dev::Server;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use sqlx::{Pool, Postgres};
|
||||
use std::net::TcpListener;
|
||||
|
||||
use crate::routes::{health_check, subscribe};
|
||||
|
||||
pub fn run(listener: TcpListener, connection: Pool<Postgres>) -> Result<Server, std::io::Error> {
|
||||
let connection = web::Data::new(connection);
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.route("/health_check", web::get().to(health_check))
|
||||
.route("/subscriptions", web::post().to(subscribe))
|
||||
.app_data(connection.clone())
|
||||
})
|
||||
.listen(listener)?
|
||||
.run();
|
||||
|
||||
Ok(server)
|
||||
}
|
||||
Reference in New Issue
Block a user