64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
#[macro_use]
|
|
extern crate justerror;
|
|
|
|
#[cfg(test)]
|
|
pub mod test_utils;
|
|
|
|
/// This is used in the bin crate and in tests.
|
|
pub use db::get_db_pool;
|
|
|
|
// 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, session_secret: &[u8]) -> axum::Router {
|
|
use axum::{middleware, routing::get};
|
|
let session_layer = db::session_layer(db_pool.clone(), session_secret).await;
|
|
let auth_layer = db::auth_layer(db_pool.clone(), session_secret).await;
|
|
|
|
// 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))
|
|
.route("/login", get(get_login).post(post_login))
|
|
.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)
|
|
.post(post_add_watch),
|
|
)
|
|
.fallback(handle_slash_redir)
|
|
.layer(middleware::from_fn_with_state(
|
|
db_pool.clone(),
|
|
users::handle_update_last_seen,
|
|
))
|
|
.layer(auth_layer)
|
|
.layer(session_layer)
|
|
.with_state(db_pool)
|
|
}
|