Compare commits
3 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
5b0592f51d | |||
7e189e84d8 | |||
79f6ebdc09 |
@@ -32,6 +32,22 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
name: Test Suite
|
name: Test Suite
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
|
# --- Add database service definition ---
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:17.4 # Or pin to a more specific version like 14.9
|
||||||
|
env:
|
||||||
|
# Use secrets for DB init, with fallbacks for flexibility
|
||||||
|
POSTGRES_USER: ${{ secrets.DB_TEST_USER || 'testuser' }}
|
||||||
|
POSTGRES_PASSWORD: ${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}
|
||||||
|
POSTGRES_DB: ${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
# Options to wait until the database is ready
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
@@ -51,7 +67,34 @@ jobs:
|
|||||||
echo "Docker environment check complete."
|
echo "Docker environment check complete."
|
||||||
# NOTE: Do NOT use continue-on-error here.
|
# NOTE: Do NOT use continue-on-error here.
|
||||||
# If Docker isn't working as expected, the job SHOULD fail here.
|
# If Docker isn't working as expected, the job SHOULD fail here.
|
||||||
- run: |
|
# --- Optional but Recommended: Database Migrations Step ---
|
||||||
|
- name: Run Database Migrations
|
||||||
|
env:
|
||||||
|
# Define TEST_DATABASE_URL using service details and secrets
|
||||||
|
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
# Make SSH agent available if migrations fetch private dependencies
|
||||||
|
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||||
|
run: |
|
||||||
|
echo "Running database migrations..."
|
||||||
|
# ===> IMPORTANT: Replace placeholder below with your actual migration command <===
|
||||||
|
# Example: Install and run sqlx-cli
|
||||||
|
# cargo install sqlx-cli --no-default-features --features native-tls,postgres
|
||||||
|
# sqlx database setup --database-url $TEST_DATABASE_URL
|
||||||
|
|
||||||
|
# Example: Install and run diesel_cli
|
||||||
|
# cargo install diesel_cli --no-default-features --features postgres
|
||||||
|
# diesel migration run --database-url $TEST_DATABASE_URL
|
||||||
|
|
||||||
|
# echo "[Placeholder] Your migration command goes here."
|
||||||
|
# ===> End of Placeholder <===
|
||||||
|
- name: Run tests
|
||||||
|
env:
|
||||||
|
# Define TEST_DATABASE_URL for tests to use
|
||||||
|
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||||
|
RUST_LOG: info # Optional: configure test log level
|
||||||
|
# Make SSH agent available if tests fetch private dependencies
|
||||||
|
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||||
|
run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||||
|
@@ -12,7 +12,7 @@ tracing-subscriber = { version = "0.3.19" }
|
|||||||
tower = { version = "0.5.2" }
|
tower = { version = "0.5.2" }
|
||||||
hyper = { version = "1.6.0" }
|
hyper = { version = "1.6.0" }
|
||||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
||||||
dotenv = { version = "0.15" }
|
dotenvy = { version = "0.15.7" }
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
22
src/lib.rs
22
src/lib.rs
@@ -8,6 +8,13 @@ mod keys {
|
|||||||
pub mod error {
|
pub mod error {
|
||||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod test {
|
||||||
|
pub const DBURL: &str = "TEST_DATABASE_URL";
|
||||||
|
pub mod error {
|
||||||
|
pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod connection_settings {
|
mod connection_settings {
|
||||||
@@ -22,12 +29,23 @@ pub mod db_pool {
|
|||||||
use crate::{connection_settings, keys};
|
use crate::{connection_settings, keys};
|
||||||
|
|
||||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenv::dotenv().ok();
|
let database_url = get_db_url().await;
|
||||||
let database_url = env::var(keys::DBURL).expect(keys::error::ERROR);
|
println!("Database url: {:?}", database_url);
|
||||||
|
|
||||||
PgPoolOptions::new()
|
PgPoolOptions::new()
|
||||||
.max_connections(connection_settings::MAXCONN)
|
.max_connections(connection_settings::MAXCONN)
|
||||||
.connect(&database_url)
|
.connect(&database_url)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_db_url() -> String {
|
||||||
|
#[cfg(debug_assertions)] // Example: Only load .env in debug builds
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
env::var(keys::test::DBURL).expect(keys::test::error::ERROR)
|
||||||
|
} else {
|
||||||
|
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
15
src/main.rs
15
src/main.rs
@@ -21,11 +21,7 @@ async fn main() {
|
|||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn app() -> Router {
|
async fn routes() -> Router {
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
|
||||||
.await
|
|
||||||
.expect("Failed to create pool");
|
|
||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
Router::new()
|
Router::new()
|
||||||
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
||||||
@@ -34,7 +30,14 @@ async fn app() -> Router {
|
|||||||
callers::endpoints::REGISTER,
|
callers::endpoints::REGISTER,
|
||||||
post(callers::register::register_user),
|
post(callers::register::register_user),
|
||||||
)
|
)
|
||||||
.layer(axum::Extension(pool))
|
}
|
||||||
|
|
||||||
|
async fn app() -> Router {
|
||||||
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create pool");
|
||||||
|
|
||||||
|
routes().await.layer(axum::Extension(pool))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Reference in New Issue
Block a user