Added icarus_envy (#140)
* Added icarus_envy * Commenting out dotenvy crate * Removing environment and keys modules * Replacing with icarus_envy * Making changes to test code * Removing lingering references in tests * Test fix * Test fix * Another test fix * Rust formatting * Removing commented crate * Using v0.2.0 version of icarus_envy
This commit was merged in pull request #140.
This commit is contained in:
Generated
+9
-1
@@ -730,8 +730,8 @@ version = "0.2.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"common-multipart-rfc7578",
|
"common-multipart-rfc7578",
|
||||||
"dotenvy",
|
|
||||||
"futures",
|
"futures",
|
||||||
|
"icarus_envy",
|
||||||
"icarus_meta",
|
"icarus_meta",
|
||||||
"icarus_models",
|
"icarus_models",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -748,6 +748,14 @@ dependencies = [
|
|||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "icarus_envy"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "git+ssh://git@git.kundeng.us/phoenix/icarus_envy.git?tag=v0.2.0#3ee8c1e573ba9637769aa0538d2c11335e39ed9f"
|
||||||
|
dependencies = [
|
||||||
|
"dotenvy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_meta"
|
name = "icarus_meta"
|
||||||
version = "0.1.30"
|
version = "0.1.30"
|
||||||
|
|||||||
+1
-1
@@ -18,9 +18,9 @@ futures = { version = "0.3.31" }
|
|||||||
uuid = { version = "1.16.0", features = ["v4", "serde"] }
|
uuid = { version = "1.16.0", features = ["v4", "serde"] }
|
||||||
sqlx = { version = "0.8.5", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
sqlx = { version = "0.8.5", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
||||||
time = { version = "0.3.41", features = ["formatting", "macros", "parsing", "serde"] }
|
time = { version = "0.3.41", features = ["formatting", "macros", "parsing", "serde"] }
|
||||||
dotenvy = { version = "0.15.7" }
|
|
||||||
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.1.30" }
|
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.1.30" }
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
|
||||||
|
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.2.0" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
common-multipart-rfc7578 = { version = "0.7.0" }
|
common-multipart-rfc7578 = { version = "0.7.0" }
|
||||||
|
|||||||
@@ -508,7 +508,7 @@ pub mod endpoint {
|
|||||||
let song_id = payload.song_id;
|
let song_id = payload.song_id;
|
||||||
match crate::callers::song::song_db::get_song(&pool, &song_id).await {
|
match crate::callers::song::song_db::get_song(&pool, &song_id).await {
|
||||||
Ok(song) => {
|
Ok(song) => {
|
||||||
let directory = crate::environment::get_root_directory().await.unwrap();
|
let directory = icarus_envy::environment::get_root_directory().await;
|
||||||
let dir = std::path::Path::new(&directory);
|
let dir = std::path::Path::new(&directory);
|
||||||
|
|
||||||
// TODO: Make this random and the file extension should not be hard coded
|
// TODO: Make this random and the file extension should not be hard coded
|
||||||
|
|||||||
+1
-1
@@ -770,7 +770,7 @@ pub mod endpoint {
|
|||||||
let mut song = payload.to_song();
|
let mut song = payload.to_song();
|
||||||
song.filename =
|
song.filename =
|
||||||
song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
||||||
song.directory = crate::environment::get_root_directory().await.unwrap();
|
song.directory = icarus_envy::environment::get_root_directory().await;
|
||||||
|
|
||||||
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
pub async fn get_db_url() -> String {
|
|
||||||
dotenvy::dotenv().ok();
|
|
||||||
std::env::var(crate::keys::DBURL).expect(crate::keys::error::ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_root_directory() -> Result<String, std::env::VarError> {
|
|
||||||
dotenvy::dotenv().ok();
|
|
||||||
std::env::var(crate::keys::ROOT_DIRECTORY)
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
pub const DBURL: &str = "DATABASE_URL";
|
|
||||||
pub const ROOT_DIRECTORY: &str = "ROOT_DIRECTORY";
|
|
||||||
|
|
||||||
pub mod error {
|
|
||||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
|
||||||
}
|
|
||||||
+19
-32
@@ -1,6 +1,4 @@
|
|||||||
pub mod callers;
|
pub mod callers;
|
||||||
pub mod environment;
|
|
||||||
pub mod keys;
|
|
||||||
|
|
||||||
pub mod db {
|
pub mod db {
|
||||||
|
|
||||||
@@ -11,7 +9,7 @@ pub mod db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
let database_url = crate::environment::get_db_url().await;
|
let database_url = icarus_envy::environment::get_db_url().await;
|
||||||
println!("Database url: {:?}", database_url);
|
println!("Database url: {:?}", database_url);
|
||||||
|
|
||||||
PgPoolOptions::new()
|
PgPoolOptions::new()
|
||||||
@@ -166,26 +164,23 @@ mod tests {
|
|||||||
mod db_mgr {
|
mod db_mgr {
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::keys;
|
|
||||||
|
|
||||||
pub const LIMIT: usize = 6;
|
pub const LIMIT: usize = 6;
|
||||||
|
|
||||||
pub async fn get_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn get_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenvy::dotenv().ok();
|
let tm_db_url = icarus_envy::environment::get_db_url().await;
|
||||||
let tm_db_url = crate::environment::get_db_url().await;
|
|
||||||
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap();
|
||||||
sqlx::PgPool::connect_with(tm_options).await
|
sqlx::PgPool::connect_with(tm_options).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn generate_db_name() -> String {
|
pub async fn generate_db_name() -> String {
|
||||||
let db_name =
|
let db_name = get_database_name().await.unwrap()
|
||||||
get_database_name().unwrap() + &"_" + &uuid::Uuid::new_v4().to_string()[..LIMIT];
|
+ &"_"
|
||||||
|
+ &uuid::Uuid::new_v4().to_string()[..LIMIT];
|
||||||
db_name
|
db_name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenvy::dotenv().ok();
|
let db_url = icarus_envy::environment::get_db_url().await;
|
||||||
let db_url = crate::environment::get_db_url().await;
|
|
||||||
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||||
sqlx::PgPool::connect_with(options).await
|
sqlx::PgPool::connect_with(options).await
|
||||||
}
|
}
|
||||||
@@ -211,29 +206,21 @@ mod tests {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
pub async fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
dotenvy::dotenv().ok(); // Load .env file if it exists
|
let database_url = icarus_envy::environment::get_db_url().await;
|
||||||
|
let parsed_url = url::Url::parse(&database_url)?;
|
||||||
|
|
||||||
match std::env::var(keys::DBURL) {
|
if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {
|
||||||
Ok(database_url) => {
|
match parsed_url
|
||||||
let parsed_url = url::Url::parse(&database_url)?;
|
.path_segments()
|
||||||
if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {
|
.and_then(|segments| segments.last().map(|s| s.to_string()))
|
||||||
match parsed_url
|
{
|
||||||
.path_segments()
|
Some(sss) => Ok(sss),
|
||||||
.and_then(|segments| segments.last().map(|s| s.to_string()))
|
None => Err("Error parsing".into()),
|
||||||
{
|
|
||||||
Some(sss) => Ok(sss),
|
|
||||||
None => Err("Error parsing".into()),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Handle other database types if needed
|
|
||||||
Err("Error parsing".into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// DATABASE_URL environment variable not found
|
|
||||||
Err("Error parsing".into())
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Handle other database types if needed
|
||||||
|
Err("Error parsing".into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user