Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub async fn start_server(with_tls: bool) -> Result<()> {

// Singletons
openid::init_once().await;
if let Err(e) = services::init_once() {
eprintln!("{e}");
exit(1);
}
if let Err(e) = DB::init_once(&DBNAME).await {
eprintln!("{e}");
exit(1);
Expand Down
3 changes: 2 additions & 1 deletion src/web/route/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
COOKIE_NAME,
openid::{OpenID, OpenIDProvider, get_openid_provider},
},
config::CONFIG,
db::{
Database,
keydb::CacheDB,
Expand All @@ -31,7 +32,7 @@ use crate::{
},
};
#[cfg(feature = "observe")]
use crate::{config::CONFIG, logger::MESSAGE_DELIMITER};
use crate::logger::MESSAGE_DELIMITER;

pub use self::oauth::generate_token_with_cookie;
use self::{
Expand Down
5 changes: 3 additions & 2 deletions src/web/route/health/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::{error, instrument};
use crate::{
db::keydb::CacheDB,
errors::Result,
web::{bridge_middleware::Htmx, helper, services::CATALOG_URLS},
web::{bridge_middleware::Htmx, helper, services},
};

mod inference_services;
Expand All @@ -32,8 +32,9 @@ async fn status(
client: Data<Client>,
cache: Data<Option<&CacheDB>>,
) -> Result<HttpResponse> {
let catalog_urls = services::get_service_health_urls();
let is = inference_services::InferenceServicesHealth::new(
&CATALOG_URLS,
&catalog_urls,
client.as_ref().clone(),
**cache,
);
Expand Down
6 changes: 3 additions & 3 deletions src/web/route/mcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
errors::{BridgeError, Result},
web::{
helper::{self, forwarding::Config},
services::CATALOG,
services,
},
};

Expand All @@ -38,7 +38,7 @@ async fn forward(
if let Some(mcp) = path.split('/').next() {
let path = path.strip_prefix(mcp).unwrap_or(path);

if !CATALOG.is_service_mcp(mcp)? {
if !services::is_service_mcp(mcp)? {
return Err(BridgeError::ServiceDoesNotExist(mcp.to_string()));
}

Expand All @@ -62,7 +62,7 @@ async fn forward(
));
}

let mut new_url = helper::log_with_level!(CATALOG.get_service(mcp), error)?;
let mut new_url = helper::log_with_level!(services::get_service(mcp), error)?;
// fastmcp temp(?) fix
if mcp.ends_with(MCP_SUFFIX) {
new_url.set_path(&format!("{path}/"));
Expand Down
19 changes: 11 additions & 8 deletions src/web/route/portal/group_admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
bridge_middleware::{HTMX_ERROR_RES, Htmx},
helper::{self, bson},
route::portal::{helper::check_admin, user_htmx::Subscription},
services::CATALOG,
services,
},
};

Expand Down Expand Up @@ -91,15 +91,20 @@ pub(super) async fn group(
let (subs, group_created_at, group_updated_at, group_last_updated) = match subscriptions {
Ok(g) => (
{
let catalog_all = services::get_all();
let mut sub_details: Vec<Subscription> = Vec::new();

g.subscriptions.iter().for_each(|name| {
if let Some(sub) = CATALOG.get_all().get(name.as_str()) {
if let Some(sub) = catalog_all.get(name.as_str()) {
sub_details.push(Subscription {
name: name.to_owned(),
kind: sub.0,
kind_designation: if sub.1 { "mcp" } else { "inference" },
description: sub.2,
kind: sub.kind.clone(),
kind_designation: if sub.mcp {
"mcp".to_string()
} else {
"inference".to_string()
},
description: sub.description.clone(),
});
}
});
Expand Down Expand Up @@ -148,9 +153,7 @@ pub(super) async fn group(
let resources: Vec<(&String, bool)> = resources
.iter()
.map(|r| {
let show = CATALOG
.get_details("resources", r, "show")
.map(|v| v.as_bool().unwrap_or(false));
let show = services::get_detail("resources", r, "show").and_then(|v| v.as_bool());
(r, show.unwrap_or(false))
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion src/web/route/portal/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(super) async fn notebook_bookkeeping<'c>(
nsc: Option<ReqData<NotebookStatusCookie>>,
bc: &mut BridgeCookie,
ctx: &mut Context,
subscription: &Vec<Subscription<'c>>,
subscription: &[Subscription],
) -> Result<Option<[Cookie<'c>; 2]>> {
// Check is user is allowed to access the notebook
if subscription
Expand Down
6 changes: 3 additions & 3 deletions src/web/route/portal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
web::{
bridge_middleware::{CookieCheck, HTMX_ERROR_RES, Htmx},
helper::log_with_level,
services::CATALOG,
services,
},
};

Expand All @@ -51,7 +51,7 @@ async fn index(data: Option<ReqData<BridgeCookie>>, db: Data<&DB>) -> Result<Htt
let mut resp = HttpResponse::SeeOther();

if !groups.is_empty() {
let all_resources = CATALOG.get_all_resources_by_name();
let all_resources = services::get_all_resources_by_name();

let group_sub = groups
.pop()
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn index(data: Option<ReqData<BridgeCookie>>, db: Data<&DB>) -> Result<Htt
}
}
}
all_resources.contains(&r.as_str())
all_resources.iter().any(|resource_name| resource_name == r)
})
.collect::<Vec<_>>()
});
Expand Down
4 changes: 2 additions & 2 deletions src/web/route/portal/system_admin/htmx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tera::Tera;
use crate::errors::Result;

pub struct GroupContent {
pub items: Vec<&'static str>,
pub items: Vec<String>,
}

pub(super) static VIEW_GROUP: &str = "components/group_view.html";
Expand All @@ -16,7 +16,7 @@ impl GroupContent {
Self { items: Vec::new() }
}

pub fn add(&mut self, item: &'static str) {
pub fn add(&mut self, item: String) {
self.items.push(item);
}

Expand Down
41 changes: 28 additions & 13 deletions src/web/route/portal/system_admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
helper::{check_admin, get_all_groups},
user_htmx::Subscription,
},
services::CATALOG,
services,
},
};

Expand Down Expand Up @@ -96,15 +96,20 @@ pub(super) async fn system(
let (subs, group_created_at, group_updated_at, group_last_updated) = match subscriptions {
Ok(g) => (
{
let catalog_all = services::get_all();
let mut sub_details: Vec<Subscription> = Vec::new();

g.subscriptions.iter().for_each(|name| {
if let Some(sub) = CATALOG.get_all().get(name.as_str()) {
if let Some(sub) = catalog_all.get(name.as_str()) {
sub_details.push(Subscription {
name: name.to_owned(),
kind: sub.0,
kind_designation: if sub.1 { "mcp" } else { "inference" },
description: sub.2,
kind: sub.kind.clone(),
kind_designation: if sub.mcp {
"mcp".to_string()
} else {
"inference".to_string()
},
description: sub.description.clone(),
});
}
});
Expand Down Expand Up @@ -153,9 +158,7 @@ pub(super) async fn system(
let resources: Vec<(&String, bool)> = resources
.iter()
.map(|r| {
let show = CATALOG
.get_details("resources", r, "show")
.map(|v| v.as_bool().unwrap_or(false));
let show = services::get_detail("resources", r, "show").and_then(|v| v.as_bool());
(r, show.unwrap_or(false))
})
.collect();
Expand Down Expand Up @@ -394,11 +397,12 @@ async fn system_tab_htmx(
| AdminTab::GroupCreate
| AdminTab::GroupView => {
let mut group_form = GroupContent::new();
let catalog_all = services::get_all();

// TODO: Move this into some cache so you don't do this over and over. For now the only
// cost is creation of a Vec in the GroupContent struct where each item is &'static str
CATALOG.get_all().iter().for_each(|(&name, (_, _, _))| {
group_form.add(name);
catalog_all.iter().for_each(|(name, _)| {
group_form.add(name.clone());
});

match tab.tab {
Expand Down Expand Up @@ -460,8 +464,8 @@ async fn system_tab_htmx(
let mut selections = group_form
.items
.iter()
.map(|&v| (v, group_info.subscriptions.iter().any(|s| s.eq(v))))
.collect::<Vec<(&str, bool)>>();
.map(|v| (v.clone(), group_info.subscriptions.iter().any(|s| s.eq(v))))
.collect::<Vec<(String, bool)>>();
selections.sort_by_key(|(_, b)| !*b);

group_form.render(
Expand Down Expand Up @@ -518,6 +522,16 @@ async fn system_tab_htmx(
.body(content))
}

#[post("reload-services")]
async fn system_reload_services(subject: Option<ReqData<BridgeCookie>>) -> Result<HttpResponse> {
let _ = check_admin(subject, UserType::SystemAdmin)?;
services::reload()?;

Ok(HttpResponse::Ok()
.content_type(ContentType::form_url_encoded())
.body("<p>Service catalog reloaded</p>"))
}

pub fn config_system(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/system_admin").service(system).service(
Expand All @@ -527,7 +541,8 @@ pub fn config_system(cfg: &mut web::ServiceConfig) {
.service(system_create_group)
.service(system_update_group)
.service(system_update_user)
.service(system_delete_user),
.service(system_delete_user)
.service(system_reload_services),
),
// .service(system_delete_group)
);
Expand Down
14 changes: 7 additions & 7 deletions src/web/route/portal/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
web::{
helper,
route::portal::user_htmx::{Profile, Subscription},
services::CATALOG,
services,
},
};

Expand Down Expand Up @@ -85,17 +85,17 @@ pub(super) async fn user(
profile.add_group(group.to_string());
});
group.subscriptions.into_iter().for_each(|subscription| {
if let Some(subscription_detail) = CATALOG.get_all().get(subscription.as_str()) {
if let Some(subscription_detail) = services::get_all().get(subscription.as_str()) {
profile.add_subscription(Subscription {
name: subscription,
kind: subscription_detail.0,
kind_designation: if subscription_detail.1 {
kind: subscription_detail.kind.clone(),
kind_designation: if subscription_detail.mcp {
// TODO: remove this hardcode
"mcp"
"mcp".to_string()
} else {
"inference"
"inference".to_string()
},
description: subscription_detail.2,
description: subscription_detail.description.clone(),
});
}
});
Expand Down
19 changes: 9 additions & 10 deletions src/web/route/portal/user_htmx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ use tera::{Context, Tera};
use crate::{
db::models::{BridgeCookie, NotebookStatusCookie, OWUICookie, User},
errors::Result,
web::services::CATALOG,
web::services,
};

#[cfg(feature = "notebook")]
use super::helper::notebook_bookkeeping;

#[derive(Serialize, Clone)]
pub struct Subscription<'s> {
pub struct Subscription {
pub name: String,
pub kind: &'s str,
pub kind_designation: &'s str,
pub description: &'s str,
pub kind: String,
pub kind_designation: String,
pub description: String,
}

pub struct Profile<'p> {
pub groups: Vec<String>,
pub subscriptions: Vec<Subscription<'p>>,
pub subscriptions: Vec<Subscription>,
user: &'p User,
}

Expand All @@ -44,7 +44,7 @@ impl<'p> Profile<'p> {
self.groups.push(group);
}

pub fn add_subscription(&mut self, subscription: Subscription<'p>) {
pub fn add_subscription(&mut self, subscription: Subscription) {
self.subscriptions.push(subscription);
}

Expand Down Expand Up @@ -80,9 +80,8 @@ impl<'p> Profile<'p> {
let resources: Vec<(&String, bool)> = resources
.iter()
.map(|r| {
let show = CATALOG
.get_details("resources", r, "show")
.map(|v| v.as_bool().unwrap_or(false));
let show = services::get_detail("resources", r, "show")
.and_then(|v| v.as_bool().map(|b| b.to_owned()));
(r, show.unwrap_or(false))
})
.collect();
Expand Down
4 changes: 1 addition & 3 deletions src/web/route/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::{
},
};

use self::services::CATALOG;

pub mod services;

const BRIDGE_PREFIX: &str = "/proxy";
Expand Down Expand Up @@ -46,7 +44,7 @@ async fn forward(
})?;

// look up service and get url
let mut new_url = helper::log_with_level!(CATALOG.get_service(service), error)?;
let mut new_url = helper::log_with_level!(self::services::get_service(service), error)?;
new_url.set_path(path);
new_url.set_query(req.uri().query());

Expand Down
Loading