made db connect configurable
This commit is contained in:
parent
611d635df5
commit
c4dd82d191
3 changed files with 23 additions and 19 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
* currently needs postgres on the default port
|
* Example with postgres running
|
||||||
* user 'postgres'
|
* run ```cargo run -- --source examples/web --watch --postgres <connect_string>```
|
||||||
* password 'boompje'
|
* the connect_string is directly passed to bb8 like [here](https://github.com/djc/bb8/blob/main/postgres/examples/static_select.rs)
|
||||||
* run ```cargo run -- --source examples/web --watch```
|
* see ```main.tp``` for the database queries that it does on startup
|
||||||
* see ```main.tp``` for the database queries
|
|
||||||
* and head to http://localhost:3000/api/customer
|
* 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
|
||||||
|
|
@ -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> {
|
fn sql(_self_val: RefMut<Value>, args: Vec<Value>) -> Result<Value, RuntimeError> {
|
||||||
let result = tokio::task::block_in_place(|| {
|
let result = tokio::task::block_in_place(|| {
|
||||||
tokio::runtime::Handle::current().block_on(async {
|
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 result = conn.query(&args[0].to_string(), &[]).await.unwrap();
|
||||||
let mut columns = vec![];
|
let mut columns = vec![];
|
||||||
|
|
||||||
|
|
|
||||||
27
src/main.rs
27
src/main.rs
|
|
@ -16,6 +16,7 @@ use tipi_lang::value::Value;
|
||||||
use tipi_lang::vm::run_async;
|
use tipi_lang::vm::run_async;
|
||||||
use tipi_lang::{DB_POOL, vm};
|
use tipi_lang::{DB_POOL, vm};
|
||||||
use tokio_postgres::NoTls;
|
use tokio_postgres::NoTls;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
/// A simple CLI tool to greet users
|
/// A simple CLI tool to greet users
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
|
@ -31,24 +32,26 @@ struct Args {
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
file: Option<String>,
|
file: Option<String>,
|
||||||
|
|
||||||
|
#[arg(short, long)]
|
||||||
|
postgres: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), TipiLangError> {
|
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();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let args = Args::parse();
|
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 {
|
if let Some(file) = args.file {
|
||||||
let source = fs::read_to_string(file).expect("Unable to read file");
|
let source = fs::read_to_string(file).expect("Unable to read file");
|
||||||
run(&source)?;
|
run(&source)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue