watch/src/main.rs

64 lines
2.1 KiB
Rust
Raw Permalink Normal View History

2024-09-02 18:34:30 +00:00
#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
2024-09-02 21:43:49 +00:00
use std::env;
2024-09-02 18:34:30 +00:00
use axum::Router;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
2024-09-02 21:43:49 +00:00
use log::info;
use sqlx::postgres::PgPoolOptions;
2024-09-02 18:34:30 +00:00
use watch::app::*;
use watch::fileserv::file_and_error_handler;
// 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-09-02 21:43:49 +00:00
let pool = PgPoolOptions::new()
.test_before_acquire(true)
.max_lifetime(None)
.connect_lazy(&env::var("DATABASE_URL").expect("DATABASE_URL was not set!"))
.expect("Failed to create database connection pool");
2024-09-02 18:34:30 +00:00
// build our application with a route
let app = Router::new()
2024-09-02 21:43:49 +00:00
.leptos_routes_with_context(
&leptos_options,
routes,
{
let pool = pool.clone();
move || provide_context(pool.clone())
},
App,
)
2024-09-02 18:34:30 +00:00
.fallback(file_and_error_handler)
2024-09-02 21:43:49 +00:00
.with_state(leptos_options)
.with_state(pool);
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", format!("{}=debug", module_path!()));
}
env_logger::init();
2024-09-02 18:34:30 +00:00
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
2024-09-02 21:43:49 +00:00
info!("listening on http://{}", &addr);
2024-09-02 18:34:30 +00: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
}