Skip to content

Commit eeb435f

Browse files
committed
refactor to static config
1 parent 9ca66d9 commit eeb435f

File tree

6 files changed

+220
-117
lines changed

6 files changed

+220
-117
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use std::path::{Path, PathBuf};
22

33
use comrak::{format_html_with_plugins, parse_document, Arena, ComrakPlugins};
4-
use rocket::{http::Status, route::Route, State};
4+
use rocket::{
5+
fs::NamedFile,
6+
http::{uri::Origin, Status},
7+
route::Route,
8+
State,
9+
};
510
use yaml_rust::YamlLoader;
611

712
use crate::{
@@ -24,35 +29,63 @@ async fn search(query: &str, index: &State<markdown::SearchIndex>) -> ResponseOk
2429
)
2530
}
2631

32+
struct Content {
33+
name: String,
34+
}
2735

28-
use rocket::fs::NamedFile;
29-
use rocket::http::uri::Origin;
36+
impl Content {
37+
pub fn gitbook_asset_path(&self, path: &str) -> PathBuf {
38+
PathBuf::from(&config::cms_dir())
39+
.join(self.name.to_lowercase())
40+
.join(".gitbook")
41+
.join("assets")
42+
.join(path)
43+
}
44+
}
45+
46+
pub async fn get_asset(path: &PathBuf) -> Option<NamedFile> {
47+
NamedFile::open(path).await.ok()
48+
}
3049

3150
#[get("/careers/.gitbook/assets/<path>", rank = 10)]
32-
pub async fn careers_assets(path: PathBuf) -> Option<NamedFile> {
33-
let path = PathBuf::from(&config::docs_dir())
34-
.join("careers").join(".gitbook").join("assets")
51+
pub async fn get_asset_careers(path: &str) -> Option<NamedFile> {
52+
let content = Content {
53+
name: "Careers".to_owned(),
54+
};
55+
get_asset(&content.gitbook_asset_path(path)).await
56+
}
57+
58+
#[get("/docs/.gitbook/assets/<path>", rank = 10)]
59+
pub async fn get_asset_docs(path: &str) -> Option<NamedFile> {
60+
let path = PathBuf::from(&config::cms_dir())
61+
.join("docs")
62+
.join(".gitbook")
63+
.join("assets")
3564
.join(path);
3665

3766
NamedFile::open(path).await.ok()
3867
}
3968

4069
#[get("/careers/<path..>", rank = 5)]
41-
async fn careers_contenthandler(mut path: PathBuf, cluster: &Cluster, origin: &Origin<'_>) -> Result<ResponseOk, Status> {
70+
async fn careers_handler(
71+
mut path: PathBuf,
72+
cluster: &Cluster,
73+
origin: &Origin<'_>,
74+
) -> Result<ResponseOk, Status> {
4275
// Rocket 0.5 began stripping trailing '/' from the path
4376
if origin.path().ends_with("/") {
4477
path = path.join("/");
4578
}
4679
let root = PathBuf::from("careers/");
47-
let index_path = PathBuf::from(&config::docs_dir())
80+
let index_path = PathBuf::from(&config::cms_dir())
4881
.join(&root)
4982
.join("SUMMARY.md");
5083
let contents = tokio::fs::read_to_string(&index_path).await.expect(
5184
format!(
5285
"could not read table of contents markdown: {:?}",
5386
index_path
5487
)
55-
.as_str(),
88+
.as_str(),
5689
);
5790
let mdast = ::markdown::to_mdast(&contents, &::markdown::ParseOptions::default())
5891
.expect("could not parse table of contents markdown");
@@ -65,28 +98,24 @@ async fn careers_contenthandler(mut path: PathBuf, cluster: &Cluster, origin: &O
6598
careers,
6699
"Careers",
67100
&Path::new("careers"),
68-
&config::docs_dir(),
101+
config::cms_dir(),
69102
)
70-
.await
71-
}
72-
#[get("/docs/.gitbook/assets/<path>", rank = 10)]
73-
pub async fn docs_gitbook_assets(path: PathBuf) -> Option<NamedFile> {
74-
let path = PathBuf::from(&config::docs_dir())
75-
.join("docs/.gitbook/assets/")
76-
.join(path);
77-
78-
NamedFile::open(path).await.ok()
103+
.await
79104
}
80105

81106
#[get("/docs/<path..>", rank = 5)]
82-
async fn doc_handler(mut path: PathBuf, cluster: &Cluster, origin: &Origin<'_>) -> Result<ResponseOk, Status> {
107+
async fn docs_handler(
108+
mut path: PathBuf,
109+
cluster: &Cluster,
110+
origin: &Origin<'_>,
111+
) -> Result<ResponseOk, Status> {
83112
info!("path: {:?}", path);
84113
if origin.path().ends_with("/") {
85114
path = path.join("");
86115
}
87116
info!("joined path: {:?}", path);
88117
let root = PathBuf::from("docs/");
89-
let index_path = PathBuf::from(&config::docs_dir())
118+
let index_path = PathBuf::from(&config::cms_dir())
90119
.join(&root)
91120
.join("SUMMARY.md");
92121
let contents = tokio::fs::read_to_string(&index_path).await.expect(
@@ -107,7 +136,7 @@ async fn doc_handler(mut path: PathBuf, cluster: &Cluster, origin: &Origin<'_>)
107136
guides,
108137
"Docs",
109138
&Path::new("docs"),
110-
&config::docs_dir(),
139+
config::cms_dir(),
111140
)
112141
.await
113142
}
@@ -172,7 +201,7 @@ async fn blog_handler<'a>(path: PathBuf, cluster: &Cluster) -> Result<ResponseOk
172201
],
173202
"Blog",
174203
&Path::new("blog"),
175-
&config::blogs_dir(),
204+
config::blogs_dir(),
176205
)
177206
.await
178207
}
@@ -183,7 +212,7 @@ async fn render<'a>(
183212
mut nav_links: Vec<NavLink>,
184213
nav_title: &'a str,
185214
folder: &'a Path,
186-
content: &'a str,
215+
content: &'a Path,
187216
) -> Result<ResponseOk, Status> {
188217
let mut path = path
189218
.to_str()
@@ -301,7 +330,14 @@ async fn render<'a>(
301330
}
302331

