Skip to content

Commit 57f04ac

Browse files
committed
remove guard on user, make user and cluster options, set default cluster on main
1 parent 94c73cc commit 57f04ac

File tree

6 files changed

+49
-64
lines changed

6 files changed

+49
-64
lines changed

pgml-dashboard/src/api/docs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rocket::{http::Status, route::Route, State};
55
use yaml_rust::YamlLoader;
66

77
use crate::{
8-
guards::{Cluster, CurrentUserState},
8+
guards::Cluster,
99
responses::{ResponseOk, Template},
1010
templates::docs::*,
1111
utils::{config, markdown},
@@ -28,7 +28,7 @@ async fn search(query: &str, index: &State<markdown::SearchIndex>) -> ResponseOk
2828
async fn doc_handler<'a>(
2929
path: PathBuf,
3030
cluster: Cluster,
31-
current_user: CurrentUserState,
31+
current_user: &State<crate::CurrentUser>,
3232
) -> Result<ResponseOk, Status> {
3333
let guides = vec![
3434
NavLink::new("Setup").children(vec![
@@ -90,7 +90,7 @@ async fn doc_handler<'a>(
9090
async fn blog_handler<'a>(
9191
path: PathBuf,
9292
cluster: Cluster,
93-
current_user: CurrentUserState,
93+
current_user: &State<crate::CurrentUser>,
9494
) -> Result<ResponseOk, Status> {
9595
render(
9696
cluster,
@@ -141,7 +141,7 @@ async fn blog_handler<'a>(
141141

142142
async fn render<'a>(
143143
cluster: Cluster,
144-
current_user: CurrentUserState,
144+
current_user: &State<crate::CurrentUser>,
145145
path: &'a PathBuf,
146146
mut nav_links: Vec<NavLink>,
147147
nav_title: &'a str,
@@ -225,7 +225,7 @@ async fn render<'a>(
225225
}
226226

227227
let layout = layout
228-
.user(&current_user.user)
228+
.user(&current_user.get_user())
229229
.cluster(&cluster.context.cluster)
230230
.nav_title(nav_title)
231231
.nav_links(&nav_links)

pgml-dashboard/src/guards.rs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use rocket::request::{FromRequest, Outcome, Request};
55
use rocket::State;
66
use sqlx::PgPool;
77

8-
use std::collections::HashMap;
9-
10-
use crate::models::User;
11-
use crate::{Clusters, Context, CurrentUser};
8+
use crate::{Clusters, Context, models};
129

1310
pub fn default_database_url() -> String {
1411
match var("DATABASE_URL") {
@@ -43,10 +40,10 @@ impl<'r> FromRequest<'r> for Cluster {
4340
let cluster_id = match cookies.get_private("cluster_id") {
4441
Some(cluster_id) => match cluster_id.value().parse::<i64>() {
4542
Ok(cluster_id) => cluster_id,
46-
Err(_) => -1,
43+
Err(_) => models::Cluster::default().id,
4744
},
4845

49-
None => -1,
46+
None => models::Cluster::default().id,
5047
};
5148

5249
let clusters = match request.guard::<&State<Clusters>>().await {
@@ -63,28 +60,3 @@ impl<'r> FromRequest<'r> for Cluster {
6360
Outcome::Success(Cluster { pool, context })
6461
}
6562
}
66-
67-
#[derive(Debug)]
68-
pub struct CurrentUserState {
69-
pub user: User,
70-
pub visible_clusters: HashMap<String, String>,
71-
}
72-
73-
#[rocket::async_trait]
74-
impl<'r> FromRequest<'r> for CurrentUserState {
75-
type Error = ();
76-
77-
async fn from_request(request: &'r Request<'_>) -> Outcome<CurrentUserState, ()> {
78-
let user_data = match request.guard::<&State<CurrentUser>>().await {
79-
Outcome::Success(user) => user,
80-
_ => return Outcome::Forward(()),
81-
};
82-
83-
let current_user_state = CurrentUserState {
84-
user: user_data.get_user(),
85-
visible_clusters: user_data.get_visible_clusters(),
86-
};
87-
88-
Outcome::Success(current_user_state)
89-
}
90-
}

pgml-dashboard/src/lib.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::Arc;
66

77
use parking_lot::Mutex;
88
use rocket::form::Form;
9+
use rocket::State;
910
use rocket::response::Redirect;
1011
use rocket::route::Route;
1112
use sailfish::TemplateOnce;
@@ -24,7 +25,7 @@ use crate::templates::{
2425
DeploymentsTab, Layout, ModelsTab, NotebooksTab, ProjectsTab, SnapshotsTab, UploaderTab,
2526
};
2627
use crate::utils::tabs;
27-
use guards::{Cluster, CurrentUserState};
28+
use guards::Cluster;
2829
use responses::{BadRequest, Error, ResponseOk};
2930
use sqlx::Executor;
3031

@@ -41,7 +42,7 @@ pub struct ClustersSettings {
4142
/// This gives it a bit of shared state that allows the dashboard to display cluster-specific information.
4243
#[derive(Debug, Default, Clone)]
4344
pub struct Context {
44-
pub cluster: models::Cluster,
45+
pub cluster: Option<models::Cluster>,
4546
}
4647

4748
/// Globally shared state, saved in memory.
@@ -117,36 +118,41 @@ impl Clusters {
117118
}
118119
}
119120

120-
#[derive(Debug, Default, Clone)]
121+
122+
// Globally shared state in memory.
123+
124+
#[derive(Debug, Clone)]
121125
pub struct CurrentUser {
122-
pub user: Arc<Mutex<models::User>>,
123-
pub visible_clusters: Arc<Mutex<HashMap<String, String>>>,
126+
pub user: Arc<Mutex<Option<models::User>>>,
127+
pub visible_clusters: Arc<Mutex<Option<HashMap<String, String>>>>,
124128
}
125129

126130
impl CurrentUser {
127-
pub fn user(&self, user: models::User) {
128-
*self.user.lock() = user;
131+
pub fn set_user(&self, user: models::User) {
132+
*self.user.lock() = Some(user);
129133
}
130134

131-
pub fn get_user(&self) -> models::User {
135+
pub fn get_user(&self) -> Option<models::User> {
132136
self.user.lock().clone()
133137
}
134138

135-
pub fn visible_clusters(&self, visible_clusters: HashMap<String, String>) {
136-
*self.visible_clusters.lock() = visible_clusters;
139+
pub fn set_visible_clusters(&self, visible_clusters: HashMap<String, String>) {
140+
*self.visible_clusters.lock() = Some(visible_clusters);
137141
}
138142

139-
pub fn get_visible_clusters(&self) -> HashMap<String, String> {
143+
pub fn get_visible_clusters(&self) -> Option<HashMap<String, String>> {
140144
self.visible_clusters.lock().clone()
141145
}
142146

147+
pub fn set_to_default(&self) {
148+
*self.user.lock() = None;
149+
*self.visible_clusters.lock() = None;
150+
}
151+
143152
pub fn new() -> CurrentUser {
144153
CurrentUser {
145-
user: Arc::new(Mutex::new( models::User {
146-
id: -1,
147-
email: "".to_string()
148-
})),
149-
visible_clusters: Arc::new(Mutex::new(HashMap::new())),
154+
user: Arc::new(Mutex::new( None )),
155+
visible_clusters: Arc::new(Mutex::new(None )),
150156
}
151157
}
152158
}
@@ -573,7 +579,7 @@ pub async fn uploaded_index(cluster: Cluster, table_name: &str) -> ResponseOk {
573579
#[get("/?<tab>&<notebook_id>&<model_id>&<project_id>&<snapshot_id>&<deployment_id>&<table_name>")]
574580
pub async fn dashboard(
575581
cluster: Cluster,
576-
current_user: CurrentUserState,
582+
current_user: &State<CurrentUser>,
577583
tab: Option<&str>,
578584
notebook_id: Option<i64>,
579585
model_id: Option<i64>,
@@ -582,9 +588,12 @@ pub async fn dashboard(
582588
deployment_id: Option<i64>,
583589
table_name: Option<String>,
584590
) -> Result<ResponseOk, Error> {
591+
// 500 error if there is no cluster in global state.
592+
let _ = cluster.context.cluster.as_ref().unwrap();
593+
585594
let mut layout = crate::templates::Layout::new("Dashboard");
586595
layout
587-
.user(&current_user.user)
596+
.user(&current_user.get_user())
588597
.cluster(&cluster.context.cluster);
589598

590599
let all_tabs = vec![

pgml-dashboard/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ async fn main() {
132132
)
133133
.unwrap();
134134

135+
clusters.set_context(-1, pgml_dashboard::Context {
136+
cluster: Some(pgml_dashboard::models::Cluster::default())
137+
});
138+
135139
pgml_dashboard::migrate(&clusters.get(-1).unwrap())
136140
.await
137141
.unwrap();

pgml-dashboard/src/templates/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl Layout {
4444
}
4545
}
4646

47-
pub fn cluster(&mut self, cluster: &models::Cluster) -> &mut Self {
48-
self.cluster = Some(cluster.to_owned());
47+
pub fn cluster(&mut self, cluster: &Option<models::Cluster>) -> &mut Self {
48+
self.cluster = cluster.to_owned();
4949
self
5050
}
5151

@@ -64,8 +64,8 @@ impl Layout {
6464
self
6565
}
6666

67-
pub fn user(&mut self, user: &models::User) -> &mut Self {
68-
self.user = Some(user.to_owned());
67+
pub fn user(&mut self, user: &Option<models::User>) -> &mut Self {
68+
self.user = user.to_owned();
6969
self
7070
}
7171

pgml-dashboard/templates/layout/nav/top.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</button>
1313
<div class="collapse navbar-collapse" id="navbarSupportedContent">
1414
<ul class="navbar-nav flex-grow-1 justify-content-center me-auto mb-2 mb-lg-0">
15-
<% if standalone_dashboard || (current_cluster.is_some() && current_cluster.unwrap().id != -1) { %>
15+
<% if current_cluster.is_some() { %>
1616
<li class="nav-item d-flex align-items-center">
1717
<a class="nav-link" href="/dashboard">Dashboard</a>
1818
</li>
@@ -36,14 +36,14 @@
3636
</button>
3737
<% if !standalone_dashboard { %>
3838
<div class="d-flex gap-2 justify-content-end align-items-center">
39-
<% if current_user.as_ref().is_none() || current_user.as_ref().unwrap().id == -1 { %>
40-
<a class="btn btn-secondary" data-controller="btn-secondary" data-btn-secondary-target="btnSecondary" href="/login">Sign In</a>
41-
<a class="btn btn-primary" href="/signup">Get Started</a>
42-
<% } else { %>
39+
<% if current_user.is_some() { %>
4340
<a class="btn btn-secondary" data-controller="btn-secondary" data-btn-secondary-target="btnSecondary" href="/clusters">Console</a>
4441
<a href="/support" class="btn btn-tertiary p-0 ps-2">
45-
<img loading="lazy" src="/dashboard/static/images/icons/help.svg" width="30" height="30" alt="help" />
42+
<img loading="lazy" src="/dashboard/static/images/icons/help.svg" width="30" height="30" alt="help" />
4643
</a>
44+
<% } else { %>
45+
<a class="btn btn-secondary" data-controller="btn-secondary" data-btn-secondary-target="btnSecondary" href="/login">Sign In</a>
46+
<a class="btn btn-primary" href="/signup">Get Started</a>
4747
<% } %>
4848
</div>
4949

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy