From 6b33157817f73dd6c367c140a1dbfefb97cdda3c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 25 Nov 2021 15:21:39 +0100 Subject: [PATCH] Service URLs are now routed from config --- Rb.yaml | 5 ++++- rustfmt.toml | 2 +- src/main.rs | 26 ++++++++++++++++++++--- src/proxy.rs | 59 +++++++++++++++++++++++++++++++-------------------- src/schema.rs | 5 +---- 5 files changed, 65 insertions(+), 32 deletions(-) diff --git a/Rb.yaml b/Rb.yaml index caf2dbf..7a2242d 100644 --- a/Rb.yaml +++ b/Rb.yaml @@ -1,6 +1,6 @@ default: address: "0.0.0.0" - ports: 8000 + port: 8000 debug: keep_alive: 5 @@ -20,6 +20,9 @@ debug: # Just 5 seconds for debugging refresh_token_expire: 60 + services: + blog: "http://localhost:8002" + databases: postgres_rb: url: "postgres://rb:rb@localhost:5434/rb" diff --git a/rustfmt.toml b/rustfmt.toml index 8e8627b..1d443c7 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,4 @@ +required_version = "1.4.38" binop_separator = "Front" blank_lines_lower_bound = 0 blank_lines_upper_bound = 1 @@ -49,7 +50,6 @@ reorder_imports = true reorder_modules = true report_fixme = "Always" report_todo = "Always" -required_version = "1.4.37" skip_children = false space_after_colon = true space_before_colon = false diff --git a/src/main.rs b/src/main.rs index 58742f7..64dc3e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use figment::{ providers::{Env, Format, Yaml}, Figment, }; +use proxy::ProxyServer; use rb::{auth::JwtConf, errors::RbError}; use rb_gw::db; use rocket::{ @@ -17,10 +18,9 @@ use rocket::{ }; use rocket_sync_db_pools::database; use serde::{Deserialize, Serialize}; -use proxy::ProxyServer; -pub mod v1; mod proxy; +pub mod v1; #[database("postgres_rb")] pub struct RbDbConn(diesel::PgConnection); @@ -76,11 +76,18 @@ pub struct AdminConfig password: String, } +#[derive(Debug, Deserialize, Serialize)] +pub struct ServicesConfig +{ + blog: String, +} + #[derive(Debug, Deserialize, Serialize)] pub struct RbConfig { admin: AdminConfig, jwt: JwtConf, + services: ServicesConfig, } #[launch] @@ -106,7 +113,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(); @@ -116,7 +123,20 @@ fn rocket() -> _ // admin user let admin_conf: AdminConfig = new_figment.extract_inner("admin").expect("admin config"); + // We need to mount the various services as ProxyServer handlers + let services_conf: ServicesConfig = new_figment + .extract_inner("services") + .expect("services config"); + rocket + .mount( + "/v1/posts", + ProxyServer::from(format!("{}/v1/posts", services_conf.blog)), + ) + .mount( + "/v1/sections", + ProxyServer::from(format!("{}/v1/sections", services_conf.blog)), + ) .manage(jwt_conf) .manage(admin_conf) .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user)) diff --git a/src/proxy.rs b/src/proxy.rs index 851ed84..88481ca 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -1,39 +1,50 @@ -use rocket::Route; -use rocket::http::Method; -use rocket::{Request, Data}; -use rocket::route::{Handler, Outcome}; -use rocket::response::Redirect; +use rocket::{ + http::Method, + response::Redirect, + route::{Handler, Outcome}, + Data, Request, Route, +}; #[derive(Clone)] -pub struct ProxyServer { +pub struct ProxyServer +{ root: String, rank: isize, } -impl ProxyServer { - const DEFAULT_RANK: isize = 10; +impl ProxyServer +{ + const DEFAULT_RANK: isize = 0; - pub fn new(root: String, rank: isize) -> Self { - ProxyServer { - root, - rank, - } + 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) + pub fn from(root: String) -> Self + { + Self::new(root, Self::DEFAULT_RANK) } } -impl Into> for ProxyServer { - fn into(self) -> Vec { +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]; + + 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()); + route.name = + Some(format!("ProxyServer: {} {}", method.as_str(), self.root.clone()).into()); routes.push(route); } @@ -43,8 +54,10 @@ impl Into> for ProxyServer { } #[rocket::async_trait] -impl Handler for ProxyServer { - async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> { - todo!() +impl Handler for ProxyServer +{ + async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> + { + todo!() } } diff --git a/src/schema.rs b/src/schema.rs index 8dcb725..e3854e3 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -19,7 +19,4 @@ table! { joinable!(refresh_tokens -> users (user_id)); -allow_tables_to_appear_in_same_query!( - refresh_tokens, - users, -); +allow_tables_to_appear_in_same_query!(refresh_tokens, users,);