Initial public release

This commit is contained in:
thakares committed 2026-06-11 20:19:03 +05:30
commit c1107147b4
92 files changed
+10562

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
use std::path::PathBuf;
use tracing::info;
use crate::config::Config;
use crate::db::Db;
use crate::jobs::backup::perform_backup;
pub async fn run(
out: Option<String>,
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
if let Some(o) = out { config.backup_dir = PathBuf::from(o); }
// Init DB connections to ensure databases exist and migrate if needed
let db = Db::init(&config)?;
info!("Starting database backup...");
let backup_path = perform_backup(&db, &config).await?;
info!("Database backup generated successfully: {}", backup_path);
Ok(())
}
+46
View File
@@ -0,0 +1,46 @@
use std::path::PathBuf;
use std::io::{self, Write};
use tracing::{info, error};
use crate::config::Config;
use crate::db::Db;
use crate::auth::hash_password;
pub async fn run(
username: Option<String>,
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
let db = Db::init(&config)?;
let final_username = match username {
Some(u) => u,
None => read_input("Enter administrator username: "),
};
if final_username.trim().is_empty() {
error!("Username cannot be empty");
return Ok(());
}
let password = read_input("Enter password: ");
if password.trim().is_empty() {
error!("Password cannot be empty");
return Ok(());
}
let hash = hash_password(&password).map_err(|e| e.to_string())?;
let conn = db.admin.lock().unwrap();
let u = crate::db::admin::create_user(&conn, &final_username, &hash)?;
info!("Successfully created admin user: {} (ID: {})", u.username, u.id);
Ok(())
}
fn read_input(prompt: &str) -> String {
print!("{}", prompt);
let _ = io::stdout().flush();
let mut input = String::new();
let _ = io::stdin().read_line(&mut input);
input.trim().to_string()
}
+76
View File
@@ -0,0 +1,76 @@
use std::path::PathBuf;
use tracing::info;
use crate::config::Config;
use crate::db::sqlite;
use rusqlite::Connection;
/// Run comprehensive database diagnostics.
///
/// Opens each database, collects health reports (schema version, journal mode,
/// foreign key enforcement, integrity check), and prints a summary.
pub async fn run(
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
info!("Running BZOD database diagnostics...");
println!("BZOD Database Doctor");
println!("====================");
println!("Data directory: {:?}", config.data_dir);
println!();
let databases = ["admin", "content", "analytics", "system"];
let mut all_healthy = true;
for db_name in &databases {
let db_path = config.data_dir.join(format!("{}.db", db_name));
if !db_path.exists() {
println!("Database: {}", db_name);
println!(" Status: NOT FOUND at {:?}", db_path);
println!();
all_healthy = false;
continue;
}
match Connection::open(&db_path) {
Ok(conn) => {
match sqlite::collect_health_report(&conn, db_name) {
Ok(report) => {
println!("Database: {}", report.database);
println!(" Path: {:?}", db_path);
println!(" Schema version: {}", report.schema_version);
println!(" Journal mode: {}", report.journal_mode);
println!(" Foreign keys: {}", if report.foreign_keys_enabled { "enabled" } else { "DISABLED" });
println!(" Integrity: {}", if report.integrity_ok { "ok" } else { "FAILED" });
if !report.integrity_ok || !report.foreign_keys_enabled {
all_healthy = false;
}
}
Err(e) => {
println!("Database: {}", db_name);
println!(" Status: ERROR collecting health report: {}", e);
all_healthy = false;
}
}
}
Err(e) => {
println!("Database: {}", db_name);
println!(" Status: FAILED to open: {}", e);
all_healthy = false;
}
}
println!();
}
println!("--------------------");
if all_healthy {
println!("Overall status: HEALTHY");
} else {
println!("Overall status: ISSUES DETECTED");
}
Ok(())
}
+24
View File
@@ -0,0 +1,24 @@
use std::path::PathBuf;
use tracing::info;
use crate::config::Config;
use crate::db::Db;
pub async fn run(
data_dir: Option<String>,
dry_run: bool,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
if dry_run {
info!("Dry run enabled: pending database migrations will be reported but not applied.");
info!("Data directory: {:?}", config.data_dir);
return Ok(());
}
info!("Running database migrations...");
let _db = Db::init(&config)?;
info!("Database migrations applied successfully.");
Ok(())
}
+75
View File
@@ -0,0 +1,75 @@
use clap::{Parser, Subcommand};
pub mod serve;
pub mod backup;
pub mod restore;
pub mod migrate;
pub mod stats;
pub mod validate;
pub mod create_admin;
pub mod doctor;
#[derive(Parser)]
#[command(name = "bzod")]
#[command(about = "BZOD - Personal Redirector & Landing Page Platform")]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
/// Start the BZOD web server
Serve {
#[arg(long)]
host: Option<String>,
#[arg(long)]
port: Option<u16>,
#[arg(long)]
data_dir: Option<String>,
},
/// Create a tar.gz backup of all databases
Backup {
#[arg(long)]
out: Option<String>,
#[arg(long)]
data_dir: Option<String>,
},
/// Restore databases from a tar.gz backup file
Restore {
#[arg(long, required = true)]
file: String,
#[arg(long)]
data_dir: Option<String>,
},
/// Apply pending database schema migrations
Migrate {
#[arg(long)]
data_dir: Option<String>,
/// Show what migrations would be applied without executing them
#[arg(long)]
dry_run: bool,
},
/// Print database statistics and record counts in the terminal
Stats {
#[arg(long)]
data_dir: Option<String>,
},
/// Perform a one-shot validation of all registered short link destinations
Validate {
#[arg(long)]
data_dir: Option<String>,
},
/// Create a new administrator user in the database
CreateAdmin {
#[arg(long)]
username: Option<String>,
#[arg(long)]
data_dir: Option<String>,
},
/// Run database diagnostics and health checks
Doctor {
#[arg(long)]
data_dir: Option<String>,
},
}
+45
View File
@@ -0,0 +1,45 @@
use std::path::PathBuf;
use std::fs::File;
use std::io::{self, Write};
use tracing::{info, error};
use flate2::read::GzDecoder;
use tar::Archive;
use crate::config::Config;
pub async fn run(
file: String,
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
let file_path = PathBuf::from(file);
if !file_path.exists() {
error!("Backup file not found: {:?}", file_path);
return Ok(());
}
info!("WARNING: Restoring will overwrite existing databases in {:?}", config.data_dir);
print!("Are you sure you want to restore? (y/N): ");
let _ = io::stdout().flush();
let mut confirm = String::new();
let _ = io::stdin().read_line(&mut confirm);
if !confirm.trim().eq_ignore_ascii_case("y") {
info!("Restore cancelled.");
return Ok(());
}
if !config.data_dir.exists() {
std::fs::create_dir_all(&config.data_dir)?;
}
info!("Restoring backup from: {:?}", file_path);
let f = File::open(&file_path)?;
let tar_gz = GzDecoder::new(f);
let mut archive = Archive::new(tar_gz);
archive.unpack(&config.data_dir)?;
info!("Database files successfully restored.");
Ok(())
}
+75
View File
@@ -0,0 +1,75 @@
use std::path::PathBuf;
use std::time::Instant;
use tracing::info;
use crate::config::Config;
use crate::db::Db;
use crate::analytics::AnalyticsQueue;
use crate::state::AppState;
use crate::web::create_router;
pub async fn run(
host: Option<String>,
port: Option<u16>,
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(h) = host { config.host = h; }
if let Some(p) = port { config.port = p; }
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
info!("Starting BZOD server on {}:{}", config.host, config.port);
info!("Database directory: {:?}", config.data_dir);
// Init DBs
let db = Db::init(&config)?;
// Init Queue
let queue = AnalyticsQueue::new(db.clone(), 1000);
// Spawn background tasks
let link_checker_db = db.clone();
let link_checker_interval = config.link_check_interval_mins;
tokio::spawn(async move {
crate::jobs::run_link_checker(link_checker_db, link_checker_interval).await;
});
let aggregator_db = db.clone();
let aggregator_interval = config.aggregation_interval_mins;
tokio::spawn(async move {
crate::jobs::run_aggregator(aggregator_db, aggregator_interval).await;
});
let retention_db = db.clone();
let retention_days = config.data_retention_days;
tokio::spawn(async move {
crate::jobs::run_retention_cleaner(retention_db, retention_days).await;
});
// Spawn optional backup scheduler
let backup_db = db.clone();
let backup_config = config.clone();
tokio::spawn(async move {
crate::jobs::backup::run_backup_scheduler(backup_db, backup_config).await;
});
let state = AppState {
admin_db: db.admin.clone(),
content_db: db.content.clone(),
analytics_db: db.analytics.clone(),
system_db: db.system.clone(),
db: db.clone(),
config: config.clone(),
analytics_queue: queue,
start_time: Instant::now(),
};
// Run axum server
let router = create_router(state);
let addr = format!("{}:{}", config.host, config.port);
let listener = tokio::net::TcpListener::bind(&addr).await?;
info!("Listening for requests on http://{}", addr);
axum::serve(listener, router).await?;
Ok(())
}
+49
View File
@@ -0,0 +1,49 @@
use std::path::PathBuf;
use crate::config::Config;
use crate::db::Db;
pub async fn run(
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
let db = Db::init(&config)?;
println!("=== BZOD Database Stats ===");
println!("Storage Directory: {:?}", config.data_dir);
let files = vec!["admin.db", "content.db", "analytics.db", "system.db"];
for f in files {
let p = config.data_dir.join(f);
if p.exists() {
let sz = std::fs::metadata(&p)?.len();
println!(" File: {} - Size: {} bytes ({:.2} MB)", f, sz, sz as f64 / 1_048_576.0);
}
}
let users_count = {
let conn = db.admin.lock().unwrap();
crate::db::admin::get_user_count(&conn)?
};
println!("Users Count: {}", users_count);
let (urls_total, urls_active, urls_dead) = {
let conn = db.content.lock().unwrap();
crate::db::content::get_url_counts(&conn)?
};
println!("Shortened URLs: {} total ({} active / {} dead)", urls_total, urls_active, urls_dead);
let pages_count = {
let conn = db.content.lock().unwrap();
crate::db::content::get_landing_page_count(&conn)?
};
println!("Landing Pages: {}", pages_count);
let total_visits = {
let conn = db.analytics.lock().unwrap();
crate::db::analytics::get_total_clicks(&conn)?
};
println!("Redirect Clicks: {}", total_visits);
Ok(())
}
+26
View File
@@ -0,0 +1,26 @@
use std::path::PathBuf;
use tracing::info;
use reqwest::Client;
use std::time::Duration;
use crate::config::Config;
use crate::db::Db;
pub async fn run(
data_dir: Option<String>,
mut config: Config,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(d) = data_dir { config.data_dir = PathBuf::from(d); }
let db = Db::init(&config)?;
info!("Running one-shot link validation...");
let client = Client::builder()
.timeout(Duration::from_secs(10))
.user_agent("bzod-cli-checker/0.1")
.build()?;
crate::jobs::perform_link_check(&db, &client).await?;
info!("Link validation complete.");
Ok(())
}