Skip to main content

wowlab_sentinel/http/routes/
nodes.rs

1//! Signed node lifecycle HTTP routes.
2
3use std::sync::Arc;
4
5use axum::{Extension, Json, Router, extract::State, routing::post};
6use wowlab_common::node_http::{
7    NodeRegistrationRequest, NodeRegistrationResponse, NodeTokenResponse, NodeUnlinkResponse,
8};
9
10use crate::{
11    http::{api_error::ApiError, auth::VerifiedNode, services::nodes::node_operations},
12    state::ServerState,
13};
14
15pub(super) fn router() -> Router<Arc<ServerState>> {
16    Router::new()
17        .route("/nodes/register", post(register))
18        .route("/nodes/token", post(refresh_token))
19        .route("/nodes/unlink", post(unlink))
20}
21
22async fn register(
23    State(state): State<Arc<ServerState>>,
24    Extension(node): Extension<VerifiedNode>,
25    Json(request): Json<NodeRegistrationRequest>,
26) -> Result<Json<NodeRegistrationResponse>, ApiError> {
27    let response = node_operations(&state)
28        .register(&node.public_key, &request)
29        .await?;
30
31    Ok(Json(response))
32}
33
34async fn refresh_token(
35    State(state): State<Arc<ServerState>>,
36    Extension(node): Extension<VerifiedNode>,
37) -> Result<Json<NodeTokenResponse>, ApiError> {
38    let response = node_operations(&state)
39        .refresh_token(&node.public_key)
40        .await?;
41
42    Ok(Json(response))
43}
44
45async fn unlink(
46    State(state): State<Arc<ServerState>>,
47    Extension(node): Extension<VerifiedNode>,
48) -> Result<Json<NodeUnlinkResponse>, ApiError> {
49    let response = node_operations(&state).unlink(&node.public_key).await?;
50
51    Ok(Json(response))
52}