Added database connection; #1

This commit is contained in:
xeovalyte 2024-02-09 15:15:12 +01:00
parent dc23f6ae60
commit bb0942eef2
No known key found for this signature in database
6 changed files with 116 additions and 17 deletions

1
application/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

View File

@ -6,3 +6,9 @@ edition = "2021"
[dependencies] [dependencies]
yew = { version = "0.21", features = ["csr"] } yew = { version = "0.21", features = ["csr"] }
yew-router = "0.18.0" yew-router = "0.18.0"
gloo-net = "0.5.0"
futures = "0.3.30"
serde_json = "1.0.113"
serde = "1.0.196"
log = "0.4.6"
wasm-logger = "0.2.0"

View File

@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use yew::prelude::*; use yew::prelude::*;
use yew_router::prelude::*; use yew_router::prelude::*;
@ -6,6 +7,7 @@ use pages::*;
mod components; mod components;
mod pages; mod pages;
mod util;
#[derive(Clone, Routable, PartialEq)] #[derive(Clone, Routable, PartialEq)]
pub enum Route { pub enum Route {
@ -31,19 +33,65 @@ fn switch(route: Route) -> Html {
} }
} }
#[function_component(App)] #[derive(Serialize, Deserialize)]
fn app() -> Html { #[serde(rename_all = "lowercase")]
html! { pub enum MsgTypes {
<BrowserRouter> Users,
<Navbar /> Register,
<main> Message,
<Switch<Route> render={switch} /> // <- must be child of <BrowserRouter> }
</main>
<Footer /> #[derive(Serialize, Deserialize)]
</BrowserRouter> #[serde(rename_all = "camelCase")]
struct WebSocketMessage {
message_type: MsgTypes,
data_array: Option<Vec<String>>,
data: Option<String>,
}
#[derive(PartialEq, Properties, Default)]
struct AppProps;
struct App;
impl Component for App {
type Message = ();
type Properties = AppProps;
fn create(_ctx: &Context<Self>) -> Self {
let wss = util::surrealdb::SurrealWebsocketService::new();
let message = WebSocketMessage {
message_type: MsgTypes::Register,
data: Some("Hi!".to_string()),
data_array: None,
};
if let Ok(_) = wss
.tx
.clone()
.try_send(serde_json::to_string(&message).unwrap())
{
log::debug!("Message send successfully")
}
App
}
fn view(&self, _ctx: &Context<Self>) -> Html {
html! {
<BrowserRouter>
<Navbar />
<main>
<Switch<Route> render={switch} /> // <- must be child of <BrowserRouter>
</main>
<Footer />
</BrowserRouter>
}
} }
} }
fn main() { fn main() {
wasm_logger::init(wasm_logger::Config::default());
yew::Renderer::<App>::new().render(); yew::Renderer::<App>::new().render();
} }

1
application/src/util.rs Normal file
View File

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

View File

@ -0,0 +1,45 @@
use futures::{channel::mpsc::Sender, stream::SplitSink, SinkExt, StreamExt};
use gloo_net::websocket::{futures::WebSocket, Message};
use yew::platform::spawn_local;
pub struct SurrealWebsocketService {
pub tx: Sender<String>,
}
impl SurrealWebsocketService {
pub fn new() -> Self {
let ws = WebSocket::open("ws://localhost:80/rpc").unwrap();
let (mut write, mut read) = ws.split();
let (in_tx, mut in_rx) = futures::channel::mpsc::channel::<String>(1000);
spawn_local(async move {
while let Some(s) = in_rx.next().await {
log::debug!("got event from channel! {}", s);
write.send(Message::Text(s)).await.unwrap();
}
});
spawn_local(async move {
while let Some(msg) = read.next().await {
match msg {
Ok(Message::Text(data)) => {
log::debug!("from websocket: {}", data);
}
Ok(Message::Bytes(b)) => {
let decoded = std::str::from_utf8(&b);
if let Ok(val) = decoded {
log::debug!("from websocket: {}", val);
}
}
Err(e) => {
log::error!("ws: {:?}", e)
}
}
}
log::debug!("WebSocket closed");
});
Self { tx: in_tx }
}
}

View File

@ -28,13 +28,9 @@
trunk trunk
dart-sass dart-sass
rust-analyzer rust-analyzer
openssl llvmPackages.clangNoLibc
pkg-config llvmPackages.lld
cargo-insta dap
llvmPackages_latest.llvm
llvmPackages_latest.bintools
zlib.out
llvmPackages_latest.lld
(rust-bin.stable.latest.default.override { (rust-bin.stable.latest.default.override {
extensions= [ "rust-src" "rust-analyzer" ]; extensions= [ "rust-src" "rust-analyzer" ];
targets = [ "wasm32-unknown-unknown" ]; targets = [ "wasm32-unknown-unknown" ];
@ -44,6 +40,8 @@
shellHook = '' shellHook = ''
alias grep=ripgrep alias grep=ripgrep
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export CC=clang
export CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER=lld
''; '';
}; };
} }