diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-09 20:55:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-09 20:55:06 +0100 |
commit | 21ec8d86f701f1a261fa2c3892034c7763a950b6 (patch) | |
tree | c88f6383d24d5bd9149820040e58fe960481207e /src | |
parent | 49f38a56618631576b672c9c9575bb8fcf91badf (diff) | |
parent | dbe8af8e64a8a0ce55672617ed240066adf88300 (diff) |
Merge pull request #2 from gico-net/feat/connection-to-db
Connection to PostgreSQL database
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 15 | ||||
-rw-r--r-- | src/main.rs | 16 |
2 files changed, 29 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..f10fd08 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,15 @@ +pub use config::ConfigError; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct Config { + pub pg: deadpool_postgres::Config, +} + +impl Config { + pub fn from_env() -> Result<Self, ConfigError> { + let mut cfg = config::Config::new(); + cfg.merge(config::Environment::new())?; + cfg.try_into() + } +} diff --git a/src/main.rs b/src/main.rs index 594491f..0ef80bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,20 @@ +mod config; + use actix_web::{web, App, HttpResponse, HttpServer}; +use dotenv::dotenv; +use tokio_postgres::NoTls; + +use crate::config::Config; + #[actix_rt::main] async fn main() -> std::io::Result<()> { - HttpServer::new(|| { - App::new().service( + dotenv().ok(); + + let config = Config::from_env().unwrap(); + let pool = config.pg.create_pool(NoTls).unwrap(); + + HttpServer::new(move || { + App::new().data(pool.clone()).service( web::resource("/") .to(|| HttpResponse::Ok().body("Hello from Rust!")), ) |