diff --git a/src/db.rs b/src/db.rs index a6e374c..7e9d39e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -20,7 +20,7 @@ const MIN_CONNS: u32 = 5; const TIMEOUT: u64 = 11; const SESSION_TTL: Duration = Duration::from_secs((365.2422 * 24. * 3600.0) as u64); -pub async fn get_pool() -> SqlitePool { +pub async fn get_db_pool() -> SqlitePool { let db_filename = { std::env::var("DATABASE_FILE").unwrap_or_else(|_| { #[cfg(not(test))] @@ -115,7 +115,7 @@ mod tests { #[tokio::test] async fn it_migrates_the_db() { - let db = super::get_pool().await; + let db = super::get_db_pool().await; let r = sqlx::query("select count(*) from witches") .fetch_one(&db) .await; @@ -130,6 +130,7 @@ mod tests { //-************************************************************************ // Session store sub-module, not a public lib. //-************************************************************************ +#[allow(dead_code)] mod session_store { use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session}; use sqlx::{pool::PoolConnection, Sqlite}; diff --git a/src/generic_handlers.rs b/src/generic_handlers.rs index a344808..cbefcb1 100644 --- a/src/generic_handlers.rs +++ b/src/generic_handlers.rs @@ -26,7 +26,7 @@ mod test { #[tokio::test] async fn slash_is_ok() { - let pool = db::get_pool().await; + let pool = db::get_db_pool().await; let secret = [0u8; 64]; let app = crate::app(pool.clone(), &secret).await.into_make_service(); @@ -37,7 +37,7 @@ mod test { #[tokio::test] async fn not_found_is_303() { - let pool = db::get_pool().await; + let pool = db::get_db_pool().await; let secret = [0u8; 64]; let app = crate::app(pool, &secret).await.into_make_service(); diff --git a/src/lib.rs b/src/lib.rs index ccba9a6..482ba6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,40 +1,45 @@ #[macro_use] extern crate justerror; -use axum::{middleware, routing::get, Router}; -use axum_login::SqliteStore; -use generic_handlers::{handle_slash, handle_slash_redir}; -use login::{get_login, get_logout, post_login, post_logout}; -use signup::{get_create_user, handle_signup_success, post_create_user}; -use sqlx::SqlitePool; -pub use users::User; -use uuid::Uuid; - -pub mod db; -pub mod generic_handlers; -pub mod login; -pub mod signup; -pub(crate) mod templates; -pub mod users; -pub(crate) mod util; -pub mod watches; -pub use templates::*; -pub use watches::templates::*; -use watches::{ - handlers::{get_search_watch, get_watches, post_search_watch, put_add_watch}, - ShowKind, Watch, -}; - #[cfg(test)] pub mod test_utils; -pub type AuthContext = axum_login::extractors::AuthContext>; +/// This is used in the bin crate and in tests. +pub use db::get_db_pool; -pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router { +// everything else is private to the crate +mod db; +mod generic_handlers; +mod login; +mod signup; +mod templates; +mod users; +mod util; +mod watches; + +// things we want in the crate namespace +use templates::*; +use users::User; +use watches::{templates::*, ShowKind, Watch}; + +type AuthContext = + axum_login::extractors::AuthContext>; + +/// Returns the router to be used as a service or test object, you do you. +pub async fn app(db_pool: sqlx::SqlitePool, secret: &[u8]) -> axum::Router { + use axum::{middleware, routing::get}; let session_layer = db::session_layer(db_pool.clone(), secret).await; let auth_layer = db::auth_layer(db_pool.clone(), secret).await; - Router::new() + // don't bother bringing handlers into the whole crate namespace + use generic_handlers::{handle_slash, handle_slash_redir}; + use login::{get_login, get_logout, post_login, post_logout}; + use signup::{get_create_user, handle_signup_success, post_create_user}; + use watches::handlers::{ + get_search_watch, get_watches, post_add_watch, post_search_watch, put_add_watch, + }; + + axum::Router::new() .route("/", get(handle_slash).post(handle_slash)) .route("/signup", get(get_create_user).post(post_create_user)) .route("/signup_success/:id", get(handle_signup_success)) @@ -42,7 +47,12 @@ pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router { .route("/logout", get(get_logout).post(post_logout)) .route("/watches", get(get_watches)) .route("/search", get(get_search_watch).post(post_search_watch)) - .route("/add", get(get_search_watch).put(put_add_watch)) + .route( + "/add", + get(get_search_watch) + .put(put_add_watch) + .post(post_add_watch), + ) .fallback(handle_slash_redir) .layer(middleware::from_fn_with_state( db_pool.clone(), diff --git a/src/main.rs b/src/main.rs index 5aa68c4..9ee5d62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::net::SocketAddr; use rand_core::{OsRng, RngCore}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; -use witch_watch::db; +use witch_watch::get_db_pool; #[tokio::main] async fn main() { @@ -14,7 +14,7 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let pool = db::get_pool().await; + let pool = get_db_pool().await; let secret = { let mut bytes = [0u8; 64]; diff --git a/src/signup.rs b/src/signup.rs index 523e28e..ec1843b 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -232,7 +232,7 @@ mod test { use axum::http::StatusCode; use crate::{ - db::get_pool, + db::get_db_pool, templates::{CreateUser, CreateUserSuccess}, test_utils::{get_test_user, insert_user, massage, server_with_pool, FORM_CONTENT_TYPE}, User, @@ -242,7 +242,7 @@ mod test { #[tokio::test] async fn post_create_user() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(GOOD_FORM); @@ -262,7 +262,7 @@ mod test { #[tokio::test] async fn get_create_user() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let resp = server.get("/signup").await; @@ -273,7 +273,7 @@ mod test { #[tokio::test] async fn handle_signup_success() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let user = get_test_user(); @@ -309,7 +309,7 @@ mod test { #[tokio::test] async fn password_mismatch() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(PASSWORD_MISMATCH_FORM); @@ -332,7 +332,7 @@ mod test { #[tokio::test] async fn password_short() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(PASSWORD_SHORT_FORM); @@ -355,7 +355,7 @@ mod test { #[tokio::test] async fn password_long() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(PASSWORD_LONG_FORM); @@ -378,7 +378,7 @@ mod test { #[tokio::test] async fn username_short() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(USERNAME_SHORT_FORM); @@ -401,7 +401,7 @@ mod test { #[tokio::test] async fn username_long() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(USERNAME_LONG_FORM); @@ -424,7 +424,7 @@ mod test { #[tokio::test] async fn username_duplicate() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(GOOD_FORM); @@ -455,7 +455,7 @@ mod test { #[tokio::test] async fn displayname_long() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let body = massage(DISPLAYNAME_LONG_FORM); @@ -478,7 +478,7 @@ mod test { #[tokio::test] async fn handle_signup_success() { - let pool = get_pool().await; + let pool = get_db_pool().await; let server = server_with_pool(&pool).await; let path = format!("/signup_success/nope"); diff --git a/src/test_utils.rs b/src/test_utils.rs index 20ae1b8..94d6378 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -19,7 +19,7 @@ pub fn get_test_user() -> User { } pub async fn server() -> TestServer { - let pool = crate::db::get_pool().await; + let pool = crate::db::get_db_pool().await; let secret = [0u8; 64]; let user = get_test_user();