made db connect configurable

This commit is contained in:
Shautvast 2026-01-15 19:33:16 +01:00
parent 611d635df5
commit c4dd82d191
3 changed files with 23 additions and 19 deletions

View file

@ -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 <connect_string>```
* 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
* optionally, adjust the database ddl, update the query in db.tp, and recheck the output

View file

@ -60,7 +60,9 @@ fn now(_self_val: RefMut<Value>, _args: Vec<Value>) -> Result<Value, RuntimeErro
fn sql(_self_val: RefMut<Value>, args: Vec<Value>) -> Result<Value, RuntimeError> {
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![];

View file

@ -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<String>,
#[arg(short, long)]
postgres: Option<String>,
}
#[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)?;