hwhkit_integration_postgres/
lib.rs1use async_trait::async_trait;
2use hwhkit_config::AppConfig;
3use hwhkit_core::{AppContext, Error as CoreError, IntegrationProvider, Result as CoreResult};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct PostgresConfig {
8 pub enabled: bool,
9 pub url: String,
10 pub max_connections: u32,
11}
12
13#[derive(Debug, Clone)]
14pub struct PostgresHandle {
15 pub url: String,
16 pub max_connections: u32,
17}
18
19#[derive(Debug, Default)]
20pub struct PostgresProvider;
21
22#[async_trait]
23impl IntegrationProvider for PostgresProvider {
24 fn key(&self) -> &'static str {
25 "postgres"
26 }
27
28 fn feature(&self) -> &'static str {
29 "postgres"
30 }
31
32 fn enabled(&self, cfg: &AppConfig) -> bool {
33 cfg.integrations.sql.postgres.enabled
34 }
35
36 fn required(&self, cfg: &AppConfig) -> bool {
37 cfg.integrations.sql.postgres.required
38 }
39
40 async fn init(&self, ctx: &mut AppContext, cfg: &AppConfig) -> CoreResult<()> {
41 let postgres = &cfg.integrations.sql.postgres;
42 if !postgres.url.starts_with("postgres://") && !postgres.url.starts_with("postgresql://") {
43 return Err(CoreError::Integration {
44 integration: self.key().to_string(),
45 reason: "postgres url must start with postgres:// or postgresql://".to_string(),
46 });
47 }
48
49 ctx.insert(PostgresHandle {
50 url: postgres.url.clone(),
51 max_connections: postgres.max_connections,
52 });
53
54 Ok(())
55 }
56}