303332
pub fn routes() -> Vec<Route> {
304-
routes![docs_gitbook_assets, doc_handler, blog_handler, careers_handler, careers_gitbook_assets, search]
333+
routes![
334+
get_asset_careers,
335+
get_asset_docs,
336+
docs_handler,
337+
blog_handler,
338+
careers_handler,
339+
search
340+
]
305341
}
306342

307343
#[cfg(test)]

pgml-dashboard/src/components/github_icon/template.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
<svg width="25" height="25" viewBox="0 0 40 39" fill="none" xmlns="http://www.w3.org/2000/svg">
66
<path d="M20 0.25C17.3736 0.25 14.7728 0.763591 12.3463 1.76145C9.91982 2.75931 7.71504 4.22189 5.85786 6.06569C2.10714 9.78942 0 14.8399 0 20.106C0 28.8824 5.74 36.3284 13.68 38.9692C14.68 39.1281 15 38.5126 15 37.9764V34.6208C9.46 35.8121 8.28 31.9601 8.28 31.9601C7.36 29.6568 6.06 29.0412 6.06 29.0412C4.24 27.8102 6.2 27.8499 6.2 27.8499C8.2 27.9889 9.26 29.895 9.26 29.895C11 32.9132 13.94 32.0196 15.08 31.5431C15.26 30.2525 15.78 29.3788 16.34 28.8824C11.9 28.386 7.24 26.6784 7.24 19.1132C7.24 16.9092 8 15.142 9.3 13.7322C9.1 13.2358 8.4 11.1708 9.5 8.49025C9.5 8.49025 11.18 7.95414 15 10.5156C16.58 10.0787 18.3 9.86032 20 9.86032C21.7 9.86032 23.42 10.0787 25 10.5156C28.82 7.95414 30.5 8.49025 30.5 8.49025C31.6 11.1708 30.9 13.2358 30.7 13.7322C32 15.142 32.76 16.9092 32.76 19.1132C32.76 26.6982 28.08 28.3661 23.62 28.8625C24.34 29.4781 25 30.6893 25 32.5359V37.9764C25 38.5126 25.32 39.1479 26.34 38.9692C34.28 36.3085 40 28.8824 40 20.106C40 17.4985 39.4827 14.9165 38.4776 12.5075C37.4725 10.0984 35.9993 7.9095 34.1421 6.06569C32.285 4.22189 30.0802 2.75931 27.6537 1.76145C25.2272 0.763591 22.6264 0.25 20 0.25Z" fill="#FAFAFA"/>
77
</svg>
8-
<% if let Ok(stars) = crate::utils::config::github_stars() { %>
9-
<span class=""><%= stars %></span>
10-
<% } %>
8+
<span class=""><%= crate::utils::config::github_stars() %></span>
119
</a>
1210
<% } else { %>
1311
<a class="d-flex align-items-center nav-link p-0 border-bottom-0" href="https://github.com/postgresml/postgresml">

pgml-dashboard/src/guards.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use sqlx::{postgres::PgPoolOptions, Executor, PgPool};
88

99
static POOL: OnceCell<PgPool> = OnceCell::new();
1010

11-
use crate::utils::config;
12-
use crate::models;
13-
use crate::Context;
11+
use crate::{models, utils::config, Context};
1412

1513
#[derive(Debug)]
1614
pub struct Cluster {
@@ -39,7 +37,7 @@ impl Cluster {
3937
Ok(())
4038
})
4139
})
42-
.connect_lazy(&config::database_url())
40+
.connect_lazy(config::database_url())
4341
.expect("Default database URL is malformed")
4442
})
4543
.clone(),

pgml-dashboard/src/templates/docs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ impl NavLink {
4444
/// Automatically expand the link and it's parents
4545
/// when one of the children is visible.
4646
pub fn should_open(&mut self, path: &str, root: &std::path::Path) -> bool {
47-
info!("should_open self: {:?}, path: {:?}, root: {:?}", self, path, root);
47+
info!(
48+
"should_open self: {:?}, path: {:?}, root: {:?}",
49+
self, path, root
50+
);
4851
if path.is_empty() {
4952
if self.href.as_str() == root.as_os_str() {
5053
return true;
51-
5254
} else {
5355
return false;
5456
}

0 commit comments

Comments
 (0)