Compare commits
6 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
4d3415acf2 | |||
c9873d95d7 | |||
f105de7c80 | |||
9b77a8dd78 | |||
dda88ce0a0 | |||
5893710431 |
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
|
@@ -37,6 +37,20 @@ jobs:
|
|||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.85.0
|
toolchain: 1.85.0
|
||||||
|
# --- 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.
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
.env
|
||||||
|
13
Cargo.toml
13
Cargo.toml
@@ -8,5 +8,14 @@ axum = { version = "0.8.3" }
|
|||||||
serde = { version = "1.0.218", features = ["derive"] }
|
serde = { version = "1.0.218", features = ["derive"] }
|
||||||
serde_json = { version = "1.0.139" }
|
serde_json = { version = "1.0.139" }
|
||||||
tokio = { version = "1.44.1", features = ["rt-multi-thread"] }
|
tokio = { version = "1.44.1", features = ["rt-multi-thread"] }
|
||||||
tracing-subscriber = "0.3.19"
|
tracing-subscriber = { version = "0.3.19" }
|
||||||
icarus-models = { git = "ssh://git@git.kundeng.us/phoenix/icarus-models.git", tag = "v0.1.14" }
|
tower = { version = "0.5.2" }
|
||||||
|
hyper = { version = "1.6.0" }
|
||||||
|
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] }
|
||||||
|
dotenv = { version = "0.15" }
|
||||||
|
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
|
30
src/callers/common.rs
Normal file
30
src/callers/common.rs
Normal 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
8
src/callers/mod.rs
Normal 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
12
src/callers/register.rs
Normal 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
10
src/config/mod.rs
Normal 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")
|
||||||
|
}
|
33
src/lib.rs
Normal file
33
src/lib.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
dotenv::dotenv().ok();
|
||||||
|
let database_url = env::var(keys::DBURL).expect(keys::error::ERROR);
|
||||||
|
|
||||||
|
PgPoolOptions::new()
|
||||||
|
.max_connections(connection_settings::MAXCONN)
|
||||||
|
.connect(&database_url)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
98
src/main.rs
98
src/main.rs
@@ -1,41 +1,93 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
// Json,
|
|
||||||
Router,
|
Router,
|
||||||
// http::StatusCode,
|
routing::{get, post},
|
||||||
routing::get,
|
|
||||||
// routing::{get, post},
|
|
||||||
};
|
};
|
||||||
// use serde::{Deserialize, Serialize};
|
// use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use icarus_auth::callers;
|
||||||
|
use icarus_auth::config;
|
||||||
|
// use sqlx::Postgres;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// initialize tracing
|
// initialize tracing
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
// build our application with a route
|
let app = app().await;
|
||||||
let app = Router::new()
|
|
||||||
// `GET /` goes to `root`
|
|
||||||
.route("/", get(root));
|
|
||||||
// `POST /users` goes to `create_user`
|
|
||||||
// .route("/users", post(create_user));
|
|
||||||
|
|
||||||
// run our app with hyper, listening globally on port 3000
|
// 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();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_full() -> String {
|
async fn app() -> Router {
|
||||||
get_address() + ":" + &get_port()
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
}
|
.await
|
||||||
fn get_address() -> String {
|
.expect("Failed to create pool");
|
||||||
String::from("0.0.0.0")
|
|
||||||
|
// 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),
|
||||||
|
)
|
||||||
|
.layer(axum::Extension(pool))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_port() -> String {
|
#[cfg(test)]
|
||||||
String::from("3000")
|
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`
|
||||||
|
|
||||||
// basic handler that responds with a static string
|
#[tokio::test]
|
||||||
async fn root() -> &'static str {
|
async fn hello_world() {
|
||||||
"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
11
src/models/common.rs
Normal 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
1
src/models/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod common;
|
153
tests/auth_tests.rs
Normal file
153
tests/auth_tests.rs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
extern crate icarus_auth;
|
||||||
|
|
||||||
|
use crate::icarus_auth::callers;
|
||||||
|
|
||||||
|
// use axum::Extension;
|
||||||
|
use axum::body::Body;
|
||||||
|
// use axum::response::Response;
|
||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
http::{Request, StatusCode},
|
||||||
|
routing::get,
|
||||||
|
};
|
||||||
|
// use hyper::client::conn;
|
||||||
|
// use sqlx::PgPool;
|
||||||
|
// use sqlx::postgres::{self, PgPoolOptions};
|
||||||
|
// use testcontainers_modules::testcontainers::runners::AsyncRunner;
|
||||||
|
// use hyper::client;
|
||||||
|
// use sqlx::postgres;
|
||||||
|
// use http::{Request, StatusCode};
|
||||||
|
// use serde_json::json;
|
||||||
|
// use tower::ServiceExt; // for `.oneshot()`
|
||||||
|
use tower::util::ServiceExt;
|
||||||
|
// use testcontainers_modules::testcontainers::core::client::
|
||||||
|
|
||||||
|
const TEST_DATABASE_URL_ENV: &str = "TEST_DATABASE_URL";
|
||||||
|
const DEFAULT_TEST_DATABASE_URL: &str =
|
||||||
|
"postgres://icarus_op_test:password@localhost:5432/icarus_auth_test";
|
||||||
|
|
||||||
|
static SETUP: std::sync::Once = std::sync::Once::new();
|
||||||
|
|
||||||
|
// Ensure tracing is initialized only once for all tests in this file
|
||||||
|
/*
|
||||||
|
static TRACING_INIT: Lazy<()> = Lazy::new(|| {
|
||||||
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
|
// Set default log level if not provided
|
||||||
|
unsafe {
|
||||||
|
std::env::set_var("RUST_LOG", "info,tower_http=debug,your_project_name=debug");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||||
|
.with_test_writer() // Write logs to the test output capture
|
||||||
|
.init();
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
async fn setup_database() -> sqlx::PgPool {
|
||||||
|
let database_url = std::env::var(TEST_DATABASE_URL_ENV)
|
||||||
|
.unwrap_or_else(|_| DEFAULT_TEST_DATABASE_URL.to_string());
|
||||||
|
let pool = sqlx::PgPool::connect(&database_url)
|
||||||
|
.await
|
||||||
|
.expect("Failed to connect to test database");
|
||||||
|
|
||||||
|
let migrator = sqlx::migrate::Migrator::new(std::path::Path::new("./migrations"))
|
||||||
|
.await
|
||||||
|
.expect("Failed to create migrator");
|
||||||
|
migrator.run(&pool).await.expect("Failed to run migrations");
|
||||||
|
|
||||||
|
// Seed here if needed
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_db_health() {
|
||||||
|
SETUP.call_once(|| {
|
||||||
|
tokio::runtime::Runtime::new().unwrap().block_on(async {
|
||||||
|
setup_database().await;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
async fn setup_test(pool: sqlx::PgPool) -> Router {
|
||||||
|
Router::new()
|
||||||
|
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
||||||
|
.layer(Extension(pool))
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_hello_world() {
|
||||||
|
let app = Router::new().route(callers::endpoints::ROOT, get(callers::common::root)); // Replace with your handler
|
||||||
|
|
||||||
|
let response = app
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.uri(callers::endpoints::ROOT)
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let body = String::from_utf8(
|
||||||
|
axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.to_vec(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(body, "Hello, World!");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[tokio::test]
|
||||||
|
async fn _test_db_health_check() {
|
||||||
|
let container = testcontainers_modules::postgres::Postgres::default()
|
||||||
|
.start()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let _host_ip = container.get_host().await.unwrap();
|
||||||
|
let port = 5432;
|
||||||
|
let host_port = container.get_host_port_ipv4(port).await.unwrap();
|
||||||
|
let conn_string = &format!(
|
||||||
|
"postgres://postgres:postgres@localhost:{}/postgres",
|
||||||
|
host_port
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Test Database: {}", conn_string);
|
||||||
|
|
||||||
|
let app = Router::new().route(callers::endpoints::DBTEST, get(callers::common::db_ping)); // Replace with your handler
|
||||||
|
|
||||||
|
let response = app
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.uri(callers::endpoints::DBTEST)
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
match PgPoolOptions::new().connect(conn_string).await {
|
||||||
|
Ok(_) => {
|
||||||
|
assert!(true, "Success");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err.to_string());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
*/
|
Reference in New Issue
Block a user