Wrote foundation for proxy server handler
This commit is contained in:
parent
0449af66d2
commit
5e4416f049
4 changed files with 59 additions and 5 deletions
|
|
@ -13,12 +13,14 @@ use rocket::{
|
|||
fairing::AdHoc,
|
||||
http::Status,
|
||||
serde::json::{json, Value},
|
||||
Build, Ignite, Request, Rocket,
|
||||
Build, Request, Rocket,
|
||||
};
|
||||
use rocket_sync_db_pools::database;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use proxy::ProxyServer;
|
||||
|
||||
pub mod v1;
|
||||
mod proxy;
|
||||
|
||||
#[database("postgres_rb")]
|
||||
pub struct RbDbConn(diesel::PgConnection);
|
||||
|
|
@ -104,7 +106,7 @@ fn rocket() -> _
|
|||
v1::auth::login,
|
||||
v1::auth::refresh_token,
|
||||
],
|
||||
);
|
||||
).mount("/v1/posts", ProxyServer::from("http://localhost:8000/v1/posts"));
|
||||
|
||||
// This let's the guards properly access the JWT credentials when needed
|
||||
let new_figment = rocket.figment();
|
||||
|
|
|
|||
50
src/proxy.rs
Normal file
50
src/proxy.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use rocket::Route;
|
||||
use rocket::http::Method;
|
||||
use rocket::{Request, Data};
|
||||
use rocket::route::{Handler, Outcome};
|
||||
use rocket::response::Redirect;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ProxyServer {
|
||||
root: String,
|
||||
rank: isize,
|
||||
}
|
||||
|
||||
impl ProxyServer {
|
||||
const DEFAULT_RANK: isize = 10;
|
||||
|
||||
pub fn new(root: String, rank: isize) -> Self {
|
||||
ProxyServer {
|
||||
root,
|
||||
rank,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from(root: &str) -> Self {
|
||||
Self::new(String::from(root), Self::DEFAULT_RANK)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Vec<Route>> for ProxyServer {
|
||||
fn into(self) -> Vec<Route> {
|
||||
let mut routes: Vec<Route> = vec![];
|
||||
|
||||
static METHODS: [Method; 5] = [Method::Get, Method::Post, Method::Patch, Method::Delete, Method::Put];
|
||||
|
||||
for method in METHODS {
|
||||
let mut route = Route::ranked(self.rank, method, "/<path..>", self.clone());
|
||||
route.name = Some(format!("ProxyServer: {} {}", method.as_str(), self.root.clone()).into());
|
||||
|
||||
routes.push(route);
|
||||
}
|
||||
|
||||
routes
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl Handler for ProxyServer {
|
||||
async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Reference in a new issue