httpd: Add root handlers for `/api/v1` and `/api`

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-03-10 12:54:14 +00:00 committed by Alexis Sellier
parent 6e90444406
commit 9f1896e07f
2 changed files with 49 additions and 30 deletions

View File

@ -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"
}
]

View File

@ -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)
}