1
0
mirror of https://github.com/actix/examples synced 2025-06-27 01:27:43 +02:00

Update dependencies for databases/postgres (#555)

Fix config code to account for breaking changes
This commit is contained in:
Andrew Langmeier
2022-06-07 19:51:42 -04:00
committed by GitHub
parent a7a7f0858b
commit 271a68c07d
3 changed files with 194 additions and 191 deletions

View File

@ -1,18 +1,10 @@
mod config {
pub use ::config::ConfigError;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Config {
#[derive(Debug, Default, Deserialize)]
pub struct ExampleConfig {
pub server_addr: String,
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()
}
}
}
mod models {
@ -106,6 +98,8 @@ mod handlers {
}
}
use crate::config::ExampleConfig;
use ::config::Config;
use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
use handlers::add_user;
@ -115,7 +109,13 @@ use tokio_postgres::NoTls;
async fn main() -> std::io::Result<()> {
dotenv().ok();
let config = crate::config::Config::from_env().unwrap();
let config_ = Config::builder()
.add_source(::config::Environment::default())
.build()
.unwrap();
let config: ExampleConfig = config_.try_deserialize().unwrap();
let pool = config.pg.create_pool(None, NoTls).unwrap();
let server = HttpServer::new(move || {