minor mod and visibility re-org

This commit is contained in:
Joe Ardent 2023-06-09 12:51:06 -07:00
parent c44c89005c
commit dd9a32dfd7
6 changed files with 58 additions and 47 deletions

View file

@ -20,7 +20,7 @@ const MIN_CONNS: u32 = 5;
const TIMEOUT: u64 = 11; const TIMEOUT: u64 = 11;
const SESSION_TTL: Duration = Duration::from_secs((365.2422 * 24. * 3600.0) as u64); 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 = { let db_filename = {
std::env::var("DATABASE_FILE").unwrap_or_else(|_| { std::env::var("DATABASE_FILE").unwrap_or_else(|_| {
#[cfg(not(test))] #[cfg(not(test))]
@ -115,7 +115,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn it_migrates_the_db() { 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") let r = sqlx::query("select count(*) from witches")
.fetch_one(&db) .fetch_one(&db)
.await; .await;
@ -130,6 +130,7 @@ mod tests {
//-************************************************************************ //-************************************************************************
// Session store sub-module, not a public lib. // Session store sub-module, not a public lib.
//-************************************************************************ //-************************************************************************
#[allow(dead_code)]
mod session_store { mod session_store {
use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session}; use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session};
use sqlx::{pool::PoolConnection, Sqlite}; use sqlx::{pool::PoolConnection, Sqlite};

View file

@ -26,7 +26,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn slash_is_ok() { async fn slash_is_ok() {
let pool = db::get_pool().await; let pool = db::get_db_pool().await;
let secret = [0u8; 64]; let secret = [0u8; 64];
let app = crate::app(pool.clone(), &secret).await.into_make_service(); let app = crate::app(pool.clone(), &secret).await.into_make_service();
@ -37,7 +37,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn not_found_is_303() { async fn not_found_is_303() {
let pool = db::get_pool().await; let pool = db::get_db_pool().await;
let secret = [0u8; 64]; let secret = [0u8; 64];
let app = crate::app(pool, &secret).await.into_make_service(); let app = crate::app(pool, &secret).await.into_make_service();

View file

@ -1,40 +1,45 @@
#[macro_use] #[macro_use]
extern crate justerror; 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)] #[cfg(test)]
pub mod test_utils; pub mod test_utils;
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>; /// 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<uuid::Uuid, User, axum_login::SqliteStore<User>>;
/// 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 session_layer = db::session_layer(db_pool.clone(), secret).await;
let auth_layer = db::auth_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("/", get(handle_slash).post(handle_slash))
.route("/signup", get(get_create_user).post(post_create_user)) .route("/signup", get(get_create_user).post(post_create_user))
.route("/signup_success/:id", get(handle_signup_success)) .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("/logout", get(get_logout).post(post_logout))
.route("/watches", get(get_watches)) .route("/watches", get(get_watches))
.route("/search", get(get_search_watch).post(post_search_watch)) .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) .fallback(handle_slash_redir)
.layer(middleware::from_fn_with_state( .layer(middleware::from_fn_with_state(
db_pool.clone(), db_pool.clone(),

View file

@ -2,7 +2,7 @@ use std::net::SocketAddr;
use rand_core::{OsRng, RngCore}; use rand_core::{OsRng, RngCore};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use witch_watch::db; use witch_watch::get_db_pool;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -14,7 +14,7 @@ async fn main() {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let pool = db::get_pool().await; let pool = get_db_pool().await;
let secret = { let secret = {
let mut bytes = [0u8; 64]; let mut bytes = [0u8; 64];

View file

@ -232,7 +232,7 @@ mod test {
use axum::http::StatusCode; use axum::http::StatusCode;
use crate::{ use crate::{
db::get_pool, db::get_db_pool,
templates::{CreateUser, CreateUserSuccess}, templates::{CreateUser, CreateUserSuccess},
test_utils::{get_test_user, insert_user, massage, server_with_pool, FORM_CONTENT_TYPE}, test_utils::{get_test_user, insert_user, massage, server_with_pool, FORM_CONTENT_TYPE},
User, User,
@ -242,7 +242,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn post_create_user() { async fn post_create_user() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(GOOD_FORM); let body = massage(GOOD_FORM);
@ -262,7 +262,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn get_create_user() { async fn get_create_user() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let resp = server.get("/signup").await; let resp = server.get("/signup").await;
@ -273,7 +273,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn handle_signup_success() { async fn handle_signup_success() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let user = get_test_user(); let user = get_test_user();
@ -309,7 +309,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn password_mismatch() { async fn password_mismatch() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(PASSWORD_MISMATCH_FORM); let body = massage(PASSWORD_MISMATCH_FORM);
@ -332,7 +332,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn password_short() { async fn password_short() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(PASSWORD_SHORT_FORM); let body = massage(PASSWORD_SHORT_FORM);
@ -355,7 +355,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn password_long() { async fn password_long() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(PASSWORD_LONG_FORM); let body = massage(PASSWORD_LONG_FORM);
@ -378,7 +378,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn username_short() { async fn username_short() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(USERNAME_SHORT_FORM); let body = massage(USERNAME_SHORT_FORM);
@ -401,7 +401,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn username_long() { async fn username_long() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(USERNAME_LONG_FORM); let body = massage(USERNAME_LONG_FORM);
@ -424,7 +424,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn username_duplicate() { async fn username_duplicate() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(GOOD_FORM); let body = massage(GOOD_FORM);
@ -455,7 +455,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn displayname_long() { async fn displayname_long() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let body = massage(DISPLAYNAME_LONG_FORM); let body = massage(DISPLAYNAME_LONG_FORM);
@ -478,7 +478,7 @@ mod test {
#[tokio::test] #[tokio::test]
async fn handle_signup_success() { async fn handle_signup_success() {
let pool = get_pool().await; let pool = get_db_pool().await;
let server = server_with_pool(&pool).await; let server = server_with_pool(&pool).await;
let path = format!("/signup_success/nope"); let path = format!("/signup_success/nope");

View file

@ -19,7 +19,7 @@ pub fn get_test_user() -> User {
} }
pub async fn server() -> TestServer { 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 secret = [0u8; 64];
let user = get_test_user(); let user = get_test_user();