made backend database url configurable through cli

This commit is contained in:
2025-12-31 18:03:17 +01:00
parent fa40aa404d
commit 1ebec422d6
5 changed files with 11 additions and 7 deletions

2
Cargo.lock generated
View File

@@ -257,7 +257,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]] [[package]]
name = "chatapp" name = "chatapp"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"argon2", "argon2",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "chatapp" name = "chatapp"
version = "0.1.0" version = "0.2.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]

View File

@@ -17,7 +17,7 @@
in { in {
packages.default = pkgs.rustPlatform.buildRustPackage { packages.default = pkgs.rustPlatform.buildRustPackage {
pname = "chatapp"; pname = "chatapp";
version = "0.1.0"; version = "0.2.0";
# Point to the backend subdirectory # Point to the backend subdirectory
src = ./backend; src = ./backend;

View File

@@ -2,9 +2,9 @@ use axum::http::StatusCode;
use sqlx::PgPool; use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
pub async fn init_db() -> Result<PgPool, sqlx::Error> { pub async fn init_db(url: String) -> Result<PgPool, sqlx::Error> {
let database_url = "postgres://chatapp:secret@localhost:5432/chatapp"; let database_url = format!("postgres://chatapp:secret@{url}/chatapp");
PgPool::connect(database_url).await PgPool::connect(database_url.as_str()).await
} }
pub async fn user_id_from_uuid(db: &PgPool, user_uuid: Uuid) -> Result<i32, (StatusCode, String)> { pub async fn user_id_from_uuid(db: &PgPool, user_uuid: Uuid) -> Result<i32, (StatusCode, String)> {

View File

@@ -29,6 +29,10 @@ pub struct Cli {
#[arg(short, long, default_value = "8080")] #[arg(short, long, default_value = "8080")]
port: String, port: String,
/// Database URL
#[arg(short, long, default_value = "localhost:5432")]
database: String,
/// Verbose mode /// Verbose mode
#[arg(short, long)] #[arg(short, long)]
verbose: bool, verbose: bool,
@@ -43,7 +47,7 @@ async fn main() -> anyhow::Result<()> {
.init(); .init();
tracing::info!("Connecting to database..."); tracing::info!("Connecting to database...");
let db_pool = db::init_db().await?; let db_pool = db::init_db(cli.database).await?;
let cors = CorsLayer::new() let cors = CorsLayer::new()
.allow_origin(Any) .allow_origin(Any)