Skip to content

Commit

Permalink
feat: connect to database using r2d2 pool
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 10, 2024
1 parent 8aaaf45 commit 57967f6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ axum = "0.7.4"
tracing-subscriber = "0.3.18"
tracing = "0.1.40"
tower-http = { version = "0.5.2", features = ["trace"] }
diesel = { version = "2.1.4", features = ["postgres", "chrono"] }
diesel = { version = "2.1.4", features = ["postgres", "chrono", "r2d2"] }
16 changes: 10 additions & 6 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ pub struct Args {
#[arg(env = "BUILDIT_AMQP_ADDR")]
pub amqp_addr: String,

/// RabbitMQ address to access queue api e.g. http://user:password@host:port/api/queues/vhost/
#[arg(env = "BUILDIT_RABBITMQ_QUEUE_API")]
pub rabbitmq_queue_api: Option<String>,
/// Database connection url
#[arg(env = "DATABASE_URL")]
pub database_url: String,

/// GitHub access token
#[arg(env = "BUILDIT_GITHUB_ACCESS_TOKEN")]
pub github_access_token: String,

#[arg(env = "ABBS_PATH")]
pub abbs_path: PathBuf,

/// RabbitMQ address to access queue api e.g. http://user:password@host:port/api/queues/vhost/
#[arg(env = "BUILDIT_RABBITMQ_QUEUE_API")]
pub rabbitmq_queue_api: Option<String>,

/// Secret
#[arg(env = "SECRET")]
pub secret: Option<String>,
Expand All @@ -51,9 +58,6 @@ pub struct Args {

#[arg(env = "GITHUB_APP_KEY_PEM_PATH")]
pub github_app_key: Option<PathBuf>,

#[arg(env = "ABBS_PATH")]
pub abbs_path: PathBuf,
}

pub static ARGS: Lazy<Args> = Lazy::new(Args::parse);
Expand Down
11 changes: 11 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use axum::{routing::get, Router};
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use server::ARGS;

// basic handler that responds with a static string
async fn root() -> &'static str {
Expand All @@ -7,8 +11,15 @@ async fn root() -> &'static str {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv()?;
tracing_subscriber::fmt::init();

tracing::info!("Connecting to database");
let manager = ConnectionManager::<PgConnection>::new(&ARGS.database_url);
let pool = Pool::builder()

Check warning on line 19 in server/src/main.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `pool`
.test_on_check_out(true)
.build(manager)?;

tracing::info!("Starting http server");

// build our application with a route
Expand Down

0 comments on commit 57967f6

Please sign in to comment.