spring_postgres/
lib.rs

1//! [![spring-rs](https://img.shields.io/github/stars/spring-rs/spring-rs)](https://spring-rs.github.io/docs/plugins/spring-postgres)
2#![doc(html_favicon_url = "https://spring-rs.github.io/favicon.ico")]
3#![doc(html_logo_url = "https://spring-rs.github.io/logo.svg")]
4
5pub mod config;
6pub extern crate tokio_postgres as postgres;
7
8use config::PgConfig;
9use spring::app::AppBuilder;
10use spring::async_trait;
11use spring::config::ConfigRegistry;
12use spring::plugin::{MutableComponentRegistry, Plugin};
13use std::sync::Arc;
14use tokio_postgres::NoTls;
15
16pub type Postgres = Arc<tokio_postgres::Client>;
17
18pub struct PgPlugin;
19
20#[async_trait]
21impl Plugin for PgPlugin {
22    async fn build(&self, app: &mut AppBuilder) {
23        let config = app
24            .get_config::<PgConfig>()
25            .expect("postgres plugin config load failed");
26
27        let (client, connection) = tokio_postgres::connect(&config.connect, NoTls)
28            .await
29            .expect("connect postgresql failed");
30
31        tokio::spawn(async move {
32            if let Err(e) = connection.await {
33                tracing::error!("postgresql connection error: {}", e);
34            }
35        });
36
37        app.add_component(Postgres::new(client));
38    }
39}