Merge branch 'main' into tests
This commit is contained in:
commit
bfda5a6fbb
3 changed files with 48 additions and 31 deletions
28
src/lib.rs
28
src/lib.rs
|
@ -1,7 +1,12 @@
|
|||
#[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;
|
||||
|
||||
|
@ -14,3 +19,26 @@ pub mod users;
|
|||
pub(crate) mod util;
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -1,15 +1,8 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{middleware, routing::get, Router};
|
||||
use rand_core::{OsRng, RngCore};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
use witch_watch::{
|
||||
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,
|
||||
};
|
||||
use witch_watch::db;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -30,29 +23,11 @@ async fn main() {
|
|||
bytes
|
||||
};
|
||||
|
||||
let session_layer = db::session_layer(pool.clone(), &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 app = witch_watch::app(pool, &secret).await;
|
||||
|
||||
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))
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
|
|
18
src/users.rs
18
src/users.rs
|
@ -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_login::{secrecy::SecretVec, AuthUser};
|
||||
|
@ -78,7 +81,18 @@ pub async fn handle_update_last_seen<BodyT>(
|
|||
next: Next<BodyT>,
|
||||
) -> impl IntoResponse {
|
||||
if let Some(user) = auth.current_user {
|
||||
user.update_last_seen(&pool).await;
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
user.update_last_seen(&pool).await;
|
||||
}
|
||||
}
|
||||
next.run(request).await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue