mirror of
https://git.alemi.dev/fedicharter.git
synced 2024-11-12 20:09:21 +01:00
feat: added some more routes to explore db contents
This commit is contained in:
parent
4e401ed173
commit
86dbf15f01
1 changed files with 41 additions and 3 deletions
|
@ -1,10 +1,11 @@
|
|||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use axum::{Router, routing::get, extract::{Query, State}, Json};
|
||||
use sea_orm::DatabaseConnection;
|
||||
use serde::Deserialize;
|
||||
use reqwest::StatusCode;
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, PaginatorTrait};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::serve::bubble::BubbleChart;
|
||||
use crate::{serve::bubble::BubbleChart, nodeinfo::model::NodeInfoOwned, entities::{domain, node_info}};
|
||||
|
||||
use super::bubble::{ChartVertex, ChartArc};
|
||||
|
||||
|
@ -12,6 +13,8 @@ pub async fn serve_api_routes(addr: SocketAddr, db: DatabaseConnection) {
|
|||
|
||||
let app = Router::new()
|
||||
.route("/crawl", get(route_crawl_domain))
|
||||
.route("/instance", get(route_get_instance))
|
||||
.route("/stats", get(route_get_stats))
|
||||
.with_state(Arc::new(db));
|
||||
|
||||
tracing::info!("listening on {}", addr);
|
||||
|
@ -41,3 +44,38 @@ async fn route_crawl_domain(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn route_get_instance(
|
||||
State(db): State<Arc<DatabaseConnection>>,
|
||||
Query(params): Query<Params>
|
||||
) -> Result<Json<NodeInfoOwned>, StatusCode> {
|
||||
Ok(Json(
|
||||
serde_json::from_str(
|
||||
&domain::Entity::find_by_id(¶ms.domain)
|
||||
.find_also_related(node_info::Entity)
|
||||
.one(db.as_ref())
|
||||
.await
|
||||
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?
|
||||
.1
|
||||
.ok_or(StatusCode::NOT_FOUND)?
|
||||
.data
|
||||
).expect("could not deserialize nodeinfo on db")
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct StatsReport {
|
||||
total_instances: u64,
|
||||
}
|
||||
|
||||
async fn route_get_stats(
|
||||
State(db): State<Arc<DatabaseConnection>>,
|
||||
) -> Json<StatsReport> {
|
||||
Json(StatsReport {
|
||||
total_instances: node_info::Entity::find()
|
||||
.count(db.as_ref())
|
||||
.await
|
||||
.expect("could not count number of instances")
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue