Merge branch 'main' into tests

This commit is contained in:
Joe Ardent 2023-05-31 13:58:38 -07:00
commit bfda5a6fbb
3 changed files with 48 additions and 31 deletions

View file

@ -1,7 +1,12 @@
#[macro_use] #[macro_use]
extern crate justerror; extern crate justerror;
use axum::{middleware, routing::get, Router};
use axum_login::SqliteStore; 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; pub use users::User;
use uuid::Uuid; use uuid::Uuid;
@ -14,3 +19,26 @@ pub mod users;
pub(crate) mod util; pub(crate) mod util;
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>; pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router {
let session_layer = db::session_layer(db_pool.clone(), secret).await;
let auth_layer = db::auth_layer(db_pool.clone(), secret).await;
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).post(handle_signup_success),
)
.route("/login", get(get_login).post(post_login))
.route("/logout", get(get_logout).post(post_logout))
.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)
}

View file

@ -1,15 +1,8 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{middleware, routing::get, Router};
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::{ use witch_watch::db;
db,
generic_handlers::{handle_slash, handle_slash_redir},
login::{get_login, get_logout, post_login, post_logout},
signup::{get_create_user, handle_signup_success, post_create_user},
users,
};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -30,29 +23,11 @@ async fn main() {
bytes bytes
}; };
let session_layer = db::session_layer(pool.clone(), &secret).await; let app = witch_watch::app(pool, &secret).await;
let auth_layer = db::auth_layer(pool.clone(), &secret).await;
let app = 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).post(handle_signup_success),
)
.route("/login", get(get_login).post(post_login))
.route("/logout", get(get_logout).post(post_logout))
.fallback(handle_slash_redir)
.route_layer(middleware::from_fn_with_state(
pool.clone(),
users::handle_update_last_seen,
))
.layer(auth_layer)
.layer(session_layer)
.with_state(pool);
let addr = ([127, 0, 0, 1], 3000); let addr = ([127, 0, 0, 1], 3000);
tracing::debug!("binding to 0.0.0.0:3000"); tracing::debug!("binding to {addr:?}");
axum::Server::bind(&SocketAddr::from(addr)) axum::Server::bind(&SocketAddr::from(addr))
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await

View file

@ -1,4 +1,7 @@
use std::fmt::Display; use std::{
fmt::Display,
time::{SystemTime, UNIX_EPOCH},
};
use axum::{extract::State, http::Request, middleware::Next, response::IntoResponse}; use axum::{extract::State, http::Request, middleware::Next, response::IntoResponse};
use axum_login::{secrecy::SecretVec, AuthUser}; use axum_login::{secrecy::SecretVec, AuthUser};
@ -78,7 +81,18 @@ pub async fn handle_update_last_seen<BodyT>(
next: Next<BodyT>, next: Next<BodyT>,
) -> impl IntoResponse { ) -> impl IntoResponse {
if let Some(user) = auth.current_user { if let Some(user) = auth.current_user {
if let Some(then) = user.last_seen {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
// The Nyquist frequency for 1-day tracking resolution is 12 hours.
if now - then > 12 * 3600 {
user.update_last_seen(&pool).await; user.update_last_seen(&pool).await;
} }
} else {
user.update_last_seen(&pool).await;
}
}
next.run(request).await next.run(request).await
} }