Failed attempt at writing proof-of-concept
This commit is contained in:
parent
6b33157817
commit
7804db5ff0
5 changed files with 378 additions and 9 deletions
|
|
@ -129,13 +129,10 @@ fn rocket() -> _
|
|||
.expect("services config");
|
||||
|
||||
rocket
|
||||
.mount(
|
||||
"/v1/posts",
|
||||
ProxyServer::from(format!("{}/v1/posts", services_conf.blog)),
|
||||
)
|
||||
.mount("/v1/posts", ProxyServer::from(services_conf.blog.clone()))
|
||||
.mount(
|
||||
"/v1/sections",
|
||||
ProxyServer::from(format!("{}/v1/sections", services_conf.blog)),
|
||||
ProxyServer::from(services_conf.blog.clone()),
|
||||
)
|
||||
.manage(jwt_conf)
|
||||
.manage(admin_conf)
|
||||
|
|
|
|||
70
src/proxy.rs
70
src/proxy.rs
|
|
@ -1,5 +1,10 @@
|
|||
use reqwest::{
|
||||
header::{HeaderMap, HeaderName, HeaderValue},
|
||||
Method as ReqMethod,
|
||||
};
|
||||
use rocket::{
|
||||
http::Method,
|
||||
data::ToByteUnit,
|
||||
http::{ Method, Header },
|
||||
response::Redirect,
|
||||
route::{Handler, Outcome},
|
||||
Data, Request, Route,
|
||||
|
|
@ -31,7 +36,7 @@ impl Into<Vec<Route>> for ProxyServer
|
|||
{
|
||||
fn into(self) -> Vec<Route>
|
||||
{
|
||||
let mut routes: Vec<Route> = vec![];
|
||||
let mut routes: Vec<Route> = Vec::new();
|
||||
|
||||
static METHODS: [Method; 5] = [
|
||||
Method::Get,
|
||||
|
|
@ -58,6 +63,65 @@ impl Handler for ProxyServer
|
|||
{
|
||||
async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r>
|
||||
{
|
||||
todo!()
|
||||
println!("{:?}", req);
|
||||
// The goals here is to convert a Rocket request into a reqwest one
|
||||
|
||||
// First, we convert the headers
|
||||
let mut headers = HeaderMap::new();
|
||||
|
||||
for header in req.headers().iter() {
|
||||
// TODO remove unwrap
|
||||
headers.insert(
|
||||
HeaderName::from_lowercase(header.name.into_string().as_bytes()).unwrap(),
|
||||
HeaderValue::from_str(&header.value.into_owned()).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
// Then the method
|
||||
let method = match req.method() {
|
||||
Method::Delete => ReqMethod::DELETE,
|
||||
Method::Get => ReqMethod::GET,
|
||||
Method::Patch => ReqMethod::PATCH,
|
||||
Method::Post => ReqMethod::POST,
|
||||
Method::Put => ReqMethod::PUT,
|
||||
_ => todo!(),
|
||||
};
|
||||
|
||||
// Then the URL
|
||||
let url = format!("{}{}?{}", self.root, req.uri().path().as_str(), req.uri().query().unwrap());
|
||||
|
||||
// And finally, the data
|
||||
// TODO don't hard-code max request size here
|
||||
static BODY_SIZE: i64 = 2;
|
||||
// TODO remove unwrap
|
||||
let data: Vec<u8> = data
|
||||
.open(BODY_SIZE.mebibytes())
|
||||
.into_bytes()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_vec();
|
||||
|
||||
// TODO remove unwrap
|
||||
let new_res = reqwest::Client::new()
|
||||
.request(method, url)
|
||||
.headers(headers)
|
||||
.body(data)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
println!("{:?}", new_res);
|
||||
|
||||
let mut rocket_res = rocket::Response::new();
|
||||
rocket_res.set_status(rocket::http::Status::new(new_res.status().as_u16()));
|
||||
// rocket_res.set_streamed_body(new_res.bytes_stream());
|
||||
|
||||
// reqwest headers -> rocket headers
|
||||
for (key, value) in new_res.headers().clone().iter_mut() {
|
||||
println!("{}", rocket_res.set_raw_header(String::from(key.clone().as_str()), String::from(value.clone().to_str().unwrap())));
|
||||
}
|
||||
|
||||
println!("{:?}", rocket_res);
|
||||
|
||||
Outcome::Success(rocket_res)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue