summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-11 22:44:07 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-11 23:51:19 -0500
commita20f3667a88affa0498e564cea17e9e795162bb8 (patch)
tree29797241f1bce86193a733f8cfdc49121f91ddad /src/lib.rs
parent61acc1f6e418d1d6947658424115af994a2689dd (diff)
feat: impl auth and modularize routers
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs50
1 files changed, 47 insertions, 3 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 13b05c0..8be6698 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,15 +1,42 @@
+use axum::{http::Uri, response::IntoResponse};
+use tower_http::{cors::CorsLayer, trace::TraceLayer};
+
pub use error::{Error, Result};
-pub use routes::init_router;
+pub mod api;
+pub mod auth;
pub mod error;
-pub mod model;
-pub mod routes;
pub mod state;
+pub mod utils;
+
+pub fn router(state: state::AppState) -> axum::Router {
+ axum::Router::new()
+ .nest("/api", api::router(state.clone()))
+ .nest("/auth", auth::router(state.clone()))
+ .fallback(fallback)
+ // TODO: do this correctly!
+ .layer(CorsLayer::permissive())
+ .layer(TraceLayer::new_for_http())
+}
+
+pub async fn fallback(uri: Uri) -> impl IntoResponse {
+ Error::RouteNotFound(uri)
+}
#[cfg(test)]
pub(crate) mod tests {
+ use crate::state::AppState;
+
+ use super::*;
+
use std::sync::Once;
+ use axum::{
+ body::Body,
+ http::{Request, StatusCode},
+ };
+ use sqlx::PgPool;
+ use tower::ServiceExt;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
pub type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@@ -28,4 +55,21 @@ pub(crate) mod tests {
std::env::set_var("JWT_SECRET", JWT_SECRET);
});
}
+
+ #[sqlx::test]
+ async fn test_fallback_not_found(pool: PgPool) -> TestResult {
+ setup_test_env();
+
+ let router = router(AppState { pool });
+
+ let request = Request::builder()
+ .uri("/does-not-exist")
+ .body(Body::empty())?;
+
+ let response = router.oneshot(request).await?;
+
+ assert_eq!(StatusCode::NOT_FOUND, response.status());
+
+ Ok(())
+ }
}