GET /time for client clock sync
Browse files at this commitb8eeecc5df5ce5d4cac1cb2c5c923e54f9479b19Parent: 221606f- feat
- GET /time for client clock sync
1 changed file +25 -0
- src/lib.rs +25 -0
src/lib.rsmodified+25 -0
| @@ -71,6 +71,7 @@ pub fn build_router(state: AppState) -> Router { | |||
| 71 | let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi()) | 71 | let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi()) |
| 72 | .route("/", get(health)) | 72 | .route("/", get(health)) |
| 73 | .routes(routes!(health)) | 73 | .routes(routes!(health)) |
| 74 | + .routes(routes!(server_time)) | ||
| 74 | .nest("/auth", web::auth::routes(&state.rate_limit)) | 75 | .nest("/auth", web::auth::routes(&state.rate_limit)) |
| 75 | .nest("/users", web::users::routes()) | 76 | .nest("/users", web::users::routes()) |
| 76 | .nest("/notes", web::notes::routes()) | 77 | .nest("/notes", web::notes::routes()) |
| @@ -141,3 +142,27 @@ fn cors_layer() -> CorsLayer { | |||
| 141 | async fn health() -> Json<serde_json::Value> { | 142 | async fn health() -> Json<serde_json::Value> { |
| 142 | Json(json!({ "status": "ok" })) | 143 | Json(json!({ "status": "ok" })) |
| 143 | } | 144 | } |
| 145 | + | ||
| 146 | +/// The server's current time. All API timestamps are UTC unix-milliseconds | ||
| 147 | +/// judged by this clock (session expiry included), so a frontend that renders | ||
| 148 | +/// countdowns or "is this in the past?" logic should not trust the device | ||
| 149 | +/// clock — fetch this once, keep `offset = now - Date.now()`, and add the | ||
| 150 | +/// offset to `Date.now()` whenever it needs the authoritative time. | ||
| 151 | +#[derive(serde::Serialize, utoipa::ToSchema)] | ||
| 152 | +struct TimeResponse { | ||
| 153 | + /// Current server time, UTC unix-milliseconds. | ||
| 154 | + #[schema(example = 1_752_275_000_000_i64)] | ||
| 155 | + now: i64, | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +#[utoipa::path( | ||
| 159 | + get, | ||
| 160 | + path = "/time", | ||
| 161 | + tag = "meta", | ||
| 162 | + responses((status = 200, description = "Current server time (UTC unix-milliseconds)", body = TimeResponse)), | ||
| 163 | +)] | ||
| 164 | +async fn server_time() -> Json<TimeResponse> { | ||
| 165 | + Json(TimeResponse { | ||
| 166 | + now: domain::timestamp::Timestamp::now().as_millis(), | ||
| 167 | + }) | ||
| 168 | +} | ||