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:
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"

View File

@ -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

View File

@ -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))

View File

@ -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<Vec<Route>> for ProxyServer {
fn into(self) -> Vec<Route> {
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];
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());
route.name =
Some(format!("ProxyServer: {} {}", method.as_str(), self.root.clone()).into());
routes.push(route);
}
@ -43,8 +54,10 @@ impl Into<Vec<Route>> 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!()
}
}

View File

@ -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,);