diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-06-19 11:47:31 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-06-20 22:26:27 -0400 |
| commit | aae24382399f755cfd80e352be7f5aa584aa5470 (patch) | |
| tree | 51c0cc3329bf4b94180a8f8418853eca7b1cc733 /src/boot | |
| parent | cd1dc0dab4b46bc5712070812192d5ce34071470 (diff) | |
Hoist heartbeat configuration out to the web handler.
The _snapshot_ is specifically a snapshot of app state. The purpose of the response struct is to annotate the snapshot with information that isn't from the app, but rather from the request or the web layer. The heartbeat timeout isn't ever used by the app layer in any way; it's used by the Axum handler for `/api/events`, instead.
I straight-up missed this when I wrote the original heartbeat changes.
Diffstat (limited to 'src/boot')
| -rw-r--r-- | src/boot/app.rs | 4 | ||||
| -rw-r--r-- | src/boot/handlers/boot/mod.rs | 20 | ||||
| -rw-r--r-- | src/boot/mod.rs | 11 |
3 files changed, 20 insertions, 15 deletions
diff --git a/src/boot/app.rs b/src/boot/app.rs index cd45c38..f531afe 100644 --- a/src/boot/app.rs +++ b/src/boot/app.rs @@ -3,7 +3,7 @@ use sqlx::sqlite::SqlitePool; use super::Snapshot; use crate::{ channel::{self, repo::Provider as _}, - event::{Heartbeat, repo::Provider as _}, + event::repo::Provider as _, message::repo::Provider as _, name, user::{self, repo::Provider as _}, @@ -21,7 +21,6 @@ impl<'a> Boot<'a> { pub async fn snapshot(&self) -> Result<Snapshot, Error> { let mut tx = self.db.begin().await?; let resume_point = tx.sequence().current().await?; - let heartbeat = Heartbeat::TIMEOUT; let users = tx.users().all(resume_point).await?; let channels = tx.channels().all(resume_point).await?; @@ -46,7 +45,6 @@ impl<'a> Boot<'a> { Ok(Snapshot { resume_point, - heartbeat, users, channels, messages, diff --git a/src/boot/handlers/boot/mod.rs b/src/boot/handlers/boot/mod.rs index 010f57b..49691f7 100644 --- a/src/boot/handlers/boot/mod.rs +++ b/src/boot/handlers/boot/mod.rs @@ -1,17 +1,26 @@ +use std::time::Duration; + use axum::{ extract::{Json, State}, response::{self, IntoResponse}, }; +use serde::Serialize; -use crate::{app::App, boot::Snapshot, error::Internal, token::extract::Identity, user::User}; +use crate::{ + app::App, boot::Snapshot, error::Internal, event::Heartbeat, token::extract::Identity, + user::User, +}; #[cfg(test)] mod test; pub async fn handler(State(app): State<App>, identity: Identity) -> Result<Response, Internal> { let snapshot = app.boot().snapshot().await?; + let heartbeat = Heartbeat::TIMEOUT; + Ok(Response { user: identity.user, + heartbeat, snapshot, }) } @@ -19,6 +28,8 @@ pub async fn handler(State(app): State<App>, identity: Identity) -> Result<Respo #[derive(serde::Serialize)] pub struct Response { pub user: User, + #[serde(serialize_with = "as_seconds")] + pub heartbeat: Duration, #[serde(flatten)] pub snapshot: Snapshot, } @@ -28,3 +39,10 @@ impl IntoResponse for Response { Json(self).into_response() } } + +fn as_seconds<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> +where + S: serde::Serializer, +{ + duration.as_secs().serialize(serializer) +} diff --git a/src/boot/mod.rs b/src/boot/mod.rs index 48da4f0..3fc2c9e 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -1,5 +1,3 @@ -use std::time::Duration; - use serde::Serialize; use crate::{channel::Channel, event::Sequence, message::Message, user::User}; @@ -10,16 +8,7 @@ pub mod handlers; #[derive(serde::Serialize)] pub struct Snapshot { pub resume_point: Sequence, - #[serde(serialize_with = "as_seconds")] - pub heartbeat: Duration, pub users: Vec<User>, pub channels: Vec<Channel>, pub messages: Vec<Message>, } - -fn as_seconds<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> -where - S: serde::Serializer, -{ - duration.as_secs().serialize(serializer) -} |
