// use serde::{Deserialize, Serialize}; pub mod callers; pub mod db { use sqlx::postgres::PgPoolOptions; use std::env; pub mod connection_settings { pub const MAXCONN: u32 = 10; } pub mod keys { pub const DBURL: &str = "DATABASE_URL"; pub mod error { pub const ERROR: &str = "DATABASE_URL must be set in .env"; } } // use crate::{connection_settings, keys}; pub async fn create_pool() -> Result { let database_url = get_db_url().await; println!("Database url: {:?}", database_url); PgPoolOptions::new() .max_connections(connection_settings::MAXCONN) .connect(&database_url) .await } async fn get_db_url() -> String { // #[cfg(debug_assertions)] // Example: Only load .env in debug builds dotenvy::dotenv().ok(); env::var(keys::DBURL).expect(keys::error::ERROR) } pub async fn migrations(pool: &sqlx::PgPool) { // Run migrations using the sqlx::migrate! macro // Assumes your migrations are in a ./migrations folder relative to Cargo.toml sqlx::migrate!("./migrations") .run(pool) .await .expect("Failed to run migrations"); } } #[tokio::main] async fn main() { // initialize tracing tracing_subscriber::fmt::init(); let pool = db::create_pool().await.expect("Failed to create pool"); db::migrations(&pool).await; // build our application with a route let app = init::app().await; // `GET /` goes to `root` // `POST /users` goes to `create_user` // .route("/users", post(create_user)); // run our app with hyper, listening globally on port 3000 let listener = tokio::net::TcpListener::bind(get_full()).await.unwrap(); axum::serve(listener, app).await.unwrap(); } pub mod init { use axum::routing::{get, post}; use std::time::Duration; use tower_http::timeout::TimeoutLayer; pub async fn routes() -> axum::Router { axum::Router::new() // `GET /` goes to `root` .route(crate::ROOT, get(crate::root)) .route( crate::callers::endpoints::QUEUESONG, post(crate::callers::song::endpoint::queue_song), ) } pub async fn app() -> axum::Router { let pool = crate::db::create_pool() .await .expect("Failed to create pool"); crate::db::migrations(&pool).await; routes() .await .layer(axum::Extension(pool)) .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) .layer(TimeoutLayer::new(Duration::from_secs(300))) } } fn get_full() -> String { get_address() + ":" + &get_port() } fn get_address() -> String { String::from("0.0.0.0") } fn get_port() -> String { String::from("3000") } pub const ROOT: &str = "/"; // basic handler that responds with a static string pub async fn root() -> &'static str { "Hello, World!" } #[cfg(test)] mod tests { use crate::db; // Might need later // use tower::ServiceExt; mod db_mgr { use std::str::FromStr; use crate::db; pub const LIMIT: usize = 6; pub async fn get_pool() -> Result { dotenvy::dotenv().ok(); let tm_db_url = std::env::var(db::keys::DBURL).expect("DATABASE_URL must be present"); let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap(); sqlx::PgPool::connect_with(tm_options).await } pub async fn generate_db_name() -> String { let db_name = get_database_name().unwrap() + &"_" + &uuid::Uuid::new_v4().to_string()[..LIMIT]; db_name } pub async fn connect_to_db(db_name: &str) -> Result { dotenvy::dotenv().ok(); let db_url = std::env::var(db::keys::DBURL).expect("DATABASE_URL must be set for tests"); let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name); sqlx::PgPool::connect_with(options).await } pub async fn create_database( template_pool: &sqlx::PgPool, db_name: &str, ) -> Result<(), sqlx::Error> { let create_query = format!("CREATE DATABASE {}", db_name); match sqlx::query(&create_query).execute(template_pool).await { Ok(_) => Ok(()), Err(e) => Err(e), } } // Function to drop a database pub async fn drop_database( template_pool: &sqlx::PgPool, db_name: &str, ) -> Result<(), sqlx::Error> { let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name); sqlx::query(&drop_query).execute(template_pool).await?; Ok(()) } pub fn get_database_name() -> Result> { dotenvy::dotenv().ok(); // Load .env file if it exists match std::env::var(db::keys::DBURL) { Ok(database_url) => { let parsed_url = url::Url::parse(&database_url)?; if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" { match parsed_url .path_segments() .and_then(|segments| segments.last().map(|s| s.to_string())) { 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()) } } } } #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); let db_name = db_mgr::generate_db_name().await; match db_mgr::create_database(&tm_pool, &db_name).await { Ok(_) => { println!("Success"); } Err(err) => { assert!(false, "Error: {:?}", err); } } let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; let song_path = String::from("tests/Machine_gun/track01.flac"); let path = std::path::Path::new(&song_path); let file_bytes = include_bytes!("../tests/Machine_gun/track01.flac"); let file_part = axum_test::multipart::Part::bytes(file_bytes.as_slice()) .file_name(&"track01.flac") .mime_type(&"audio/flac"); let _multipart_form = axum_test::multipart::MultipartForm::new().add_part("file", file_part); let app = crate::init::routes().await.layer(axum::Extension(pool)); let server = axum_test::TestServer::new(app); match server { Ok(_ser) => {} Err(err) => { assert!(false, "Error: {:?}", err); } } match reqwest::multipart::Form::new().file("file", path).await { Ok(_form) => { let mut builder = axum::http::Request::builder(); builder = builder.method(axum::http::Method::POST); builder = builder.uri(crate::callers::endpoints::QUEUESONG); } Err(err) => { assert!(false, "Error: {:?}", err); } } // TODO: Add code to send request with multipart form data. Add a few flac files in the // tests directory /* let response = app .clone() .oneshot( axum::http::Request::builder() .method(axum::http::Method::POST) .header( axum::http::header::CONTENT_TYPE, format!( "multipart/form-data; boundary={}", multipart_body.boundary() ), ) .uri(crate::callers::endpoints::QUEUESONG) .body(axum::body::Body::from(multipart_bytes)), ) .await; */ let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } }