用 enumerate 改进了一下 meausre 的返回值
This commit is contained in:
parent
e9ccc8604a
commit
35d94e14f6
46
src/serve.rs
46
src/serve.rs
@ -1,10 +1,28 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Path,
|
extract::{Path, Query},
|
||||||
http::header::HeaderMap,
|
http::header::HeaderMap,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub enum MeasureRes {
|
||||||
|
Forbidden,
|
||||||
|
BadResquest,
|
||||||
|
Data(Vec<u8>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for MeasureRes {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
match self {
|
||||||
|
Self::Forbidden => (StatusCode::FORBIDDEN).into_response(),
|
||||||
|
Self::BadResquest => (StatusCode::BAD_REQUEST).into_response(),
|
||||||
|
Self::Data(data) => (StatusCode::OK, data).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// ```ts
|
/// ```ts
|
||||||
/// import express from 'express'
|
/// import express from 'express'
|
||||||
///
|
///
|
||||||
@ -27,40 +45,28 @@ use axum::{
|
|||||||
///
|
///
|
||||||
/// export default MeasureRoute
|
/// export default MeasureRoute
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> Response<Vec<u8>> {
|
pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> MeasureRes {
|
||||||
let mut data: Vec<u8> = Vec::new();
|
let mut data: Vec<u8> = Vec::new();
|
||||||
match header.get("x-openbmclapi-secret") {
|
match header.get("x-openbmclapi-secret") {
|
||||||
Some(secret) => {
|
Some(secret) => {
|
||||||
if secret != "secret" {
|
if secret != "secret" {
|
||||||
return Response::builder()
|
return MeasureRes::Forbidden;
|
||||||
.status(StatusCode::FORBIDDEN)
|
|
||||||
.body(data)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
if size > 200 {
|
if size > 200 {
|
||||||
return Response::builder()
|
return MeasureRes::BadResquest;
|
||||||
.status(StatusCode::BAD_REQUEST)
|
|
||||||
.body(data)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
let buffer: Vec<u8> = vec![0x00, 0x66, 0xcc, 0xff];
|
let buffer: Vec<u8> = vec![0x00, 0x66, 0xcc, 0xff];
|
||||||
for _ in 0..size {
|
for _ in 0..size {
|
||||||
data.extend(&buffer);
|
data.extend(&buffer);
|
||||||
}
|
}
|
||||||
// return (StatusCode::OK, response);
|
return MeasureRes::Data(data);
|
||||||
return Response::builder()
|
|
||||||
.status(StatusCode::OK)
|
|
||||||
.body(data)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
None => Response::builder()
|
None => MeasureRes::Forbidden
|
||||||
.status(StatusCode::FORBIDDEN)
|
|
||||||
.body(data)
|
|
||||||
.unwrap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 返回文件的请求函数
|
/// 返回文件的请求函数
|
||||||
pub async fn get_file() -> impl IntoResponse {
|
/// app.get('/download/:hash(\\w+)', async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
pub async fn res_donwload(header: HeaderMap, Query(param): Query<HashMap<String, String>>, Path(hash): Path<String>) -> impl IntoResponse {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user