From 5e4416f049660fbd8f416a96d333faa6c4036306 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 25 Nov 2021 14:14:33 +0100 Subject: [PATCH] Wrote foundation for proxy server handler --- Cargo.toml | 1 + Rb.yaml | 7 ++++--- src/main.rs | 6 ++++-- src/proxy.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/proxy.rs diff --git a/Cargo.toml b/Cargo.toml index 62613f7..285f175 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ incremental = true lto = "fat" incremental = true codegen-units = 1 +panic = "abort" # For releases also try to max optimizations for dependencies: [profile.release.build-override] diff --git a/Rb.yaml b/Rb.yaml index 3416fb2..caf2dbf 100644 --- a/Rb.yaml +++ b/Rb.yaml @@ -32,8 +32,9 @@ release: limits: forms: 32768 - admin_user: "admin" - admin_pass: "password" + admin: + username: "bever" + password: "bever" jwt: key: "secret" refresh_token_size: 64 @@ -42,4 +43,4 @@ release: databases: postgres_rb: - url: "postgres://rb:rb@db:5432/rb" + url: "postgres://rb:rb@localhost:5434/rb" diff --git a/src/main.rs b/src/main.rs index cbad5c1..58742f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); diff --git a/src/proxy.rs b/src/proxy.rs new file mode 100644 index 0000000..851ed84 --- /dev/null +++ b/src/proxy.rs @@ -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> for ProxyServer { + fn into(self) -> Vec { + let mut routes: Vec = 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, "/", 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!() + } +}