pub mod archive;
pub mod controller;
pub mod schedule;
use rustlavel::prelude::*;
use rustlavel::{Plugin, Setup};
use crate::modules::Module;
use crate::support::settings::{Kind, Setting, choice, env};
pub use controller::BackupController;
fn guard(permission: &str) -> Can {
Can::permission(permission).login_path("/login")
}
const SCHEDULES: &[(&str, &str)] = &[
("disabled", "Never"),
("daily", "Every day"),
("weekly", "Every week"),
("monthly", "Every month"),
];
const RETENTIONS: &[(&str, &str)] = &[
("0", "Keep every backup"),
("3", "Keep the last 3"),
("7", "Keep the last 7"),
("14", "Keep the last 14"),
("30", "Keep the last 30"),
];
static SETTINGS: [Setting; 3] = [
choice("backup.schedule", "disabled", SCHEDULES),
choice("backup.retention", "0", RETENTIONS),
env("backup.path", Kind::Text, "storage/backups", "BACKUP_PATH"),
];
static PERMISSIONS: [(&str, &str); 4] = [
("backups.view", "See the list of database backups"),
("backups.create", "Take a backup"),
("backups.restore", "Restore the database from a backup"),
("backups.delete", "Delete a backup"),
];
pub struct Backup;
impl Plugin for Backup {
fn name(&self) -> &'static str {
"backup"
}
fn register(self: Box<Self>, setup: &mut Setup<'_>) {
setup.router.group("/admin/settings/backup", |backup| {
backup.middleware(Authenticate::default().login_path("/login"));
backup.middleware(crate::support::epoch::SessionEpoch);
backup.middleware(crate::support::idle::IdleTimeout);
backup.post("/create", BackupController::store).middleware(guard("backups.create"));
backup.get("/{id}/download", BackupController::download).middleware(guard("backups.view"));
backup.post("/{id}/restore", BackupController::restore).middleware(guard("backups.restore"));
backup.post("/{id}/delete", BackupController::destroy).middleware(guard("backups.delete"));
});
}
}
impl Module for Backup {
fn permissions(&self) -> &'static [(&'static str, &'static str)] {
&PERMISSIONS
}
fn settings(&self) -> &'static [Setting] {
&SETTINGS
}
}