Migrate `/v1/stats` route

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2022-11-24 12:42:21 +00:00
parent e5e8a403fc
commit b96f357c2e
No known key found for this signature in database
GPG Key ID: 9FACFECE1E3B3994
3 changed files with 32 additions and 1 deletions

View File

@ -121,6 +121,11 @@ async fn root_handler(Extension(ctx): Extension<Context>) -> impl IntoResponse {
"href": "/v1/delegates/:did/projects",
"rel": "projects",
"type": "GET"
},
{
"href": "/v1/stats",
"rel": "stats",
"type": "GET"
}
]
});

View File

@ -2,6 +2,7 @@ mod delegates;
mod node;
mod projects;
mod sessions;
mod stats;
use axum::Router;
@ -12,7 +13,8 @@ pub fn router(ctx: Context) -> Router {
.merge(node::router(ctx.clone()))
.merge(sessions::router(ctx.clone()))
.merge(delegates::router(ctx.clone()))
.merge(projects::router(ctx));
.merge(projects::router(ctx.clone()))
.merge(stats::router(ctx));
Router::new().nest("/v1", routes)
}

View File

@ -0,0 +1,24 @@
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{Extension, Json, Router};
use serde_json::json;
use crate::api::error::Error;
use crate::api::Context;
pub fn router(ctx: Context) -> Router {
Router::new()
.route("/stats", get(stats_handler))
.layer(Extension(ctx))
}
/// Return the stats for the node.
/// `GET /stats`
async fn stats_handler(Extension(ctx): Extension<Context>) -> impl IntoResponse {
let storage = &ctx.profile.storage;
let projects = storage.projects()?.len();
Ok::<_, Error>(Json(
json!({ "projects": { "count": projects }, "users": { "count": 0 } }),
))
}