Skip to content

Commit fa77028

Browse files
committed
sentry - Connect to Postgres for Application
1 parent 652cf4b commit fa77028

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

sentry/src/db.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
use redis::aio::MultiplexedConnection;
22
use redis::RedisError;
33

4+
use bb8::Pool;
5+
use bb8_postgres::{
6+
tokio_postgres::{Error as PostgresError, NoTls},
7+
PostgresConnectionManager,
8+
};
49
use lazy_static::lazy_static;
510

611
lazy_static! {
712
static ref REDIS_URL: String =
813
std::env::var("REDIS_URL").unwrap_or_else(|_| String::from("redis://127.0.0.1:6379"));
14+
static ref POSTGRES_URL: String = std::env::var("POSTGRES_URL")
15+
.unwrap_or_else(|_| String::from("postgresql://postgres:postgres@localhost:5432"));
916
}
1017

1118
pub async fn redis_connection() -> Result<MultiplexedConnection, RedisError> {
1219
let client = redis::Client::open(REDIS_URL.as_str()).expect("Wrong redis connection string");
1320
client.get_multiplexed_tokio_connection().await
1421
}
22+
23+
pub async fn postgres_connection() -> Result<Pool<PostgresConnectionManager<NoTls>>, PostgresError>
24+
{
25+
let pg_mgr = PostgresConnectionManager::new_from_stringlike(POSTGRES_URL.as_str(), NoTls)?;
26+
27+
Pool::builder().build(pg_mgr).await
28+
}

sentry/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use crate::middleware::auth;
55
use crate::middleware::cors::{cors, Cors};
6+
use bb8::Pool;
7+
use bb8_postgres::{tokio_postgres::NoTls, PostgresConnectionManager};
68
use hyper::service::{make_service_fn, service_fn};
79
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
810
use primitives::adapter::Adapter;
@@ -41,6 +43,7 @@ pub struct Application<A: Adapter> {
4143
adapter: A,
4244
logger: Logger,
4345
redis: MultiplexedConnection,
46+
_postgres: Pool<PostgresConnectionManager<NoTls>>,
4447
_clustered: bool,
4548
port: u16,
4649
config: Config,
@@ -52,6 +55,7 @@ impl<A: Adapter + 'static> Application<A> {
5255
config: Config,
5356
logger: Logger,
5457
redis: MultiplexedConnection,
58+
postgres: Pool<PostgresConnectionManager<NoTls>>,
5559
clustered: bool,
5660
port: u16,
5761
) -> Self {
@@ -60,6 +64,7 @@ impl<A: Adapter + 'static> Application<A> {
6064
config,
6165
logger,
6266
redis,
67+
_postgres: postgres,
6368
_clustered: clustered,
6469
port,
6570
}

sentry/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use primitives::config::configuration;
99
use primitives::util::logging::{Async, PrefixedCompactFormat, TermDecorator};
1010
use primitives::util::tests::prep_db::{AUTH, IDS};
1111
use primitives::ValidatorId;
12-
use sentry::db::redis_connection;
12+
use sentry::db::{postgres_connection, redis_connection};
1313
use sentry::Application;
1414
use slog::{o, Drain, Logger};
1515
use std::convert::TryFrom;
@@ -98,15 +98,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
9898

9999
let logger = logger();
100100
let redis = redis_connection().await?;
101+
let postgres = postgres_connection().await?;
101102

102103
match adapter {
103104
AdapterTypes::EthereumAdapter(adapter) => {
104-
Application::new(*adapter, config, logger, redis, clustered, port)
105+
Application::new(*adapter, config, logger, redis, postgres, clustered, port)
105106
.run()
106107
.await
107108
}
108109
AdapterTypes::DummyAdapter(adapter) => {
109-
Application::new(*adapter, config, logger, redis, clustered, port)
110+
Application::new(*adapter, config, logger, redis, postgres, clustered, port)
110111
.run()
111112
.await
112113
}

0 commit comments

Comments
 (0)