From c4dd82d191ede60857382a5ea06f81b00d6fdd23 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Thu, 15 Jan 2026 19:33:16 +0100 Subject: [PATCH] made db connect configurable --- examples/web/README.md | 11 +++++------ src/builtins/globals.rs | 4 +++- src/main.rs | 27 +++++++++++++++------------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/examples/web/README.md b/examples/web/README.md index 177eddd..6a51027 100644 --- a/examples/web/README.md +++ b/examples/web/README.md @@ -1,7 +1,6 @@ -* currently needs postgres on the default port -* user 'postgres' -* password 'boompje' -* run ```cargo run -- --source examples/web --watch``` -* see ```main.tp``` for the database queries +* Example with postgres running +* run ```cargo run -- --source examples/web --watch --postgres ``` +* the connect_string is directly passed to bb8 like [here](https://github.com/djc/bb8/blob/main/postgres/examples/static_select.rs) +* see ```main.tp``` for the database queries that it does on startup * and head to http://localhost:3000/api/customer -* optionally, adjust the database ddl, update the query in db.tp, and recheck the outpout \ No newline at end of file +* optionally, adjust the database ddl, update the query in db.tp, and recheck the output \ No newline at end of file diff --git a/src/builtins/globals.rs b/src/builtins/globals.rs index 4f1bf32..d395f27 100644 --- a/src/builtins/globals.rs +++ b/src/builtins/globals.rs @@ -60,7 +60,9 @@ fn now(_self_val: RefMut, _args: Vec) -> Result, args: Vec) -> Result { let result = tokio::task::block_in_place(|| { tokio::runtime::Handle::current().block_on(async { - let mut conn = DB_POOL.get().unwrap().get().await.unwrap(); + let mut conn = DB_POOL.get() + .expect(format!("Error running query '{}'. Did you add --postgres on the commandline?",args[0]).as_str()) + .get().await.unwrap(); let result = conn.query(&args[0].to_string(), &[]).await.unwrap(); let mut columns = vec![]; diff --git a/src/main.rs b/src/main.rs index a807889..2293377 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use tipi_lang::value::Value; use tipi_lang::vm::run_async; use tipi_lang::{DB_POOL, vm}; use tokio_postgres::NoTls; +use std::str::FromStr; /// A simple CLI tool to greet users #[derive(Parser, Debug)] @@ -31,24 +32,26 @@ struct Args { #[arg(short, long)] file: Option, + + #[arg(short, long)] + postgres: Option, } #[tokio::main] async fn main() -> Result<(), TipiLangError> { - //TODO make configurable - let manager = PostgresConnectionManager::new_from_stringlike( - "host=localhost user=postgres password=boompje", - NoTls, - ) - .unwrap(); - let pool = bb8::Pool::builder() - .build(manager) - .await - .expect("cannot create the the database connection pool"); - let _ = DB_POOL.set(pool); - tracing_subscriber::fmt::init(); + let args = Args::parse(); + if let Some(pg) = args.postgres { + let config = tokio_postgres::config::Config::from_str(&pg).map_err(|e|TipiLangError::Platform(e.to_string()))?; + let manager = PostgresConnectionManager::new(config,NoTls); + let pool = bb8::Pool::builder() + .build(manager) + .await + .expect("cannot create the the database connection pool"); + let _ = DB_POOL.set(pool); + } + if let Some(file) = args.file { let source = fs::read_to_string(file).expect("Unable to read file"); run(&source)?;