Implemented SurrealDB

This commit is contained in:
xeovalyte 2024-04-04 16:06:36 +02:00
parent 362e73acff
commit 1ceb513228
No known key found for this signature in database
12 changed files with 127 additions and 21 deletions

View File

@ -20,10 +20,16 @@ wasm-bindgen = "=0.2.92"
thiserror = "1" thiserror = "1"
tracing = { version = "0.1", optional = true } tracing = { version = "0.1", optional = true }
http = "1" http = "1"
surrealdb = { version = "1.3.1", optional = true }
serde = "1.0.197"
serde_json = "1.0.115"
cfg-if = "1.0.0"
once_cell = "1.19.0"
[features] [features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"] hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
ssr = [ ssr = [
"dep:surrealdb",
"dep:axum", "dep:axum",
"dep:tokio", "dep:tokio",
"dep:tower", "dep:tower",

View File

@ -3,14 +3,15 @@ use leptos::*;
use leptos_meta::*; use leptos_meta::*;
use leptos_router::*; use leptos_router::*;
use crate::components;
use crate::pages;
#[component] #[component]
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc. // Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context(); provide_meta_context();
view! { view! {
// injects a stylesheet into the document <head> // injects a stylesheet into the document <head>
// id=leptos means cargo-leptos will hot-reload this stylesheet // id=leptos means cargo-leptos will hot-reload this stylesheet
<Stylesheet id="leptos" href="/pkg/application.css"/> <Stylesheet id="leptos" href="/pkg/application.css"/>
@ -27,24 +28,12 @@ pub fn App() -> impl IntoView {
} }
.into_view() .into_view()
}> }>
<components::header::Header />
<main> <main>
<Routes> <Routes>
<Route path="" view=HomePage/> <Route path="" view=pages::index::HomePage/>
</Routes> </Routes>
</main> </main>
</Router> </Router>
} }
} }
/// Renders the home page of your application.
#[component]
fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let (count, set_count) = create_signal(0);
let on_click = move |_| set_count.update(|count| *count += 1);
view! {
<h1>"Welcome to Leptos!"</h1>
<button on:click=on_click>"Click Me: " {count}</button>
}
}

View File

@ -0,0 +1 @@
pub mod header;

View File

@ -0,0 +1,15 @@
use leptos::*;
/// Renders the home page of your application.
#[component]
pub fn Header() -> impl IntoView {
// Creates a reactive value to update the button
view! {
<header>
<div class="header-container">
<h3>"WRB Timings"</h3>
<div>Connection: <span>???</span></div>
</div>
</header>
}
}

View File

@ -1,11 +1,11 @@
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
use application::app::*;
use application::fileserv::file_and_error_handler;
use axum::Router; use axum::Router;
use leptos::*; use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes}; use leptos_axum::{generate_route_list, LeptosRoutes};
use application::app::*;
use application::fileserv::file_and_error_handler;
// Setting get_configuration(None) means we'll be using cargo-leptos's env values // Setting get_configuration(None) means we'll be using cargo-leptos's env values
// For deployment these variables are: // For deployment these variables are:
@ -25,6 +25,11 @@ async fn main() {
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
logging::log!("listening on http://{}", &addr); logging::log!("listening on http://{}", &addr);
application::util::surrealdb::connect()
.await
.expect("Database connection failed");
axum::serve(listener, app.into_make_service()) axum::serve(listener, app.into_make_service())
.await .await
.unwrap(); .unwrap();

View File

@ -0,0 +1 @@
pub mod index;

View File

@ -0,0 +1,14 @@
use leptos::*;
/// Renders the home page of your application.
#[component]
pub fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let (count, set_count) = create_signal(0);
let on_click = move |_| set_count.update(|count| *count += 1);
view! {
<h1>"Welcome to Leptos!"</h1>
<button on:click=on_click>"Click Me: " {count}</button>
}
}

View File

@ -1 +1,32 @@
pub mod tables; use leptos::{logging, server, ServerFnError};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::borrow::Cow;
pub mod schemes;
cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] {
use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::opt::auth::Root;
use surrealdb::Surreal;
pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init);
}
}
#[cfg(feature = "ssr")]
pub async fn connect() -> Result<(), ServerFnError> {
DB.connect::<Ws>("localhost:80").await?;
DB.signin(Root {
username: "root",
password: "root",
})
.await?;
DB.use_ns("wrb").use_db("timings").await?;
Ok(())
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,19 @@
header {
width: 100%;
background-color: $secondary-bg-color;
display: flex;
justify-content: center;
}
.header-container {
display: flex;
align-items: center;
width: 100%;
max-width: 1000px;
margin: 10px 20px;
}
.header-container h3 {
margin: 0;
margin-right: auto;
}

View File

@ -1,4 +1,28 @@
$primary-color: #eb6330;
$primary-color-light: hsl(16.36, 82.38%, 55.49%, 0.8);
$secondary-color: #465651;
$accent-color: #89969f;
$primary-bg-color: #0d0b0b;
$secondary-bg-color: #151719;
$secondary-bg-color-light: hsl(204, 11%, 12%, 1);
$secondary-bg-color-lighter: hsl(204, 11%, 15%, 1);
$accent-bg-color: #181a19;
$text-color: #f3efef;
@import "header";
html,
body { body {
font-family: sans-serif; height: 100vh;
text-align: center; max-width: 100vw;
display: flex;
flex-direction: column;
margin: 0;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}
body {
background-color: $primary-bg-color;
color: $text-color;
align-items: center;
} }