Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: Add root handlers for `/api/v1` and `/api`
xphoniex committed 3 years ago
commit 44bf989d2e54ac86f6e9988a8a0162ed6a0bc374
parent 41e151266f0aada2212168a38cdeb99b297dcde4
2 files changed +49 -30
modified 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<Context>) -> 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"
            }
        ]
modified 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<Context>) -> 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)
+
}