Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Local state #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Local state
  • Loading branch information
levkk committed Jun 12, 2023
commit 6218cc01dd180d539b51c2348775185a86a9fca2
8 changes: 4 additions & 4 deletions 8 pgml-dashboard/src/api/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn search(query: &str, index: &State<markdown::SearchIndex>) -> ResponseOk
}

#[get("/docs/<path..>", rank = 10)]
async fn doc_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk, Status> {
async fn doc_handler<'a>(path: PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
let guides = vec![
NavLink::new("Setup").children(vec![
NavLink::new("Installation").children(vec![
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn doc_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk,
}

#[get("/blog/<path..>", rank = 10)]
async fn blog_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk, Status> {
async fn blog_handler<'a>(path: PathBuf, cluster: &Cluster) -> Result<ResponseOk, Status> {
render(
cluster,
&path,
Expand Down Expand Up @@ -123,7 +123,7 @@ async fn blog_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk,
}

async fn render<'a>(
cluster: Cluster,
cluster: &Cluster,
path: &'a PathBuf,
mut nav_links: Vec<NavLink>,
nav_title: &'a str,
Expand Down Expand Up @@ -201,7 +201,7 @@ async fn render<'a>(
let user = if cluster.context.user.is_anonymous() {
None
} else {
Some(cluster.context.user)
Some(cluster.context.user.clone())
};

let mut layout = crate::templates::Layout::new(&title);
Expand Down
100 changes: 46 additions & 54 deletions 100 pgml-dashboard/src/guards.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashMap;
use std::env::var;

use rocket::http::CookieJar;
use rocket::request::{FromRequest, Outcome, Request};
use rocket::State;
use sqlx::PgPool;
use rocket::request::{self, FromRequest, Request};
use sqlx::{postgres::PgPoolOptions, Executor, PgPool};

use crate::models::User;
use crate::{Clusters, Context};
use crate::models;
use crate::{ClustersSettings, Context};

pub fn default_database_url() -> String {
match var("DATABASE_URL") {
Expand All @@ -17,63 +16,56 @@ pub fn default_database_url() -> String {

#[derive(Debug)]
pub struct Cluster {
pool: Option<PgPool>,
pool: PgPool,
pub context: Context,
}

impl<'a> Cluster {
pub fn pool(&'a self) -> &'a PgPool {
self.pool.as_ref().unwrap()
}
}

#[rocket::async_trait]
impl<'r> FromRequest<'r> for Cluster {
type Error = ();
impl Default for Cluster {
fn default() -> Self {
let max_connections = 5;
let min_connections = 1;
let idle_timeout = 15_000;

async fn from_request(request: &'r Request<'_>) -> Outcome<Cluster, ()> {
// Using `State` as a request guard. Use `inner()` to get an `'r`.
let cookies = match request.guard::<&CookieJar<'r>>().await {
Outcome::Success(cookies) => cookies,
_ => return Outcome::Forward(()),
let settings = ClustersSettings {
max_connections,
idle_timeout,
min_connections,
};

let cluster_id = match cookies.get_private("cluster_id") {
Some(cluster_id) => match cluster_id.value().parse::<i64>() {
Ok(cluster_id) => cluster_id,
Err(_) => -1,
Cluster {
pool: PgPoolOptions::new()
.max_connections(settings.max_connections)
.idle_timeout(std::time::Duration::from_millis(settings.idle_timeout))
.min_connections(settings.min_connections)
.after_connect(|conn, _meta| {
Box::pin(async move {
conn.execute("SET application_name = 'pgml_dashboard';")
.await?;
Ok(())
})
})
.connect_lazy(&default_database_url())
.expect("Default database URL is malformed"),
context: Context {
user: models::User::default(),
cluster: models::Cluster::default(),
visible_clusters: HashMap::default(),
},
}
}
}

None => -1,
};

let user_id: i64 = match cookies.get_private("user_id") {
Some(user_id) => match user_id.value().parse::<i64>() {
Ok(user_id) => user_id,
Err(_) => -1,
},

None => -1,
};

let clusters_shared_state = match request.guard::<&State<Clusters>>().await {
Outcome::Success(pool) => pool,
_ => return Outcome::Forward(()),
};

let pool = clusters_shared_state.get(cluster_id);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for &'r Cluster {
type Error = ();

let context = Context {
user: User {
id: user_id,
email: "".to_string(),
},
cluster: clusters_shared_state.get_context(cluster_id).cluster,
visible_clusters: clusters_shared_state
.get_context(cluster_id)
.visible_clusters,
};
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
request::Outcome::Success(request.local_cache(|| Cluster::default()))
}
}

Outcome::Success(Cluster { pool, context })
impl<'a> Cluster {
pub fn pool(&'a self) -> &'a PgPool {
&self.pool
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.