Commit 2fb75318
2fb753185e8fMerged
Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified
Description
[attractor-e7q] Initialize DB pool in main.rs and inject into Leptos context
- Initialize SQLite database pool via db::init_db() on server startup
- Remove hardcoded single-project DocumentWatcher initialization
- Restructure AppState to use db and per-project watchers map
- Inject SqlitePool into Leptos server function context via provide_context
- Expected breakage in documents.rs (fixed in attractor-3jj)
Details
- Commit
- 2fb753185e8f
- Parent
- 8baac2756b13
crates/attractor-web/src/main.rsmodifiedView file
@@ -1,0 +1,98 @@
1+
#[cfg(feature = "ssr")]2+
#[tokio::main]3+
async fn main() {4+
use axum::{routing::get, Router};5+
use leptos::config::get_configuration;6+
use leptos_axum::{generate_route_list, LeptosRoutes};7+
use attractor_web::App;8+
use std::sync::{Arc, Mutex};9+
use std::collections::HashMap;10+
11+
tracing_subscriber::fmt::init();12+
13+
let conf = get_configuration(None).unwrap();14+
let addr = conf.leptos_options.site_addr;15+
let leptos_options = conf.leptos_options;16+
let routes = generate_route_list(App);17+
18+
// Initialize SQLite database19+
let db = attractor_web::server::db::init_db()20+
.await21+
.expect("Failed to initialize database");22+
23+
let terminal_sessions = attractor_web::server::terminal::TerminalSessions::default();24+
25+
let app_state = attractor_web::server::AppState {26+
db: db.clone(),27+
watchers: Arc::new(Mutex::new(HashMap::new())),28+
terminal_sessions,29+
};30+
31+
// API routes with AppState32+
let api_routes = Router::new()33+
.route(34+
"/api/terminal/ws",35+
get(attractor_web::server::terminal::ws_terminal),36+
)37+
.route(38+
"/api/documents/stream",39+
get(attractor_web::server::documents::document_stream),40+
)41+
.route(42+
"/api/stream/{session_id}",43+
get(attractor_web::server::stream::stream_events),44+
)45+
.with_state(app_state);46+
47+
// Leptos routes with LeptosOptions state48+
let app = api_routes49+
.leptos_routes_with_context(50+
&leptos_options,51+
routes,52+
{53+
let pool = db.clone();54+
move || {55+
leptos::prelude::provide_context(pool.clone());56+
}57+
},58+
{59+
let leptos_options = leptos_options.clone();60+
move || shell(leptos_options.clone())61+
},62+
)63+
.fallback(leptos_axum::file_and_error_handler(shell))64+
.with_state(leptos_options);65+
66+
tracing::info!("listening on http://{}", &addr);67+
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();68+
let app = app.into_make_service();69+
axum::serve(listener, app).await.unwrap();70+
}71+
72+
#[cfg(feature = "ssr")]73+
fn shell(options: leptos::config::LeptosOptions) -> impl leptos::IntoView {74+
use leptos::prelude::*;75+
use leptos_meta::*;76+
use attractor_web::App;77+
78+
view! {79+
<!DOCTYPE html>80+
<html lang="en">81+
<head>82+
<meta charset="utf-8"/>83+
<meta name="viewport" content="width=device-width, initial-scale=1"/>84+
<AutoReload options=options.clone() />85+
<HydrationScripts options/>86+
<MetaTags/>87+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/[email protected]/css/xterm.min.css"/>88+
</head>89+
<body>90+
<App/>91+
<script type="module" src="/js/xterm-setup.js"></script>92+
</body>93+
</html>94+
}95+
}96+
97+
#[cfg(not(feature = "ssr"))]98+
pub fn main() {}