// Leptos view types are deeply nested; release builds overflow the default limit #![recursion_limit = "512"] use actix_web::middleware::Logger; use leptos_captcha::spow::pow::Pow; use log::error; use rezervator::backend::appearance::check_appearance; use rezervator::backend::company::check_company; use rezervator::backend::mail::{check_messages, Mailer}; use rezervator::backend::user::create_admin; #[cfg(feature = "ssr")] #[actix_web::main] async fn main() -> std::io::Result<()> { use actix_files::Files; use actix_web::*; use leptos::prelude::*; use leptos::config::get_configuration; use leptos_meta::MetaTags; use leptos_actix::{generate_route_list, LeptosRoutes}; use rezervator::app::*; use rezervator::backend::*; use actix_session::storage::CookieSessionStore; use actix_session::SessionMiddleware; use actix_web::cookie::Key; use actix_web::web::Data; use sqlx::migrate; use sqlx::postgres::PgPoolOptions; use rezervator::backend::auth_middleware::Authentication; use rezervator::config::load_config; use env_logger::Env; use std::env::args; use getopts::Options; use log::info; let args: Vec = args().collect(); let mut opts = Options::new(); opts.optopt("c", "config", "Path to config file. Default is ./config.toml", ""); opts.optflag("h", "help", "Show help"); let matches = opts.parse(&args[1..]).expect("Can't parse commandline"); if matches.opt_present("h") { println!("{}",opts.usage(format!("Usage: {} [options]", args.get(0).unwrap_or(&"".to_string())).as_str())); return Ok(()); } Pow::init_random().expect("Cannot init captcha"); let cfg_path = matches.opt_str("c").unwrap_or("config.toml".to_string()); let srv_conf = load_config(cfg_path.trim()); env_logger::Builder::from_env(Env::default().default_filter_or(srv_conf.logging().severity())).init(); info!("Starting server"); let conf = get_configuration(None).unwrap(); let key = Key::from(srv_conf.session().key()); let pool = PgPoolOptions::new() .max_connections(10) .connect(&srv_conf.database().con_string()).await.unwrap(); migrate!().run(&pool).await.expect("could not run SQLx migrations"); let mut mailer = Mailer::new(srv_conf.mailing()); mailer.start_sender(); if let Err(e) = create_admin(&pool).await { error!("Error while checking admin user: {:?}", e); } if let Err(e) = check_company(&pool).await { error!("Error while checking company: {:?}", e); } if let Err(e) = check_messages(&pool).await { error!("Error while checking messages: {:?}", e); } if let Err(e) = check_appearance(&pool).await { error!("Error while checking messages: {:?}", e); } HttpServer::new(move || { // Generate the list of routes in your Leptos App let routes = generate_route_list(App); let leptos_options = &conf.leptos_options; let site_root = leptos_options.site_root.clone().to_string(); App::new() .app_data(Data::new(AppData::new(pool.clone(), mailer.clone()))) .wrap(Logger::default()) .wrap(Authentication) .wrap(SessionMiddleware::new( CookieSessionStore::default(), key.clone() )) // Server functions under /api are registered by leptos_routes .leptos_routes(routes, { let leptos_options = leptos_options.clone(); move || { view! { } } }) .app_data(Data::new(leptos_options.to_owned())) .service(appearance::upload) .service(Files::new("/", site_root)) .wrap(middleware::Compress::default()) }) .bind(srv_conf.network().bind_address())? .run() .await } #[cfg(not(any(feature = "ssr", feature = "csr")))] pub fn main() { // no client-side main function // unless we want this to work with e.g., Trunk for pure client-side testing // see lib.rs for hydration function instead // see optional feature `ssg` instead } #[cfg(all(not(feature = "ssr"), feature = "csr"))] pub fn main() { // a client-side main function is required for using `trunk serve` // prefer using `cargo leptos serve` instead // to run: `trunk serve --open --features ssg` use rezervator::app::*; console_error_panic_hook::set_once(); leptos::mount::mount_to_body(App); }