Compare commits
7 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
5b0592f51d | |||
7e189e84d8 | |||
79f6ebdc09 | |||
68a9998572 | |||
b6787de66b | |||
4d3415acf2 | |||
c9873d95d7 |
2
.env.sample
Normal file
2
.env.sample
Normal file
@@ -0,0 +1,2 @@
|
||||
DATABASE_URL=postgres://username:password@localhost/database_name
|
||||
TEST_DATABASE_URL=postgres://username:password@localhost/database_name_test
|
@@ -32,12 +32,69 @@ jobs:
|
||||
test:
|
||||
name: Test Suite
|
||||
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:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
- run: |
|
||||
# --- Add this step for explicit verification ---
|
||||
- name: Verify Docker Environment
|
||||
run: |
|
||||
echo "Runner User Info:"
|
||||
id
|
||||
echo "Checking Docker Version:"
|
||||
docker --version
|
||||
echo "Checking Docker Daemon Status (info):"
|
||||
docker info
|
||||
echo "Checking Docker Daemon Status (ps):"
|
||||
docker ps -a
|
||||
echo "Docker environment check complete."
|
||||
# NOTE: Do NOT use continue-on-error here.
|
||||
# If Docker isn't working as expected, the job SHOULD fail here.
|
||||
# --- 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
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
.env
|
||||
|
11
Cargo.toml
11
Cargo.toml
@@ -8,5 +8,14 @@ axum = { version = "0.8.3" }
|
||||
serde = { version = "1.0.218", features = ["derive"] }
|
||||
serde_json = { version = "1.0.139" }
|
||||
tokio = { version = "1.44.1", features = ["rt-multi-thread"] }
|
||||
tracing-subscriber = "0.3.19"
|
||||
tracing-subscriber = { version = "0.3.19" }
|
||||
tower = { version = "0.5.2" }
|
||||
hyper = { version = "1.6.0" }
|
||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
||||
dotenvy = { version = "0.15.7" }
|
||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
||||
|
||||
[dev-dependencies]
|
||||
http-body-util = "0.1.3"
|
||||
reqwest = { version = "0.12.5", features = ["json"] } # For making HTTP requests in tests
|
||||
once_cell = "1.19" # Useful for lazy initialization in tests/app setup
|
||||
|
1
migrations/20250402221858_init_migrate.sql
Normal file
1
migrations/20250402221858_init_migrate.sql
Normal file
@@ -0,0 +1 @@
|
||||
-- Add migration script here
|
3
run_migrations.txt
Normal file
3
run_migrations.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
cargo install sqlx-cli
|
||||
sqlx migrate add init_migration
|
||||
sqlx migrate run
|
@@ -1,4 +1,30 @@
|
||||
use axum::{Extension, Json, http::StatusCode};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct TestResult {
|
||||
message: String,
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
pub async fn root() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
||||
|
||||
pub async fn db_ping(Extension(pool): Extension<sqlx::PgPool>) -> (StatusCode, Json<TestResult>) {
|
||||
match sqlx::query("SELECT 1").execute(&pool).await {
|
||||
Ok(_) => {
|
||||
let tr = TestResult {
|
||||
message: String::from("This works"),
|
||||
};
|
||||
(StatusCode::OK, Json(tr))
|
||||
}
|
||||
Err(e) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(TestResult {
|
||||
message: e.to_string(),
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@@ -3,5 +3,6 @@ pub mod register;
|
||||
|
||||
pub mod endpoints {
|
||||
pub const ROOT: &str = "/";
|
||||
pub const REGISTER: &str = "api/v2/register";
|
||||
pub const REGISTER: &str = "/api/v2/register";
|
||||
pub const DBTEST: &str = "/api/v2/test/db";
|
||||
}
|
||||
|
51
src/lib.rs
Normal file
51
src/lib.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
pub mod callers;
|
||||
pub mod config;
|
||||
pub mod models;
|
||||
|
||||
mod keys {
|
||||
pub const DBURL: &str = "DATABASE_URL";
|
||||
|
||||
pub mod error {
|
||||
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 {
|
||||
pub const MAXCONN: u32 = 5;
|
||||
}
|
||||
|
||||
pub mod db_pool {
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use std::env;
|
||||
|
||||
use crate::{connection_settings, keys};
|
||||
|
||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
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();
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
env::var(keys::test::DBURL).expect(keys::test::error::ERROR)
|
||||
} else {
|
||||
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||
}
|
||||
}
|
||||
}
|
89
src/main.rs
89
src/main.rs
@@ -2,26 +2,95 @@ use axum::{
|
||||
Router,
|
||||
routing::{get, post},
|
||||
};
|
||||
// use std::net::SocketAddr;
|
||||
|
||||
pub mod callers;
|
||||
pub mod config;
|
||||
pub mod models;
|
||||
use icarus_auth::callers;
|
||||
use icarus_auth::config;
|
||||
// use sqlx::Postgres;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
.route(callers::endpoints::ROOT, get(callers::common::root))
|
||||
.route(
|
||||
callers::endpoints::REGISTER,
|
||||
post(callers::register::register_user),
|
||||
);
|
||||
let app = app().await;
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let url = config::get_full();
|
||||
let listener = tokio::net::TcpListener::bind(url).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn routes() -> Router {
|
||||
// build our application with a route
|
||||
Router::new()
|
||||
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
||||
.route(callers::endpoints::ROOT, get(callers::common::root))
|
||||
.route(
|
||||
callers::endpoints::REGISTER,
|
||||
post(callers::register::register_user),
|
||||
)
|
||||
}
|
||||
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::{
|
||||
body::Body,
|
||||
// extract::connect_info::MockConnectInfo,
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use http_body_util::BodyExt;
|
||||
// use http_body_util::BodyExt; // for `collect`
|
||||
// use serde_json::{Value, json};
|
||||
// use tokio::net::TcpListener;
|
||||
// use tower::{Service, ServiceExt}; // for `call`, `oneshot`, and `ready`
|
||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||
|
||||
#[tokio::test]
|
||||
async fn hello_world() {
|
||||
let app = app().await;
|
||||
|
||||
// `Router` implements `tower::Service<Request<Body>>` so we can
|
||||
// call it like any tower service, no need to run an HTTP server.
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(callers::endpoints::ROOT)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
/*
|
||||
match response.into_body().collect().await {
|
||||
Ok(o) => {
|
||||
let parsed: String = match String::from_utf8(o.to_bytes()) {
|
||||
Ok(s) => s,
|
||||
Err(err) => {
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false,
|
||||
"Error: {:?}", err.to_string());
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
assert_eq!(&body[..], b"Hello, World!");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user