wrb-timings/application/src/main.rs

61 lines
2.1 KiB
Rust
Raw Normal View History

2024-04-06 17:24:27 +02:00
use application::util::websocket::{server::AppState, server::WebSocketState};
2024-04-03 22:43:39 +02:00
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
2024-04-04 16:06:36 +02:00
use application::fileserv::file_and_error_handler;
2024-04-06 17:24:27 +02:00
use application::{app::*, util::websocket::server};
use axum::{routing::get, Router};
2024-04-03 22:43:39 +02:00
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
2024-04-06 17:24:27 +02:00
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
2024-04-03 22:43:39 +02:00
2024-04-05 08:54:01 +02:00
application::util::surrealdb::connect()
.await
.expect("Database connection failed");
2024-04-03 22:43:39 +02:00
// Setting get_configuration(None) means we'll be using cargo-leptos's env values
// For deployment these variables are:
// <https://github.com/leptos-rs/start-axum#executing-a-server-on-a-remote-machine-without-the-toolchain>
// Alternately a file can be specified such as Some("Cargo.toml")
// The file would need to be included with the executable when moved to deployment
let conf = get_configuration(None).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);
2024-04-06 17:24:27 +02:00
let client_set = Arc::new(Mutex::new(HashSet::<uuid::Uuid>::new()));
let (tx, _rx) = broadcast::channel(100);
let websocket_state = WebSocketState { client_set, tx };
let app_state = AppState {
websocket_state: websocket_state.into(),
routes: routes.clone(),
leptos_options,
};
2024-04-03 22:43:39 +02:00
// build our application with a route
let app = Router::new()
2024-04-06 17:24:27 +02:00
.route("/ws", get(server::websocket_handler))
.leptos_routes(&app_state, routes, App)
2024-04-03 22:43:39 +02:00
.fallback(file_and_error_handler)
2024-04-06 17:24:27 +02:00
.with_state(app_state);
2024-04-03 22:43:39 +02:00
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
logging::log!("listening on http://{}", &addr);
2024-04-04 16:06:36 +02:00
2024-04-03 22:43:39 +02:00
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
#[cfg(not(feature = "ssr"))]
pub fn main() {
// no client-side main function
// unless we want this to work with e.g., Trunk for a purely client-side app
// see lib.rs for hydration function instead
}