This commit is contained in:
2025-04-20 09:57:58 +02:00
parent 0518ddb93d
commit 852347df2b
8 changed files with 80 additions and 197 deletions

View File

@@ -25,7 +25,7 @@ async fn main() -> Result<(), Error> {
// Create and start server
info!("Server listening on {}:{}", config.host, config.port);
info!("Swagger UI available at http://{}:{}/swagger", config.host, config.port);
info!("API documentation available at http://{}:{}/redoc", config.host, config.port);
let server = Server::new(config);

View File

@@ -5,6 +5,9 @@ use std::collections::HashMap;
use utoipa::ToSchema;
/// RPC request structure
///
/// This structure represents an RPC request to the ACLDB API.
/// It contains the method name, parameters, and a signature for authentication.
#[derive(Debug, Clone, Deserialize, ToSchema)]
pub struct RpcRequest {
/// Method name
@@ -16,6 +19,9 @@ pub struct RpcRequest {
}
/// RPC response structure
///
/// This structure represents the response from an RPC request.
/// It contains either a result or an error message.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct RpcResponse {
/// Result of the operation
@@ -25,6 +31,9 @@ pub struct RpcResponse {
}
/// ACL update request parameters
///
/// Parameters for updating or creating an ACL with specified permissions.
/// Used with the `acl_update` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct AclUpdateParams {
/// Public key of the requesting user
@@ -40,6 +49,9 @@ pub struct AclUpdateParams {
}
/// ACL remove request parameters
///
/// Parameters for removing specific public keys from an existing ACL.
/// Used with the `acl_remove` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct AclRemoveParams {
/// Public key of the requesting user
@@ -53,6 +65,9 @@ pub struct AclRemoveParams {
}
/// ACL delete request parameters
///
/// Parameters for deleting an entire ACL.
/// Used with the `acl_del` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct AclDelParams {
/// Public key of the requesting user
@@ -64,6 +79,9 @@ pub struct AclDelParams {
}
/// Set request parameters
///
/// Parameters for storing data with optional ACL protection.
/// Used with the `set` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct SetParams {
/// Public key of the requesting user
@@ -83,6 +101,9 @@ pub struct SetParams {
}
/// Delete request parameters
///
/// Parameters for deleting data with ACL verification.
/// Used with the `del` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct DelParams {
/// Public key of the requesting user
@@ -98,6 +119,9 @@ pub struct DelParams {
}
/// Get request parameters
///
/// Parameters for retrieving data with ACL verification.
/// Used with the `get` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct GetParams {
/// Public key of the requesting user
@@ -113,6 +137,9 @@ pub struct GetParams {
}
/// Prefix request parameters
///
/// Parameters for searching for keys with a specific prefix.
/// Used with the `prefix` method.
#[derive(Debug, Deserialize, ToSchema)]
pub struct PrefixParams {
/// Public key of the requesting user

View File

@@ -10,7 +10,6 @@ use serde_json::json;
use log::{error, info};
use utoipa::OpenApi;
use utoipa_redoc::{Redoc, Servable};
use utoipa_swagger_ui::SwaggerUi;
use crate::ACLDB;
use crate::rpc::{RpcInterface, RpcRequest, RpcResponse, AclUpdateParams, AclRemoveParams, AclDelParams, SetParams, DelParams, GetParams, PrefixParams};
@@ -166,7 +165,6 @@ impl Server {
info!("Starting ACLDB server on {}:{}", self.config.host, self.config.port);
info!("API documentation available at: http://{}:{}/redoc", self.config.host, self.config.port);
info!("Swagger UI available at: http://{}:{}/swagger", self.config.host, self.config.port);
// Start the HTTP server
HttpServer::new(move || {
@@ -184,10 +182,6 @@ impl Server {
.service(
Redoc::with_url("/redoc", serde_json::to_value(ApiDoc::openapi()).unwrap())
)
.service(
SwaggerUi::new("/swagger/{_:.*}")
.url("/api-docs/openapi.json", ApiDoc::openapi())
)
})
.bind(format!("{0}:{1}", self.config.host, self.config.port))?
.run()
@@ -266,16 +260,22 @@ fn extract_circle_id(request: &web::Json<RpcRequest>) -> Result<String, Error> {
/// API documentation schema
#[derive(OpenApi)]
#[openapi(
paths(
handle_rpc,
health_check
),
components(
schemas(RpcRequest, RpcResponse)
schemas(RpcRequest, RpcResponse, AclUpdateParams, AclRemoveParams, AclDelParams, SetParams, DelParams, GetParams, PrefixParams)
),
tags(
(name = "acldb", description = "ACLDB API - Access Control Database")
(name = "acldb", description = "ACLDB API - Access Control Database"),
(name = "acl", description = "Access Control List Management"),
(name = "data", description = "Data Operations")
),
info(
title = "ACLDB API",
version = "1.0.0",
description = "API for managing access control lists and data operations in HeroDB",
description = "API for managing access control lists and data operations in HeroDB. This API provides functionality for ACL management and data operations with access control.",
contact(
name = "HeroDB Team",
url = "https://ourworld.tf"
@@ -285,6 +285,29 @@ fn extract_circle_id(request: &web::Json<RpcRequest>) -> Result<String, Error> {
struct ApiDoc;
/// Handler for RPC requests
///
/// This endpoint handles all RPC requests to the ACLDB API.
/// Supported methods include:
/// - `acl_update`: Update or create an ACL with specified permissions
/// - `acl_remove`: Remove specific public keys from an existing ACL
/// - `acl_del`: Delete an entire ACL
/// - `set`: Store data with optional ACL protection
/// - `get`: Retrieve data with ACL verification
/// - `del`: Delete data with ACL verification
/// - `prefix`: Search for keys with a specific prefix
#[utoipa::path(
post,
path = "/rpc",
request_body = RpcRequest,
responses(
(status = 200, description = "RPC request processed successfully", body = RpcResponse),
(status = 400, description = "Invalid request parameters", body = RpcResponse),
(status = 401, description = "Permission denied", body = RpcResponse),
(status = 404, description = "Resource not found", body = RpcResponse),
(status = 500, description = "Server error", body = RpcResponse)
),
tag = "acldb"
)]
async fn handle_rpc(
server: web::Data<Server>,
request: web::Json<RpcRequest>,
@@ -475,6 +498,16 @@ async fn process_request(
}
/// Handler for health check
///
/// This endpoint provides a simple health check to verify the server is running.
#[utoipa::path(
get,
path = "/health",
responses(
(status = 200, description = "Server is healthy", body = String)
),
tag = "acldb"
)]
async fn health_check() -> impl Responder {
HttpResponse::Ok().json(json!({"status": "ok"}))
}