#[cfg(feature = "ssr")] pub(crate) mod beacons { #[allow(dead_code)] pub const LINUX_BEACON: &'static [u8] = include_bytes!(std::env!("SPARSE_BEACON_LINUX")); #[allow(dead_code)] pub const FREEBSD_BEACON: &'static [u8] = include_bytes!(std::env!("SPARSE_BEACON_FREEBSD")); #[allow(dead_code)] pub const WINDOWS_BEACON: &'static [u8] = include_bytes!(std::env!("SPARSE_BEACON_WINDOWS")); #[allow(dead_code)] pub const LINUX_INSTALLER: &'static [u8] = include_bytes!(std::env!("SPARSE_INSTALLER_LINUX")); #[allow(dead_code)] pub const FREEBSD_INSTALLER: &'static [u8] = include_bytes!(std::env!("SPARSE_INSTALLER_FREEBSD")); } #[cfg(feature = "ssr")] mod cli; #[cfg(feature = "ssr")] mod webserver; #[cfg(feature = "ssr")] pub mod users; pub mod error; pub mod db; #[cfg(feature = "ssr")] #[tokio::main] async fn main() -> anyhow::Result { use std::{path::PathBuf, process::ExitCode, str::FromStr}; use structopt::StructOpt; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use sqlx::sqlite::{SqlitePool, SqliteConnectOptions}; tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| format!("{}=debug,tower_http=trace", env!("CARGO_CRATE_NAME")).into()), ) .with(tracing_subscriber::fmt::layer()) .init(); let options = cli::Options::from_args(); let db_location = options.db_location.clone() .or(std::env::var("DATABASE_URL") .map(|p| p.replace("sqlite://", "")) .map(PathBuf::from) .ok()) .unwrap_or(PathBuf::from("./db.sqlite")); tracing::info!("Using database {}", db_location.display()); let db_exists = std::fs::metadata(&db_location); let run_init = if let Err(e) = db_exists { if !options.init_ok { tracing::error!("Database doesn't exist, and initialization not allowed!"); tracing::error!("{:?}", e); return Ok(ExitCode::FAILURE) } tracing::info!("Database doesn't exist, readying initialization"); true } else { false }; let pool = SqlitePool::connect_with( SqliteConnectOptions::from_str(&format!("sqlite://{}", db_location.to_string_lossy()))? .create_if_missing(options.init_ok) ).await?; tracing::info!("Running database migrations..."); sqlx::migrate!() .run(&pool) .await?; tracing::info!("Done running database migrations!"); match options.command.clone() { Some(cli::Command::Serve { management_address, bind_address }) => { tracing::info!("Performing requested action, acting as web server"); webserver::serve_web(management_address, bind_address, pool).await } Some(cli::Command::ExtractPubKey { }) => { Ok(ExitCode::SUCCESS) } Some(cli::Command::User { command }) => { cli::user::handle_user_command(command, pool).await } None => { use std::net::{Ipv4Addr, SocketAddrV4}; tracing::info!("Performing default action of acting as web server"); let default_management_ip = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 3000); let default_beacon_ip = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 5000); webserver::serve_web(default_management_ip, default_beacon_ip, pool).await } } } #[cfg(not(feature = "ssr"))] pub fn main() {}