All checks were successful
Rust Build / Check (pull_request) Successful in 47s
Rust Build / Test Suite (pull_request) Successful in 53s
Rust Build / Rustfmt (pull_request) Successful in 27s
Rust Build / Clippy (pull_request) Successful in 48s
Rust Build / build (pull_request) Successful in 1m28s
80 lines
2.1 KiB
Rust
80 lines
2.1 KiB
Rust
use axum::{
|
|
Router,
|
|
routing::{get, post},
|
|
};
|
|
|
|
use icarus_auth::callers;
|
|
use icarus_auth::config;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// initialize tracing
|
|
tracing_subscriber::fmt::init();
|
|
|
|
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");
|
|
|
|
// 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 on testcontainer DB");
|
|
|
|
routes().await.layer(axum::Extension(pool))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use axum::{
|
|
body::Body,
|
|
http::{Request, StatusCode},
|
|
};
|
|
use http_body_util::BodyExt;
|
|
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);
|
|
|
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
|
assert_eq!(&body[..], b"Hello, World!");
|
|
}
|
|
}
|