Skip to content

Commit 266b3de

Browse files
committed
comment out everything and put leptos in
1 parent 847e8c5 commit 266b3de

File tree

8 files changed

+1440
-1289
lines changed

8 files changed

+1440
-1289
lines changed

pgml-dashboard/Cargo.lock

Lines changed: 1026 additions & 1178 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pgml-dashboard/Cargo.toml

Lines changed: 138 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,149 @@ repository = "https://github.com/postgremsl/postgresml"
1010
include = ["src/", "sqlx-data.json", "templates/", "migrations/", "static/"]
1111
default-run = "pgml-dashboard"
1212

13+
[lib]
14+
crate-type = ["cdylib", "rlib"]
15+
1316
[dependencies]
1417
anyhow = "1"
15-
comrak = "0.17"
16-
chrono = { version = "0.4", features = ["serde"] }
17-
csv-async = "1"
18-
console-subscriber = "*"
19-
convert_case = "0.6"
20-
dotenv = "0.15"
21-
env_logger = "0.10"
22-
glob = "*"
23-
itertools = "0.10"
24-
parking_lot = "0.12"
25-
lazy_static = "1.4"
26-
log = "0.4"
27-
markdown = "1.0.0-alpha.14"
28-
num-traits = "0.2"
29-
once_cell = "1.18"
30-
pgml = { path = "../pgml-sdks/pgml/" }
31-
pgml-components = { path = "../packages/pgml-components" }
32-
pgvector = { version = "0.3", features = [ "sqlx", "postgres" ] }
33-
rand = "0.8"
34-
regex = "1.9"
35-
reqwest = { version = "0.11", features = ["json"] }
36-
rocket = { git = "https://github.com/SergioBenitez/Rocket", features = ["secrets", "json"] }
37-
sailfish = "0.8.0" # 0.8.1 has breaking changes
38-
serde = "1"
39-
sentry = "0.31"
40-
sentry-log = "0.31"
41-
sentry-anyhow = "0.31"
42-
serde_json = "1"
43-
sqlparser = "0.38"
44-
sqlx = { version = "0.7.3", features = [ "runtime-tokio-rustls", "postgres", "json", "migrate", "time", "uuid", "bigdecimal"] }
45-
time = "0.3"
46-
tokio = { version = "1", features = ["full"] }
47-
url = "2.4"
48-
yaml-rust = "0.4"
49-
zoomies = { git="https://github.com/HyperparamAI/zoomies.git", branch="master" }
50-
ws = { package = "rocket_ws", git = "https://github.com/SergioBenitez/Rocket" }
51-
futures = "0.3.29"
18+
# comrak = "0.17"
19+
# chrono = { version = "0.4", features = ["serde"] }
20+
# csv-async = "1"
21+
# console-subscriber = "*"
22+
# convert_case = "0.6"
23+
# dotenv = "0.15"
24+
# env_logger = "0.10"
25+
# glob = "*"
26+
# itertools = "0.10"
27+
# parking_lot = "0.12"
28+
# lazy_static = "1.4"
29+
# log = "0.4"
30+
# markdown = "1.0.0-alpha.14"
31+
# num-traits = "0.2"
32+
# once_cell = "1.18"
33+
pgml = { path = "../pgml-sdks/pgml/", optional = true }
34+
# pgml-components = { path = "../packages/pgml-components" }
35+
# pgvector = { version = "0.3", features = [ "sqlx", "postgres" ] }
36+
# rand = "0.8"
37+
# regex = "1.9"
38+
# sailfish = "0.8.0" # 0.8.1 has breaking changes
39+
# serde = "1"
40+
sentry = { version = "0.31", optional = true }
41+
# sentry-log = "0.31"
42+
# sentry-anyhow = "0.31"
43+
# serde_json = "1"
44+
# sqlparser = "0.38"
45+
sqlx = { version = "0.7.3", features = [ "runtime-tokio-rustls", "postgres", "json", "migrate", "time", "uuid", "bigdecimal"], optional = true }
46+
# time = "0.3"
47+
# url = "2.4"
48+
# yaml-rust = "0.4"
49+
zoomies = { git = "https://github.com/HyperparamAI/zoomies.git", branch = "master", optional = true }
50+
# futures = "0.3.29"
51+
52+
axum = { version = "0.7", optional = true }
53+
console_error_panic_hook = "0.1"
54+
leptos = { version = "0.6" }
55+
leptos_axum = { version = "0.6", optional = true }
56+
leptos_meta = { version = "0.6" }
57+
leptos_router = { version = "0.6" }
58+
tokio = { version = "1", features = ["rt-multi-thread", "sync"], optional = true }
59+
tower = { version = "0.4", optional = true }
60+
tower-http = { version = "0.5", features = ["fs"], optional = true }
61+
wasm-bindgen = "=0.2.92"
62+
thiserror = "1"
63+
tracing = { version = "0.1", optional = true }
64+
http = "1"
5265

