-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from renegade-fi/joey/funds-manager-endpoints
funds-manager: Use `diesel-async` and add `warp` endpoints
- Loading branch information
Showing
10 changed files
with
213 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
//! Database code | ||
use diesel_async::AsyncPgConnection; | ||
use native_tls::TlsConnector; | ||
use postgres_native_tls::MakeTlsConnector; | ||
use renegade_util::err_str; | ||
use tracing::error; | ||
|
||
use crate::error::FundsManagerError; | ||
|
||
pub mod models; | ||
#[allow(missing_docs)] | ||
pub mod schema; | ||
|
||
/// Establish a connection to the database | ||
pub async fn establish_connection(db_url: &str) -> Result<AsyncPgConnection, FundsManagerError> { | ||
// Build a TLS connector, we don't validate certificates for simplicity. | ||
// Practically this is unnecessary because we will be limiting our traffic to | ||
// within a siloed environment when deployed | ||
let connector = TlsConnector::builder() | ||
.danger_accept_invalid_certs(true) | ||
.build() | ||
.map_err(err_str!(FundsManagerError::Db))?; | ||
let connector = MakeTlsConnector::new(connector); | ||
let (client, conn) = tokio_postgres::connect(db_url, connector) | ||
.await | ||
.map_err(err_str!(FundsManagerError::Db))?; | ||
|
||
// Spawn the connection handle in a separate task | ||
tokio::spawn(async move { | ||
if let Err(e) = conn.await { | ||
error!("Connection error: {}", e); | ||
} | ||
}); | ||
|
||
AsyncPgConnection::try_from(client).await.map_err(err_str!(FundsManagerError::Db)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.