Wrote foundation for proxy server handler

main
Jef Roosens 2021-11-25 14:14:33 +01:00
parent 0449af66d2
commit 5e4416f049
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 59 additions and 5 deletions

View File

@ -52,6 +52,7 @@ incremental = true
lto = "fat" lto = "fat"
incremental = true incremental = true
codegen-units = 1 codegen-units = 1
panic = "abort"
# For releases also try to max optimizations for dependencies: # For releases also try to max optimizations for dependencies:
[profile.release.build-override] [profile.release.build-override]

View File

@ -32,8 +32,9 @@ release:
limits: limits:
forms: 32768 forms: 32768
admin_user: "admin" admin:
admin_pass: "password" username: "bever"
password: "bever"
jwt: jwt:
key: "secret" key: "secret"
refresh_token_size: 64 refresh_token_size: 64
@ -42,4 +43,4 @@ release:
databases: databases:
postgres_rb: postgres_rb:
url: "postgres://rb:rb@db:5432/rb" url: "postgres://rb:rb@localhost:5434/rb"

View File

@ -13,12 +13,14 @@ use rocket::{
fairing::AdHoc, fairing::AdHoc,
http::Status, http::Status,
serde::json::{json, Value}, serde::json::{json, Value},
Build, Ignite, Request, Rocket, Build, Request, Rocket,
}; };
use rocket_sync_db_pools::database; use rocket_sync_db_pools::database;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use proxy::ProxyServer;
pub mod v1; pub mod v1;
mod proxy;
#[database("postgres_rb")] #[database("postgres_rb")]
pub struct RbDbConn(diesel::PgConnection); pub struct RbDbConn(diesel::PgConnection);
@ -104,7 +106,7 @@ fn rocket() -> _
v1::auth::login, v1::auth::login,
v1::auth::refresh_token, 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 // This let's the guards properly access the JWT credentials when needed
let new_figment = rocket.figment(); let new_figment = rocket.figment();

50
src/proxy.rs 100644
View 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!()
}
}