5366
[dev-dependencies]
5467
scraper = "0.17"
5568

5669
[build-dependencies]
5770
glob = "*"
71+
72+
[features]
73+
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
74+
ssr = [
75+
"dep:axum",
76+
"dep:tokio",
77+
"dep:tower",
78+
"dep:tower-http",
79+
"dep:leptos_axum",
80+
"dep:sqlx",
81+
"dep:zoomies",
82+
"dep:sentry",
83+
"dep:pgml",
84+
"leptos/ssr",
85+
"leptos_meta/ssr",
86+
"leptos_router/ssr",
87+
"dep:tracing",
88+
]
89+
90+
# Defines a size-optimized profile for the WASM bundle in release mode
91+
[profile.wasm-release]
92+
inherits = "release"
93+
opt-level = 'z'
94+
lto = true
95+
codegen-units = 1
96+
panic = "abort"
97+
98+
[package.metadata.leptos]
99+
# The name used by wasm-bindgen/cargo-leptos for the JS/WASM bundle. Defaults to the crate name
100+
output-name = "pgml_dashboard"
101+
102+
# The site root folder is where cargo-leptos generate all output. WARNING: all content of this folder will be erased on a rebuild. Use it in your server setup.
103+
site-root = "target/site"
104+
105+
# The site-root relative folder where all compiled output (JS, WASM and CSS) is written
106+
# Defaults to pkg
107+
site-pkg-dir = "pkg"
108+
109+
# [Optional] The source CSS file. If it ends with .sass or .scss then it will be compiled by dart-sass into CSS. The CSS is optimized by Lightning CSS before being written to <site-root>/<site-pkg>/app.css
110+
# style-file = "style/main.scss"
111+
# Assets source dir. All files found here will be copied and synchronized to site-root.
112+
# The assets-dir cannot have a sub directory with the same name/path as site-pkg-dir.
113+
#
114+
# Optional. Env: LEPTOS_ASSETS_DIR.
115+
assets-dir = "public"
116+
117+
# The IP and port (ex: 127.0.0.1:3000) where the server serves the content. Use it in your server setup.
118+
site-addr = "127.0.0.1:3000"
119+
120+
# The port to use for automatic reload monitoring
121+
reload-port = 3001
122+
123+
# [Optional] Command to use when running end2end tests. It will run in the end2end dir.
124+
# [Windows] for non-WSL use "npx.cmd playwright test"
125+
# This binary name can be checked in Powershell with Get-Command npx
126+
end2end-cmd = "npx playwright test"
127+
end2end-dir = "end2end"
128+
129+
# The browserlist query used for optimizing the CSS.
130+
browserquery = "defaults"
131+
132+
# The environment Leptos will run in, usually either "DEV" or "PROD"
133+
env = "DEV"
134+
135+
# The features to use when compiling the bin target
136+
#
137+
# Optional. Can be over-ridden with the command line parameter --bin-features
138+
bin-features = ["ssr"]
139+
140+
# If the --no-default-features flag should be used when compiling the bin target
141+
#
142+
# Optional. Defaults to false.
143+
bin-default-features = false
144+
145+
# The features to use when compiling the lib target
146+
#
147+
# Optional. Can be over-ridden with the command line parameter --lib-features
148+
lib-features = ["hydrate"]
149+
150+
# If the --no-default-features flag should be used when compiling the lib target
151+
#
152+
# Optional. Defaults to false.
153+
lib-default-features = false
154+
155+
# The profile to use for the lib target when compiling for release
156+
#
157+
# Optional. Defaults to "release".
158+
lib-profile-release = "wasm-release"

pgml-dashboard/src/app.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::error_template::{AppError, ErrorTemplate};
2+
use leptos::*;
3+
use leptos_meta::*;
4+
use leptos_router::*;
5+
6+
#[component]
7+
pub fn App() -> impl IntoView {
8+
// Provides context that manages stylesheets, titles, meta tags, etc.
9+
provide_meta_context();
10+
11+
view! {
12+
13+
14+
// injects a stylesheet into the document <head>
15+
// id=leptos means cargo-leptos will hot-reload this stylesheet
16+
<Stylesheet id="leptos" href="/pkg/pgml_dasboard.css"/>
17+
18+
// sets the document title
19+
<Title text="Welcome to Leptos"/>
20+
21+
// content for this welcome page
22+
<Router fallback=|| {
23+
let mut outside_errors = Errors::default();
24+
outside_errors.insert_with_default_key(AppError::NotFound);
25+
view! {
26+
<ErrorTemplate outside_errors/>
27+
}
28+
.into_view()
29+
}>
30+
<main>
31+
<Routes>
32+
<Route path="" view=HomePage/>
33+
</Routes>
34+
</main>
35+
</Router>
36+
}
37+
}
38+
39+
/// Renders the home page of your application.
40+
#[component]
41+
fn HomePage() -> impl IntoView {
42+
// Creates a reactive value to update the button
43+
let (count, set_count) = create_signal(0);
44+
let on_click = move |_| set_count.update(|count| *count += 1);
45+
46+
view! {
47+
<h1>"Welcome to Leptos!"</h1>
48+
<button on:click=on_click>"Click Me: " {count}</button>
49+
}
50+
}

