Service URLs are now routed from config

main
Jef Roosens 2021-11-25 15:21:39 +01:00
parent 5e4416f049
commit 6b33157817
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
5 changed files with 65 additions and 32 deletions

View File

@ -1,6 +1,6 @@
default: default:
address: "0.0.0.0" address: "0.0.0.0"
ports: 8000 port: 8000
debug: debug:
keep_alive: 5 keep_alive: 5
@ -20,6 +20,9 @@ debug:
# Just 5 seconds for debugging # Just 5 seconds for debugging
refresh_token_expire: 60 refresh_token_expire: 60
services:
blog: "http://localhost:8002"
databases: databases:
postgres_rb: postgres_rb:
url: "postgres://rb:rb@localhost:5434/rb" url: "postgres://rb:rb@localhost:5434/rb"

View File

@ -1,3 +1,4 @@
required_version = "1.4.38"
binop_separator = "Front" binop_separator = "Front"
blank_lines_lower_bound = 0 blank_lines_lower_bound = 0
blank_lines_upper_bound = 1 blank_lines_upper_bound = 1
@ -49,7 +50,6 @@ reorder_imports = true
reorder_modules = true reorder_modules = true
report_fixme = "Always" report_fixme = "Always"
report_todo = "Always" report_todo = "Always"
required_version = "1.4.37"
skip_children = false skip_children = false
space_after_colon = true space_after_colon = true
space_before_colon = false space_before_colon = false

View File

@ -7,6 +7,7 @@ use figment::{
providers::{Env, Format, Yaml}, providers::{Env, Format, Yaml},
Figment, Figment,
}; };
use proxy::ProxyServer;
use rb::{auth::JwtConf, errors::RbError}; use rb::{auth::JwtConf, errors::RbError};
use rb_gw::db; use rb_gw::db;
use rocket::{ use rocket::{
@ -17,10 +18,9 @@ use 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;
mod proxy; mod proxy;
pub mod v1;
#[database("postgres_rb")] #[database("postgres_rb")]
pub struct RbDbConn(diesel::PgConnection); pub struct RbDbConn(diesel::PgConnection);
@ -76,11 +76,18 @@ pub struct AdminConfig
password: String, password: String,
} }
#[derive(Debug, Deserialize, Serialize)]
pub struct ServicesConfig
{
blog: String,
}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct RbConfig pub struct RbConfig
{ {
admin: AdminConfig, admin: AdminConfig,
jwt: JwtConf, jwt: JwtConf,
services: ServicesConfig,
} }
#[launch] #[launch]
@ -106,7 +113,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();
@ -116,7 +123,20 @@ fn rocket() -> _
// admin user // admin user
let admin_conf: AdminConfig = new_figment.extract_inner("admin").expect("admin config"); 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 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(jwt_conf)
.manage(admin_conf) .manage(admin_conf)
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user)) .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))

View File

@ -1,39 +1,50 @@
use rocket::Route; use rocket::{
use rocket::http::Method; http::Method,
use rocket::{Request, Data}; response::Redirect,
use rocket::route::{Handler, Outcome}; route::{Handler, Outcome},
use rocket::response::Redirect; Data, Request, Route,
};
#[derive(Clone)] #[derive(Clone)]
pub struct ProxyServer { pub struct ProxyServer
{
root: String, root: String,
rank: isize, rank: isize,
} }
impl ProxyServer { impl ProxyServer
const DEFAULT_RANK: isize = 10; {
const DEFAULT_RANK: isize = 0;
pub fn new(root: String, rank: isize) -> Self { pub fn new(root: String, rank: isize) -> Self
ProxyServer { {
root, ProxyServer { root, rank }
rank,
}
} }
pub fn from(root: &str) -> Self { pub fn from(root: String) -> Self
Self::new(String::from(root), Self::DEFAULT_RANK) {
Self::new(root, Self::DEFAULT_RANK)
} }
} }
impl Into<Vec<Route>> for ProxyServer { impl Into<Vec<Route>> for ProxyServer
fn into(self) -> Vec<Route> { {
fn into(self) -> Vec<Route>
{
let mut routes: Vec<Route> = vec![]; let mut routes: Vec<Route> = 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 { for method in METHODS {
let mut route = Route::ranked(self.rank, method, "/<path..>", self.clone()); let mut route = Route::ranked(self.rank, method, "/<path..>", 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); routes.push(route);
} }
@ -43,8 +54,10 @@ impl Into<Vec<Route>> for ProxyServer {
} }
#[rocket::async_trait] #[rocket::async_trait]
impl Handler for ProxyServer { impl Handler for ProxyServer
async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> { {
todo!() async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r>
{
todo!()
} }
} }

View File

@ -19,7 +19,4 @@ table! {
joinable!(refresh_tokens -> users (user_id)); joinable!(refresh_tokens -> users (user_id));
allow_tables_to_appear_in_same_query!( allow_tables_to_appear_in_same_query!(refresh_tokens, users,);
refresh_tokens,
users,
);