mod graphs;
mod info;
mod observability;
mod overview;
mod schema;
mod sparql;
mod sql;
mod validate;
pub use schema::SchemaCache;
use axum::extract::{Extension, Request};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::Router;
use crate::error::ApiError;
use crate::v2::auth::Identity;
use crate::AppState;
pub fn router() -> Router<AppState> {
Router::new()
.route("/info", get(info::handler))
.route("/overview", get(overview::handler))
.route("/sql/execute", post(sql::execute))
.route("/sql/validate", post(sql::validate))
.route("/sparql/execute", post(sparql::execute))
.route("/sparql/validate", post(sparql::validate))
.route("/schema/sql", get(schema::sql))
.route("/schema/sparql", get(schema::sparql))
.route("/graphs", get(graphs::list_graphs))
.route("/graphs/:id", get(graphs::get_graph_detail))
.route("/graphs/:id/triples", get(graphs::get_graph_triples))
.nest("/observability", observability::router())
}
pub(crate) async fn require_admin(
Extension(identity): Extension<Identity>,
req: Request,
next: Next,
) -> Response {
match identity.0 {
Some(user) if user.is_admin => next.run(req).await,
Some(_) => {
ApiError::Forbidden("administrator access is required".to_owned()).into_response()
}
None => ApiError::Unauthorized("authentication is required".to_owned()).into_response(),
}
}