|
|
@ -1,6 +1,6 @@
|
|
|
|
use crate::utils::error::MultihookResult;
|
|
|
|
use crate::utils::error::MultihookResult;
|
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
|
|
|
use hyper::{Body, Request, Response, Server};
|
|
|
|
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::Infallible;
|
|
|
|
use std::convert::Infallible;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::future::Future;
|
|
|
@ -9,6 +9,7 @@ use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct HTTPCallback<T1, T2> {
|
|
|
|
pub struct HTTPCallback<T1, T2> {
|
|
|
|
|
|
|
|
methods: Vec<Method>,
|
|
|
|
inner: Arc<
|
|
|
|
inner: Arc<
|
|
|
|
dyn Fn(
|
|
|
|
dyn Fn(
|
|
|
|
Request<T1>,
|
|
|
|
Request<T1>,
|
|
|
@ -32,10 +33,27 @@ impl HTTPCallback<Body, Body> {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
inner: Arc::new(cb),
|
|
|
|
inner: Arc::new(cb),
|
|
|
|
|
|
|
|
methods: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn allow_method(mut self, method: Method) -> Self {
|
|
|
|
|
|
|
|
self.methods.push(method);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn validate_method(&self, method: &Method) -> bool {
|
|
|
|
|
|
|
|
self.methods.contains(method)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn execute(&self, req: Request<Body>) -> MultihookResult<Response<Body>> {
|
|
|
|
pub async fn execute(&self, req: Request<Body>) -> MultihookResult<Response<Body>> {
|
|
|
|
|
|
|
|
if !self.validate_method(req.method()) {
|
|
|
|
|
|
|
|
return Ok(Response::builder()
|
|
|
|
|
|
|
|
.status(StatusCode::METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
.body(Body::from("Method not allowed"))
|
|
|
|
|
|
|
|
.unwrap());
|
|
|
|
|
|
|
|
}
|
|
|
|
self.inner.as_ref()(req).await
|
|
|
|
self.inner.as_ref()(req).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,7 +69,7 @@ impl HTTPServer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn execute_callback(&self, req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
|
|
|
async fn execute_callback(&self, req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
|
|
|
let path = req.uri().path();
|
|
|
|
let path = &req.uri().path()[1..];
|
|
|
|
let response = if let Some(cb) = self.routes.get(path) {
|
|
|
|
let response = if let Some(cb) = self.routes.get(path) {
|
|
|
|
match cb.as_ref().execute(req).await {
|
|
|
|
match cb.as_ref().execute(req).await {
|
|
|
|
Ok(res) => res,
|
|
|
|
Ok(res) => res,
|
|
|
@ -62,8 +80,8 @@ impl HTTPServer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Response::builder()
|
|
|
|
Response::builder()
|
|
|
|
.status(404)
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.body(Body::from("Unknown endpoint"))
|
|
|
|
.body(Body::from("404 - Not Found"))
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|