Moon and retrograde calendars, "today in the sky" on the own chart
New public pages: /holdnaptar (new and full moons, quarters, eclipses and the Moon's sign changes, month by month, with the Moon of today) and /retrograd (the retrograde periods of Mercury to Pluto with their shadows, and which planets are retrograde now), each with a page per year around the current one. The own chart gets a "Ma az égen" card: the Moon's sign, phase and natal house, the tightest transits to the chart in fixed sentences, and the retrograde planets. No AI, nothing stored. The events come from astro-engine (sampling and bisection, checked against swetest and the 2026 eclipses) through horoscope-api GET /v1/sky and /v1/sky/events; the backend serves them at /v1/public/sky… and keeps them in memory (the sky now for five minutes, a year's events for good). Links in the mobile menu, the footer, the sitemap and the free tools on the home page; the desktop header is unchanged (it is full at 1280 px). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -56,6 +56,8 @@ pub struct AppState {
|
||||
pub stripe: stripe::Client,
|
||||
/// Uploaded photos; `None` when no object storage is configured.
|
||||
pub storage: Option<storage::Storage>,
|
||||
/// The sky by date, the same for everyone (see `routes::sky`).
|
||||
pub sky: routes::sky::SkyCache,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -71,6 +73,7 @@ impl AppState {
|
||||
models: models::Catalog::new(&config.openrouter_models_url),
|
||||
stripe: stripe::Client::default(),
|
||||
storage: config.storage.clone().map(storage::Storage::new),
|
||||
sky: Default::default(),
|
||||
db,
|
||||
config,
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ mod prompts;
|
||||
mod questions;
|
||||
mod recalculate;
|
||||
mod shares;
|
||||
pub mod sky;
|
||||
mod synastries;
|
||||
mod tarot;
|
||||
|
||||
@@ -130,6 +131,8 @@ pub fn router(state: Arc<AppState>) -> Router {
|
||||
.route("/v1/public/chart", post(calculator::chart))
|
||||
.route("/v1/public/geo/countries", get(calculator::countries))
|
||||
.route("/v1/public/geo/places", get(calculator::places))
|
||||
.route("/v1/public/sky", get(sky::now))
|
||||
.route("/v1/public/sky/events", get(sky::events))
|
||||
.route("/v1/public/celebrities", get(celebrities::public_list))
|
||||
.route("/v1/public/celebrities/{slug}", get(celebrities::public_view))
|
||||
.route("/v1/public/photos/{*key}", get(celebrities::photo))
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
//! The sky by date for the public calendars (`/holdnaptar`, `/retrograd`) and
|
||||
//! the "today in the sky" card: the horoscope service's answer, the same for
|
||||
//! everyone, kept in memory (the sky now for a few minutes, a year's events
|
||||
//! for good), so page views and repeated calls do not recalculate it.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::{Query, State};
|
||||
use axum_extra::extract::WithRejection;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use super::St;
|
||||
use crate::error::{ApiError, ApiResult};
|
||||
|
||||
/// How long the sky "now" is reused.
|
||||
const NOW_TTL: Duration = Duration::from_secs(5 * 60);
|
||||
/// Years served around the current one.
|
||||
const YEARS_AROUND: i32 = 5;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SkyCache {
|
||||
now: Mutex<Option<(Instant, Value)>>,
|
||||
years: Mutex<HashMap<i32, Value>>,
|
||||
}
|
||||
|
||||
pub async fn now(State(state): St) -> ApiResult<Json<Value>> {
|
||||
if let Some((at, sky)) = state.sky.now.lock().unwrap().as_ref()
|
||||
&& at.elapsed() < NOW_TTL
|
||||
{
|
||||
return Ok(Json(sky.clone()));
|
||||
}
|
||||
let sky = state.horoscope.sky().await?;
|
||||
*state.sky.now.lock().unwrap() = Some((Instant::now(), sky.clone()));
|
||||
Ok(Json(sky))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct EventsQuery {
|
||||
year: i32,
|
||||
}
|
||||
|
||||
pub async fn events(
|
||||
State(state): St,
|
||||
WithRejection(Query(query), _): WithRejection<Query<EventsQuery>, ApiError>,
|
||||
) -> ApiResult<Json<Value>> {
|
||||
let current = OffsetDateTime::now_utc().year();
|
||||
if (query.year - current).abs() > YEARS_AROUND {
|
||||
return Err(ApiError::BadRequest(format!(
|
||||
"year must be between {} and {}",
|
||||
current - YEARS_AROUND,
|
||||
current + YEARS_AROUND
|
||||
)));
|
||||
}
|
||||
if let Some(events) = state.sky.years.lock().unwrap().get(&query.year) {
|
||||
return Ok(Json(events.clone()));
|
||||
}
|
||||
let events = state.horoscope.sky_events(query.year).await?;
|
||||
let mut years = state.sky.years.lock().unwrap();
|
||||
// Years that fell out of the window as time went on.
|
||||
years.retain(|y, _| (y - current).abs() <= YEARS_AROUND);
|
||||
years.insert(query.year, events.clone());
|
||||
Ok(Json(events))
|
||||
}
|
||||
@@ -90,6 +90,9 @@ async fn fake_forecast(Json(body): Json<Value>) -> (StatusCode, Json<Value>) {
|
||||
)
|
||||
}
|
||||
|
||||
/// Calls of the fake sky endpoints (only `the_sky_is_shared_and_kept` uses them).
|
||||
static SKY_CALLS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
|
||||
async fn fake_jwks() -> Json<Value> {
|
||||
Json(
|
||||
json!({"keys": [{"kid": "test-key", "kty": "RSA", "alg": "RS256", "use": "sig", "n": GOOGLE_KEY_N.trim(), "e": "AQAB"}]}),
|
||||
@@ -221,6 +224,20 @@ async fn fake_services_with(s3: FakeS3) -> String {
|
||||
)
|
||||
.route("/v1/synastry", post(fake_synastry))
|
||||
.route("/v1/forecast", post(fake_forecast))
|
||||
.route(
|
||||
"/v1/sky",
|
||||
get(|| async {
|
||||
let n = SKY_CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
Json(json!({"call": n, "planets": [], "moon": {"phase": "full_moon"}}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/v1/sky/events",
|
||||
get(|axum::extract::Query(q): axum::extract::Query<std::collections::HashMap<String, String>>| async move {
|
||||
SKY_CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
Json(json!({"year": q["year"].parse::<i32>().unwrap(), "lunations": []}))
|
||||
}),
|
||||
)
|
||||
.route("/jwks", get(fake_jwks));
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
@@ -1009,7 +1026,7 @@ async fn export_and_delete_account(pool: PgPool) {
|
||||
/// config and catalog, shared links, invitation pages, the waitlist and
|
||||
/// unsubscribing. Every other route (charts, calculations, analyses) needs a
|
||||
/// logged-in user, so other apps cannot use them.
|
||||
const PUBLIC_ROUTES: [&str; 22] = [
|
||||
const PUBLIC_ROUTES: [&str; 24] = [
|
||||
"/healthz",
|
||||
// Stripe calls it; the signature proves it.
|
||||
"/v1/payments/stripe/webhook",
|
||||
@@ -1035,6 +1052,9 @@ const PUBLIC_ROUTES: [&str; 22] = [
|
||||
"/v1/public/celebrities",
|
||||
"/v1/public/celebrities/{slug}",
|
||||
"/v1/public/photos/{*key}",
|
||||
// The sky by date, the same for everyone (the moon and retrograde calendars).
|
||||
"/v1/public/sky",
|
||||
"/v1/public/sky/events",
|
||||
];
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
@@ -3237,3 +3257,29 @@ async fn the_user_list_pages_with_a_total(pool: PgPool) {
|
||||
assert_eq!(r.body["total"].as_i64(), Some(1));
|
||||
assert_eq!(r.body["stats"], json!({ "users": 5, "buyers": 1, "test_buyers": 1 }));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn the_sky_is_shared_and_kept(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
let calls = || SKY_CALLS.load(std::sync::atomic::Ordering::Relaxed);
|
||||
let before = calls();
|
||||
// No login needed; the second call is answered from memory.
|
||||
let r = app.get("/v1/public/sky", None).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!(r.body["moon"]["phase"], "full_moon");
|
||||
let again = app.get("/v1/public/sky", None).await;
|
||||
assert_eq!(again.body, r.body);
|
||||
assert_eq!(calls() - before, 1);
|
||||
|
||||
let year = time::OffsetDateTime::now_utc().year();
|
||||
let r = app.get(&format!("/v1/public/sky/events?year={}", year + 1), None).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
assert_eq!(r.body["year"], year + 1);
|
||||
app.get(&format!("/v1/public/sky/events?year={}", year + 1), None).await;
|
||||
assert_eq!(calls() - before, 2);
|
||||
for bad in [format!("year={}", year + 6), format!("year={}", year - 6), "year=abc".into(), String::new()] {
|
||||
let r = app.get(&format!("/v1/public/sky/events?{bad}"), None).await;
|
||||
assert_eq!(r.status, StatusCode::BAD_REQUEST, "{bad}: {}", r.body);
|
||||
}
|
||||
assert_eq!(calls() - before, 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user