mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-05 12:57:42 +00:00
refactor: move api crate to api dir
Signed-off-by: bestgopher <84328409@qq.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{header::CONTENT_TYPE, HeaderValue, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use mime::APPLICATION_JSON;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct ErrorResponse {
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub key: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bucket_name: Option<String>,
|
||||
pub resource: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub region: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub request_id: Option<String>,
|
||||
pub host_id: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub actual_object_size: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub range_requested: Option<String>,
|
||||
}
|
||||
|
||||
impl IntoResponse for APIError {
|
||||
fn into_response(self) -> Response {
|
||||
let code = self.http_status_code;
|
||||
let err_response = ErrorResponse::from(self);
|
||||
let json_res = match serde_json::to_vec(&err_response) {
|
||||
Ok(r) => r,
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
|
||||
};
|
||||
|
||||
Response::builder()
|
||||
.status(code)
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static(APPLICATION_JSON.as_ref()))
|
||||
.body(Body::from(json_res))
|
||||
.unwrap_or_else(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct APIError {
|
||||
code: String,
|
||||
description: String,
|
||||
http_status_code: StatusCode,
|
||||
object_size: Option<String>,
|
||||
range_requested: Option<String>,
|
||||
}
|
||||
|
||||
pub enum ErrorCode {
|
||||
ErrNotImplemented,
|
||||
ErrServerNotInitialized,
|
||||
}
|
||||
|
||||
impl IntoResponse for ErrorCode {
|
||||
fn into_response(self) -> Response {
|
||||
APIError::from(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorCode> for APIError {
|
||||
fn from(value: ErrorCode) -> Self {
|
||||
use ErrorCode::*;
|
||||
|
||||
match value {
|
||||
ErrNotImplemented => APIError {
|
||||
code: "NotImplemented".into(),
|
||||
description: "A header you provided implies functionality that is not implemented.".into(),
|
||||
http_status_code: StatusCode::NOT_IMPLEMENTED,
|
||||
..Default::default()
|
||||
},
|
||||
ErrServerNotInitialized => APIError {
|
||||
code: "ServerNotInitialized".into(),
|
||||
description: "Server not initialized yet, please try again.".into(),
|
||||
http_status_code: StatusCode::SERVICE_UNAVAILABLE,
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<APIError> for ErrorResponse {
|
||||
fn from(value: APIError) -> Self {
|
||||
Self {
|
||||
code: value.code,
|
||||
message: value.description,
|
||||
actual_object_size: value.object_size,
|
||||
range_requested: value.range_requested,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod list_pools;
|
||||
@@ -0,0 +1,79 @@
|
||||
use crate::Result as LocalResult;
|
||||
use crate::{error::ErrorCode, object_api::ObjectApi};
|
||||
|
||||
use axum::{extract::State, Json};
|
||||
use serde::Serialize;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct PoolStatus {
|
||||
id: i64,
|
||||
cmdline: String,
|
||||
#[serde(rename = "lastUpdate")]
|
||||
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
||||
last_updat: OffsetDateTime,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "decommissionInfo")]
|
||||
decommission_info: Option<PoolDecommissionInfo>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct PoolDecommissionInfo {
|
||||
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
||||
start_time: OffsetDateTime,
|
||||
start_size: i64,
|
||||
total_size: i64,
|
||||
current_size: i64,
|
||||
complete: bool,
|
||||
failed: bool,
|
||||
canceled: bool,
|
||||
|
||||
#[serde(rename = "objectsDecommissioned")]
|
||||
items_decommissioned: i64,
|
||||
#[serde(rename = "objectsDecommissionedFailed")]
|
||||
items_decommission_failed: i64,
|
||||
#[serde(rename = "bytesDecommissioned")]
|
||||
bytes_done: i64,
|
||||
#[serde(rename = "bytesDecommissionedFailed")]
|
||||
bytes_failed: i64,
|
||||
}
|
||||
|
||||
pub async fn handler(State(ec_store): State<ObjectApi>) -> LocalResult<Json<Vec<PoolStatus>>> {
|
||||
// if ecstore::is_legacy().await {
|
||||
// return Err(ErrorCode::ErrNotImplemented);
|
||||
// }
|
||||
|
||||
let pools = (*ec_store).as_ref().ok_or(ErrorCode::ErrNotImplemented)?;
|
||||
|
||||
// todo, 调用pool.status()接口获取每个池的数据
|
||||
//
|
||||
let mut result = Vec::new();
|
||||
for (idx, _pool) in pools.pools.iter().enumerate() {
|
||||
// 这里mock一下数据
|
||||
result.push(PoolStatus {
|
||||
id: idx as _,
|
||||
cmdline: "cmdline".into(),
|
||||
last_updat: OffsetDateTime::now_utc(),
|
||||
decommission_info: if idx % 2 == 0 {
|
||||
Some(PoolDecommissionInfo {
|
||||
start_time: OffsetDateTime::now_utc(),
|
||||
start_size: 1,
|
||||
total_size: 2,
|
||||
current_size: 2,
|
||||
complete: true,
|
||||
failed: true,
|
||||
canceled: true,
|
||||
items_decommissioned: 1,
|
||||
items_decommission_failed: 1,
|
||||
bytes_done: 1,
|
||||
bytes_failed: 1,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
pub mod error;
|
||||
pub mod handlers;
|
||||
pub mod object_api;
|
||||
|
||||
use axum::{extract::Request, response::Response, routing::get, BoxError, Router};
|
||||
use ecstore::store::ECStore;
|
||||
use error::ErrorCode;
|
||||
use handlers::list_pools;
|
||||
use object_api::ObjectApi;
|
||||
use tower::Service;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, ErrorCode>;
|
||||
|
||||
pub fn register_admin_router(
|
||||
ec_store: Option<ECStore>,
|
||||
) -> impl Service<Request, Response = Response, Error: Into<BoxError>, Future: Send> + Clone {
|
||||
Router::new()
|
||||
.nest("/admin/v3", Router::new().route("/pools/list", get(list_pools::handler)))
|
||||
.with_state::<()>(ObjectApi::new(ec_store))
|
||||
.into_service()
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use ecstore::store::ECStore;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ObjectApi(Option<ECStore>);
|
||||
|
||||
impl Deref for ObjectApi {
|
||||
type Target = Option<ECStore>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectApi {
|
||||
pub fn new(t: Option<ECStore>) -> Self {
|
||||
Self(t)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user