pgml-dashboard/src/error_template.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use http::status::StatusCode;
2+
use leptos::*;
3+
use thiserror::Error;
4+
5+
#[derive(Clone, Debug, Error)]
6+
pub enum AppError {
7+
#[error("Not Found")]
8+
NotFound,
9+
}
10+
11+
impl AppError {
12+
pub fn status_code(&self) -> StatusCode {
13+
match self {
14+
AppError::NotFound => StatusCode::NOT_FOUND,
15+
}
16+
}
17+
}
18+
19+
// A basic function to display errors served by the error boundaries.
20+
// Feel free to do more complicated things here than just displaying the error.
21+
#[component]
22+
pub fn ErrorTemplate(
23+
#[prop(optional)] outside_errors: Option<Errors>,
24+
#[prop(optional)] errors: Option<RwSignal<Errors>>,
25+
) -> impl IntoView {
26+
let errors = match outside_errors {
27+
Some(e) => create_rw_signal(e),
28+
None => match errors {
29+
Some(e) => e,
30+
None => panic!("No Errors found and we expected errors!"),
31+
},
32+
};
33+
// Get Errors from Signal
34+
let errors = errors.get_untracked();
35+
36+
// Downcast lets us take a type that implements `std::error::Error`
37+
let errors: Vec<AppError> = errors
38+
.into_iter()
39+
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
40+
.collect();
41+
println!("Errors: {errors:#?}");
42+
43+
// Only the response code for the first error is actually sent from the server
44+
// this may be customized by the specific application
45+
#[cfg(feature = "ssr")]
46+
{
47+
use leptos_axum::ResponseOptions;
48+
let response = use_context::<ResponseOptions>();
49+
if let Some(response) = response {
50+
response.set_status(errors[0].status_code());
51+
}
52+
}
53+
54+
view! {
55+
<h1>{if errors.len() > 1 {"Errors"} else {"Error"}}</h1>
56+
<For
57+
// a function that returns the items we're iterating over; a signal is fine
58+
each= move || {errors.clone().into_iter().enumerate()}
59+
// a unique key for each item as a reference
60+
key=|(index, _error)| *index
61+
// renders each item to a view
62+
children=move |error| {
63+
let error_string = error.1.to_string();
64+
let error_code= error.1.status_code();
65+
view! {
66+
<h2>{error_code.to_string()}</h2>
67+
<p>"Error: " {error_string}</p>
68+
}
69+
}
70+
/>
71+
}
72+
}

pgml-dashboard/src/fileserv.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::app::App;
2+
use axum::response::Response as AxumResponse;
3+
use axum::{
4+
body::Body,
5+
extract::State,
6+
http::{Request, Response, StatusCode},
7+
response::IntoResponse,
8+
};
9+
use leptos::*;
10+
use tower::ServiceExt;
11+
use tower_http::services::ServeDir;
12+
13+
pub async fn file_and_error_handler(State(options): State<LeptosOptions>, req: Request<Body>) -> AxumResponse {
14+
let root = options.site_root.clone();
15+
let (parts, body) = req.into_parts();
16+
17+
let mut static_parts = parts.clone();
18+
static_parts.headers.clear();
19+
if let Some(encodings) = parts.headers.get("accept-encoding") {
20+
static_parts.headers.insert("accept-encoding", encodings.clone());
21+
}
22+
23+
let res = get_static_file(Request::from_parts(static_parts, Body::empty()), &root)
24+
.await
25+
.unwrap();
26+
27+
if res.status() == StatusCode::OK {
28+
res.into_response()
29+
} else {
30+
let handler = leptos_axum::render_app_to_stream(options.to_owned(), App);
31+
handler(Request::from_parts(parts, body)).await.into_response()
32+
}
33+
}
34+
35+
async fn get_static_file(request: Request<Body>, root: &str) -> Result<Response<Body>, (StatusCode, String)> {
36+
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
37+
// This path is relative to the cargo root
38+
match ServeDir::new(root)
39+
.precompressed_gzip()
40+
.precompressed_br()
41+
.oneshot(request)
42+
.await
43+
{
44+
Ok(res) => Ok(res.into_response()),
45+
Err(err) => Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Error serving files: {err}"))),
46+
}
47+
}

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