add environment based db config
This commit is contained in:
parent
bd35323627
commit
1d689bb884
|
@ -29,9 +29,11 @@ Running sandbox:
|
|||
```shell
|
||||
$ sudo ./scripts/remove_db.sh
|
||||
$ sudo ./scripts/deploy_sandbox.sh
|
||||
$ cargo run --no-default-features --features "server" -- 0.0.0.0:4000
|
||||
$ . ./scripts/env.sh && cargo run --no-default-features
|
||||
--features "server" -- 0.0.0.0:4000
|
||||
--cert certs/certificate.crt --key private.key &
|
||||
$ cargo run --no-default-features --features "client,tls_no_verify" &
|
||||
$ cargo run --no-default-features
|
||||
--features "client,tls_no_verify" &
|
||||
```
|
||||
|
||||
## Built With
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
#!/bin/sh
|
||||
|
||||
export DB_HOST="localhost"
|
||||
export DB_HOST_PORT="5432"
|
||||
export DB_NAME="pt_db"
|
||||
|
||||
export DB_USER="pt_usr"
|
||||
export DB_PASS="PASSWORD"
|
||||
|
||||
export DB_SESS_USER="sessions_schema_usr"
|
||||
export DB_SESS_PASS="PASSWORD"
|
||||
|
||||
export DB_ACC_USER="accounts_schema_usr"
|
||||
export DB_ACC_PASS="PASSWORD"
|
||||
|
||||
export DB_PORTFOLIO_USER="portfolio_schema_usr"
|
||||
export DB_PORTFOLIO_PASS="PASSWORD"
|
|
@ -7,7 +7,6 @@ use crate::common::message::message_builder::message_builder;
|
|||
use crate::common::message::message_type::MessageType;
|
||||
use crate::common::misc::return_flags::ReturnFlags;
|
||||
|
||||
use crate::server::db::config::{DB_PORTFOLIO_PASS, DB_PORTFOLIO_USER};
|
||||
use crate::server::db::initializer::db_connect;
|
||||
use crate::server::network::jwt_wrapper::verify_jwt_token;
|
||||
|
||||
|
@ -30,9 +29,12 @@ pub async fn acc_retrieve_portfolio(
|
|||
};
|
||||
|
||||
/* connect to SQL database using user ```postfolio_schema_user``` */
|
||||
let sql_conn = db_connect(DB_PORTFOLIO_USER, DB_PORTFOLIO_PASS)
|
||||
.await
|
||||
.map_err(|_| ReturnFlags::ServerRetrievePortfolioFailed)?;
|
||||
let sql_conn = db_connect(
|
||||
std::env::var("DB_PORTFOLIO_USER").unwrap(),
|
||||
std::env::var("DB_PORTFOLIO_PASS").unwrap(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| ReturnFlags::ServerRetrievePortfolioFailed)?;
|
||||
|
||||
/* get userId's portfolio positions */
|
||||
let mut portfolio: Portfolio = Portfolio::default();
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
pub static DB_HOST: &'static str = "localhost";
|
||||
pub static DB_HOST_PORT: &'static str = "5432";
|
||||
pub static DB_NAME: &'static str = "pt_db";
|
||||
|
||||
pub static DB_USER: &'static str = "pt_usr";
|
||||
pub static DB_PASS: &'static str = "PASSWORD";
|
||||
|
||||
pub static DB_SESS_USER: &'static str = "sessions_schema_usr";
|
||||
pub static DB_SESS_PASS: &'static str = "PASSWORD";
|
||||
|
||||
pub static DB_ACC_USER: &'static str = "accounts_schema_usr";
|
||||
pub static DB_ACC_PASS: &'static str = "PASSWORD";
|
||||
|
||||
pub static DB_PORTFOLIO_USER: &'static str = "portfolio_schema_usr";
|
||||
pub static DB_PORTFOLIO_PASS: &'static str = "PASSWORD";
|
|
@ -1,5 +1,3 @@
|
|||
use crate::server::db::config::{DB_HOST, DB_HOST_PORT, DB_NAME};
|
||||
|
||||
/// Establishes a postgresql connection to the SQL database.
|
||||
///
|
||||
/// Creates a postgresql connection.
|
||||
|
@ -16,13 +14,17 @@ use crate::server::db::config::{DB_HOST, DB_HOST_PORT, DB_NAME};
|
|||
/// let mut client = db_connect(DB_USER, DB_PASS)?;
|
||||
/// ```
|
||||
pub async fn db_connect(
|
||||
user: &'static str,
|
||||
pass: &'static str,
|
||||
user: String,
|
||||
pass: String,
|
||||
) -> Result<tokio_postgres::Client, tokio_postgres::Error> {
|
||||
/* Generate the requested string */
|
||||
let db_connect_str = format!(
|
||||
"host={} port={} dbname={} user={} password={}",
|
||||
DB_HOST, DB_HOST_PORT, DB_NAME, user, pass
|
||||
std::env::var("DB_HOST").unwrap(),
|
||||
std::env::var("DB_HOST_PORT").unwrap(),
|
||||
std::env::var("DB_NAME").unwrap(),
|
||||
user,
|
||||
pass
|
||||
);
|
||||
let (client, connection) =
|
||||
tokio_postgres::connect(db_connect_str.as_str(), tokio_postgres::NoTls).await?;
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
pub mod cmd;
|
||||
pub mod config;
|
||||
pub mod initializer;
|
||||
|
|
|
@ -12,7 +12,6 @@ use tokio_rustls::TlsAcceptor;
|
|||
|
||||
use crate::server::network::gen_tls_server_config::gen_tls_server_config;
|
||||
|
||||
use crate::server::db::config::{DB_ACC_PASS, DB_ACC_USER};
|
||||
use crate::server::db::initializer::db_connect;
|
||||
use crate::server::network::handle_data::handle_data;
|
||||
|
||||
|
@ -122,12 +121,19 @@ pub async fn libtrader_init_server() -> std::io::Result<()> {
|
|||
libtrader_init_log()?;
|
||||
|
||||
// Initialize SQL connection
|
||||
let sql_shared_conn = Arc::new(db_connect(DB_ACC_USER, DB_ACC_PASS).await.map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::ConnectionAborted,
|
||||
format!("SQL_CONNECTION_FAILED: {}", err),
|
||||
let sql_shared_conn = Arc::new(
|
||||
db_connect(
|
||||
std::env::var("DB_ACC_USER").unwrap(),
|
||||
std::env::var("DB_ACC_PASS").unwrap(),
|
||||
)
|
||||
})?);
|
||||
.await
|
||||
.map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::ConnectionAborted,
|
||||
format!("SQL_CONNECTION_FAILED: {}", err),
|
||||
)
|
||||
})?,
|
||||
);
|
||||
|
||||
// Initialize arguments
|
||||
let options: Options = argh::from_env();
|
||||
|
|
Loading…
Reference in New Issue