init admin_route

This commit is contained in:
weisd
2024-11-05 17:41:35 +08:00
parent 0ae379fd7a
commit c410784cc9
12 changed files with 234 additions and 94 deletions
+1
View File
@@ -0,0 +1 @@
pub trait Handler: Send + Sync + 'static {}
+3
View File
@@ -0,0 +1,3 @@
pub mod handler;
pub mod operation;
pub mod router;
+10
View File
@@ -0,0 +1,10 @@
use hyper::{Method, StatusCode};
use matchit::Params;
use s3s::{Body, S3Request, S3Response, S3Result};
#[async_trait::async_trait]
pub trait Operation: Send + Sync + 'static {
fn method(&self) -> Method;
fn uri(&self) -> &'static str;
async fn call(&self, req: S3Request<Body>, params: Params<'_, '_>) -> S3Result<S3Response<(StatusCode, Body)>>;
}
+72
View File
@@ -0,0 +1,72 @@
use std::collections::HashMap;
use std::str::FromStr;
use hyper::http::Extensions;
use hyper::HeaderMap;
use hyper::Method;
use hyper::StatusCode;
use hyper::Uri;
use s3s::route::S3Route;
use s3s::s3_error;
use s3s::Body;
use s3s::S3Request;
use s3s::S3Response;
use s3s::S3Result;
use tracing::warn;
use crate::operation::Operation;
use common::error::Result;
use matchit::Router;
const ADMIN_PREFIX: &str = "/rustfs/admin";
// const ADMIN_VERSION: &str = "v3";
pub struct S3Router<T> {
router: Router<T>,
}
impl<T: Operation> S3Router<T> {
pub fn new() -> Self {
let router = Router::new();
Self { router }
}
pub fn insert(&mut self, operation: T) -> Result<()> {
let path = Self::make_route_str(operation.method(), &operation.uri());
warn!("set uri {}", &path);
self.router.insert(path, operation)?;
Ok(())
}
fn make_route_str(method: Method, path: &str) -> String {
format!("{}|{}{}", method.as_str(), ADMIN_PREFIX, path)
}
}
#[async_trait::async_trait]
impl<T> S3Route for S3Router<T>
where
T: Operation,
{
fn is_match(&self, _method: &Method, uri: &Uri, _headers: &HeaderMap, _: &mut Extensions) -> bool {
uri.path().starts_with("/rustfs/admin")
}
async fn call(&self, req: S3Request<Body>) -> S3Result<S3Response<(StatusCode, Body)>> {
let uri = format!("{}|{}", &req.method, req.uri.path());
// warn!("get uri {}", &uri);
if let Ok(mat) = self.router.at(&uri) {
let op: &T = mat.value;
return op.call(req, mat.params).await;
}
return Err(s3_error!(NotImplemented));
}
}