chyabrung-cli 0.2.1

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//! Site statistics, cached for a minute.

use crate::models::{Post, User};
use chyabrung::prelude::*;
use std::time::Duration;

/// `GET /api/stats`
pub async fn index(State(db): State<Db>, State(cache): State<Cache>) -> Result<Json<serde_json::Value>> {
    let stats = cache
        .remember("stats", Some(Duration::from_secs(60)), || async {
            Ok(json!({ "users": User::query(&db).count().await?, "posts": Post::query(&db).count().await? }))
        })
        .await?;
    Ok(Json(stats))
}