httpd: Add root handlers for `/api/v1` and `/api`
Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
parent
6e90444406
commit
9f1896e07f
|
|
@ -4,7 +4,6 @@ use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use axum::extract::State;
|
|
||||||
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
|
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
|
||||||
use axum::http::Method;
|
use axum::http::Method;
|
||||||
use axum::response::{IntoResponse, Json};
|
use axum::response::{IntoResponse, Json};
|
||||||
|
|
@ -76,12 +75,8 @@ impl Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn router(ctx: Context) -> Router {
|
pub fn router(ctx: Context) -> Router {
|
||||||
let root_router = Router::new()
|
|
||||||
.route("/", get(root_handler))
|
|
||||||
.with_state(ctx.clone());
|
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(root_router)
|
.route("/", get(root_handler))
|
||||||
.merge(v1::router(ctx))
|
.merge(v1::router(ctx))
|
||||||
.layer(
|
.layer(
|
||||||
CorsLayer::new()
|
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!({
|
let response = json!({
|
||||||
"message": "Welcome!",
|
"path": "/api",
|
||||||
"service": "radicle-httpd",
|
|
||||||
"version": format!("{}-{}", VERSION, env!("GIT_HEAD")),
|
|
||||||
"node": { "id": ctx.profile.public_key },
|
|
||||||
"path": "/",
|
|
||||||
"links": [
|
"links": [
|
||||||
{
|
{
|
||||||
"href": "/v1/projects",
|
"href": "/v1",
|
||||||
"rel": "projects",
|
"rel": "v1",
|
||||||
"type": "GET"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"href": "/v1/node",
|
|
||||||
"rel": "node",
|
|
||||||
"type": "GET"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"href": "/v1/delegates/:did/projects",
|
|
||||||
"rel": "projects",
|
|
||||||
"type": "GET"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"href": "/v1/stats",
|
|
||||||
"rel": "stats",
|
|
||||||
"type": "GET"
|
"type": "GET"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,21 @@ mod projects;
|
||||||
mod sessions;
|
mod sessions;
|
||||||
mod stats;
|
mod stats;
|
||||||
|
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::response::{IntoResponse, Json};
|
||||||
|
use axum::routing::get;
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::api::Context;
|
use crate::api::{Context, VERSION};
|
||||||
|
|
||||||
pub fn router(ctx: Context) -> Router {
|
pub fn router(ctx: Context) -> Router {
|
||||||
|
let root_router = Router::new()
|
||||||
|
.route("/", get(root_handler))
|
||||||
|
.with_state(ctx.clone());
|
||||||
|
|
||||||
let routes = Router::new()
|
let routes = Router::new()
|
||||||
|
.merge(root_router)
|
||||||
.merge(node::router(ctx.clone()))
|
.merge(node::router(ctx.clone()))
|
||||||
.merge(sessions::router(ctx.clone()))
|
.merge(sessions::router(ctx.clone()))
|
||||||
.merge(delegates::router(ctx.clone()))
|
.merge(delegates::router(ctx.clone()))
|
||||||
|
|
@ -18,3 +27,37 @@ pub fn router(ctx: Context) -> Router {
|
||||||
|
|
||||||
Router::new().nest("/v1", routes)
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue