Compare commits

...

14 Commits

Author SHA1 Message Date
5b0592f51d Moved router code to its own function (#12)
All checks were successful
Release Tagging / release (push) Successful in 34s
Rust Build / Check (push) Successful in 46s
Rust Build / Rustfmt (push) Successful in 25s
Rust Build / Check (pull_request) Successful in 49s
Rust Build / Test Suite (pull_request) Successful in 58s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / Test Suite (push) Successful in 51s
Rust Build / Clippy (push) Successful in 46s
Rust Build / build (push) Successful in 1m9s
Rust Build / Clippy (pull_request) Successful in 43s
Rust Build / build (pull_request) Successful in 1m14s
Reviewed-on: #12
2025-04-03 16:31:54 +00:00
KD
7e189e84d8 Adding code to use test database when in debug mode (#10)
Some checks failed
Release Tagging / release (push) Successful in 38s
Rust Build / Test Suite (push) Has been cancelled
Rust Build / Rustfmt (push) Has been cancelled
Rust Build / Clippy (push) Has been cancelled
Rust Build / Check (push) Has been cancelled
Rust Build / build (push) Has been cancelled
Reviewed-on: #10
Co-authored-by: KD <kundeng94@gmail.com>
Co-committed-by: KD <kundeng94@gmail.com>
2025-04-03 16:26:36 +00:00
KD
79f6ebdc09 Moved router code to its own function
Some checks failed
Rust Build / Check (pull_request) Successful in 53s
Rust Build / Test Suite (pull_request) Failing after 58s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 57s
Rust Build / build (pull_request) Successful in 1m21s
2025-04-03 12:25:01 -04:00
68a9998572 Removing test file (#11)
Some checks failed
Release Tagging / release (push) Successful in 35s
Rust Build / Check (push) Successful in 42s
Rust Build / Test Suite (push) Failing after 57s
Rust Build / Rustfmt (push) Successful in 24s
Rust Build / Clippy (push) Successful in 50s
Rust Build / build (push) Successful in 1m9s
Reviewed-on: #11
2025-04-03 14:17:10 +00:00
KD
b6787de66b Removing test file
Some checks failed
Rust Build / Check (pull_request) Successful in 48s
Rust Build / Test Suite (pull_request) Failing after 54s
Rust Build / Rustfmt (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Successful in 53s
Rust Build / build (pull_request) Successful in 1m28s
2025-04-03 10:07:58 -04:00
KD
4d3415acf2 Added config file for db (#9)
Some checks failed
Release Tagging / release (push) Successful in 47s
Rust Build / Check (push) Successful in 53s
Rust Build / Test Suite (push) Failing after 54s
Rust Build / Rustfmt (push) Successful in 28s
Rust Build / Clippy (push) Successful in 50s
Rust Build / build (push) Successful in 1m5s
Reviewed-on: #9
Co-authored-by: KD <kundeng94@gmail.com>
Co-committed-by: KD <kundeng94@gmail.com>
2025-04-03 13:59:54 +00:00
c9873d95d7 Added test (#8)
All checks were successful
Release Tagging / release (push) Successful in 30s
Rust Build / Check (push) Successful in 32s
Rust Build / Test Suite (push) Successful in 40s
Rust Build / Rustfmt (push) Successful in 27s
Rust Build / Clippy (push) Successful in 32s
Rust Build / build (push) Successful in 40s
Reviewed-on: #8
Co-authored-by: phoenix <kundeng94@gmail.com>
Co-committed-by: phoenix <kundeng94@gmail.com>
2025-04-01 00:29:09 +00:00
f105de7c80 Separated the code (#7)
All checks were successful
Release Tagging / release (push) Successful in 26s
Rust Build / Check (push) Successful in 27s
Rust Build / Test Suite (push) Successful in 30s
Rust Build / Rustfmt (push) Successful in 23s
Rust Build / Clippy (push) Successful in 31s
Rust Build / build (push) Successful in 35s
Reviewed-on: #7
2025-03-31 23:23:50 +00:00
9b77a8dd78 Separated the code
All checks were successful
Rust Build / Check (pull_request) Successful in 33s
Rust Build / Test Suite (pull_request) Successful in 35s
Rust Build / Rustfmt (pull_request) Successful in 25s
Rust Build / Clippy (pull_request) Successful in 34s
Rust Build / build (pull_request) Successful in 43s
2025-03-31 19:16:15 -04:00
dda88ce0a0 Updated icarus_models (#6)
All checks were successful
Rust Build / Test Suite (push) Successful in 33s
Rust Build / Rustfmt (push) Successful in 25s
Release Tagging / release (push) Successful in 31s
Rust Build / Check (push) Successful in 33s
Rust Build / Clippy (push) Successful in 30s
Rust Build / build (push) Successful in 42s
Rust Build / Check (pull_request) Successful in 29s
Rust Build / Test Suite (pull_request) Successful in 37s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 31s
Rust Build / build (pull_request) Successful in 41s
Reviewed-on: #6
2025-03-30 17:55:16 +00:00
KD
5893710431 Updated icarus_models
All checks were successful
Rust Build / Check (pull_request) Successful in 42s
Rust Build / Test Suite (pull_request) Successful in 47s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / Clippy (pull_request) Successful in 44s
Rust Build / build (pull_request) Successful in 59s
2025-03-30 13:49:20 -04:00
0a678228dd Using action variable (#4)
All checks were successful
Release Tagging / release (push) Successful in 29s
Rust Build / Check (push) Successful in 30s
Rust Build / Test Suite (push) Successful in 34s
Rust Build / Rustfmt (push) Successful in 26s
Rust Build / Clippy (push) Successful in 31s
Rust Build / build (push) Successful in 38s
Rust Build / Check (pull_request) Successful in 29s
Rust Build / Test Suite (pull_request) Successful in 31s
Rust Build / Rustfmt (pull_request) Successful in 28s
Rust Build / Clippy (pull_request) Successful in 32s
Rust Build / build (pull_request) Successful in 37s
Reviewed-on: #4
2025-03-30 03:22:19 +00:00
bfc14c96a7 Using action variable
All checks were successful
Rust Build / Check (pull_request) Successful in 32s
Rust Build / Test Suite (pull_request) Successful in 29s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Clippy (pull_request) Successful in 33s
Rust Build / build (pull_request) Successful in 52s
2025-03-29 22:56:55 -04:00
f2005de668 Fix build warnings (#3)
All checks were successful
Rust Build / Check (push) Successful in 29s
Rust Build / Test Suite (push) Successful in 35s
Rust Build / Rustfmt (push) Successful in 25s
Rust Build / Clippy (push) Successful in 29s
Rust Build / build (push) Successful in 38s
Release Tagging / release (push) Successful in 28s
Rust Build / Check (pull_request) Successful in 38s
Rust Build / Test Suite (pull_request) Successful in 33s
Rust Build / Rustfmt (pull_request) Successful in 25s
Rust Build / Clippy (pull_request) Successful in 31s
Rust Build / build (pull_request) Successful in 38s
Reviewed-on: #3
Co-authored-by: phoenix <kundeng94@gmail.com>
Co-committed-by: phoenix <kundeng94@gmail.com>
2025-03-29 22:35:57 +00:00
14 changed files with 320 additions and 26 deletions

2
.env.sample Normal file
View File

@@ -0,0 +1,2 @@
DATABASE_URL=postgres://username:password@localhost/database_name
TEST_DATABASE_URL=postgres://username:password@localhost/database_name_test

View File

@@ -19,17 +19,90 @@ jobs:
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.85.0
- run: cargo check
- run: |
mkdir -p ~/.ssh
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
chmod 600 ~/.ssh/gitlab_deploy_key
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
eval $(ssh-agent -s)
ssh-add -v ~/.ssh/gitlab_deploy_key
cargo check
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: cargo test
# --- 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
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
eval $(ssh-agent -s)
ssh-add -v ~/.ssh/gitlab_deploy_key
cargo test
fmt:
name: Rustfmt
@@ -40,7 +113,15 @@ jobs:
with:
toolchain: 1.85.0
- run: rustup component add rustfmt
- run: cargo fmt --all -- --check
- run: |
mkdir -p ~/.ssh
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
chmod 600 ~/.ssh/gitlab_deploy_key
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
eval $(ssh-agent -s)
ssh-add -v ~/.ssh/gitlab_deploy_key
cargo fmt --all -- --check
clippy:
name: Clippy
@@ -51,7 +132,15 @@ jobs:
with:
toolchain: 1.85.0
- run: rustup component add clippy
- run: cargo clippy -- -D warnings
- run: |
mkdir -p ~/.ssh
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
chmod 600 ~/.ssh/gitlab_deploy_key
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
eval $(ssh-agent -s)
ssh-add -v ~/.ssh/gitlab_deploy_key
cargo clippy -- -D warnings
build:
name: build
@@ -61,5 +150,13 @@ jobs:
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: 1.85.0
- run: cargo build
- run: |
mkdir -p ~/.ssh
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
chmod 600 ~/.ssh/gitlab_deploy_key
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
eval $(ssh-agent -s)
ssh-add -v ~/.ssh/gitlab_deploy_key
cargo build --release

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/target
Cargo.lock
.env

View File

@@ -8,4 +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

View File

@@ -0,0 +1 @@
-- Add migration script here

3
run_migrations.txt Normal file
View File

@@ -0,0 +1,3 @@
cargo install sqlx-cli
sqlx migrate add init_migration
sqlx migrate run

30
src/callers/common.rs Normal file
View File

@@ -0,0 +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(),
}),
),
}
}

8
src/callers/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
pub mod common;
pub mod register;
pub mod endpoints {
pub const ROOT: &str = "/";
pub const REGISTER: &str = "/api/v2/register";
pub const DBTEST: &str = "/api/v2/test/db";
}

12
src/callers/register.rs Normal file
View File

@@ -0,0 +1,12 @@
use axum::{Json, http::StatusCode};
use crate::models;
pub async fn register_user(
Json(payload): Json<models::common::CreateUser>,
) -> (StatusCode, Json<models::common::User>) {
let user = models::common::User {
username: payload.username.clone(),
};
(StatusCode::CREATED, Json(user))
}

10
src/config/mod.rs Normal file
View File

@@ -0,0 +1,10 @@
pub 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")
}

51
src/lib.rs Normal file
View 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)
}
}
}

View File

@@ -1,39 +1,96 @@
use axum::{
Json, Router,
http::StatusCode,
Router,
routing::{get, post},
};
use serde::{Deserialize, Serialize};
// use std::net::SocketAddr;
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()
// `GET /` goes to `root`
.route("/", get(root));
// `POST /users` goes to `create_user`
// .route("/users", post(create_user));
let app = app().await;
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind(get_full()).await.unwrap();
let url = config::get_full();
let listener = tokio::net::TcpListener::bind(url).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
fn get_full() -> String {
get_address() + ":" + &get_port()
}
fn get_address() -> String {
String::from("0.0.0.0")
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),
)
}
fn get_port() -> String {
String::from("3000")
async fn app() -> Router {
let pool = icarus_auth::db_pool::create_pool()
.await
.expect("Failed to create pool");
routes().await.layer(axum::Extension(pool))
}
// basic handler that responds with a static string
async fn root() -> &'static str {
"Hello, World!"
#[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!");
}
}

11
src/models/common.rs Normal file
View File

@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
pub struct CreateUser {
pub username: String,
}
#[derive(Serialize)]
pub struct User {
pub username: String,
}

1
src/models/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod common;