diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index 9592e5fb..883426be 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -4,7 +4,6 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use axum::extract::State; use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; use axum::http::Method; use axum::response::{IntoResponse, Json}; @@ -76,12 +75,8 @@ impl Context { } pub fn router(ctx: Context) -> Router { - let root_router = Router::new() - .route("/", get(root_handler)) - .with_state(ctx.clone()); - Router::new() - .merge(root_router) + .route("/", get(root_handler)) .merge(v1::router(ctx)) .layer( CorsLayer::new() @@ -98,32 +93,13 @@ pub fn router(ctx: Context) -> Router { ) } -async fn root_handler(State(ctx): State) -> impl IntoResponse { +async fn root_handler() -> impl IntoResponse { let response = json!({ - "message": "Welcome!", - "service": "radicle-httpd", - "version": format!("{}-{}", VERSION, env!("GIT_HEAD")), - "node": { "id": ctx.profile.public_key }, - "path": "/", + "path": "/api", "links": [ { - "href": "/v1/projects", - "rel": "projects", - "type": "GET" - }, - { - "href": "/v1/node", - "rel": "node", - "type": "GET" - }, - { - "href": "/v1/delegates/:did/projects", - "rel": "projects", - "type": "GET" - }, - { - "href": "/v1/stats", - "rel": "stats", + "href": "/v1", + "rel": "v1", "type": "GET" } ] diff --git a/radicle-httpd/src/api/v1.rs b/radicle-httpd/src/api/v1.rs index 97829cdf..112be1e5 100644 --- a/radicle-httpd/src/api/v1.rs +++ b/radicle-httpd/src/api/v1.rs @@ -4,12 +4,21 @@ mod projects; mod sessions; mod stats; +use axum::extract::State; +use axum::response::{IntoResponse, Json}; +use axum::routing::get; use axum::Router; +use serde_json::json; -use crate::api::Context; +use crate::api::{Context, VERSION}; pub fn router(ctx: Context) -> Router { + let root_router = Router::new() + .route("/", get(root_handler)) + .with_state(ctx.clone()); + let routes = Router::new() + .merge(root_router) .merge(node::router(ctx.clone())) .merge(sessions::router(ctx.clone())) .merge(delegates::router(ctx.clone())) @@ -18,3 +27,37 @@ pub fn router(ctx: Context) -> Router { Router::new().nest("/v1", routes) } + +async fn root_handler(State(ctx): State) -> impl IntoResponse { + let response = json!({ + "message": "Welcome!", + "service": "radicle-httpd", + "version": format!("{}-{}", VERSION, env!("GIT_HEAD")), + "node": { "id": ctx.profile.public_key }, + "path": "/api/v1", + "links": [ + { + "href": "/projects", + "rel": "projects", + "type": "GET" + }, + { + "href": "/node", + "rel": "node", + "type": "GET" + }, + { + "href": "/delegates/:did/projects", + "rel": "projects", + "type": "GET" + }, + { + "href": "/stats", + "rel": "stats", + "type": "GET" + } + ] + }); + + Json(response) +}