Introduces data_dir

This commit is contained in:
Antoine Gersant 2024-10-05 20:04:39 -07:00
parent 664ff721e2
commit 369bf3821b
8 changed files with 31 additions and 11 deletions

View file

@ -16,6 +16,7 @@
- Added a new `/get_songs` endpoint which returns song metadata in bulk.
- Added a new `/peaks` endpoint which returns audio signal peaks that can be used to draw waveform visualizations.
- The `/thumbnail` endpoint supports a new size labeled `tiny` which returns 40x40px images.
- Persistent data, such as playlists, is now saved in a directory that may be configured with the `--data` CLI option or the `POLARIS_DATA_DIR` environment variable.
### Web client

View file

@ -12,6 +12,7 @@ RUNSTATEDIR ?= $(LOCALSTATEDIR)/run
%-system: POLARIS_BIN_PATH := $(BINDIR)/polaris
%-system: export POLARIS_WEB_DIR := $(DATADIR)/polaris/web
%-system: export POLARIS_SWAGGER_DIR := $(DATADIR)/polaris/swagger
%-system: export POLARIS_DATA_DIR := $(LOCALSTATEDIR)/lib/polaris
%-system: export POLARIS_DB_DIR := $(LOCALSTATEDIR)/lib/polaris
%-system: export POLARIS_LOG_DIR := $(LOCALSTATEDIR)/log/polaris
%-system: export POLARIS_CACHE_DIR := $(LOCALSTATEDIR)/cache/polaris
@ -30,6 +31,7 @@ endif
%-xdg: POLARIS_BIN_PATH := $(XDG_BINDIR)/polaris
%-xdg: export POLARIS_WEB_DIR := $(XDG_DATADIR)/web
%-xdg: export POLARIS_SWAGGER_DIR := $(XDG_DATADIR)/swagger
%-xdg: export POLARIS_DATA_DIR := $(XDG_DATADIR)
%-xdg: export POLARIS_DB_DIR := $(XDG_DATADIR)
%-xdg: export POLARIS_LOG_DIR := $(XDG_CACHEDIR)
%-xdg: export POLARIS_CACHE_DIR := $(XDG_CACHEDIR)
@ -58,6 +60,7 @@ list-paths:
$(info POLARIS_BIN_PATH is $(POLARIS_BIN_PATH))
$(info POLARIS_WEB_DIR is $(POLARIS_WEB_DIR))
$(info POLARIS_SWAGGER_DIR is $(POLARIS_SWAGGER_DIR))
$(info POLARIS_DATA_DIR is $(POLARIS_DATA_DIR))
$(info POLARIS_DB_DIR is $(POLARIS_DB_DIR))
$(info POLARIS_LOG_DIR is $(POLARIS_LOG_DIR))
$(info POLARIS_CACHE_DIR is $(POLARIS_CACHE_DIR))
@ -90,6 +93,7 @@ uninstall-bin:
uninstall-data:
rm -rf $(POLARIS_WEB_DIR)
rm -rf $(POLARIS_SWAGGER_DIR)
rm -rf $(POLARIS_DATA_DIR)
rm -rf $(POLARIS_DB_DIR)
rm -rf $(POLARIS_LOG_DIR)
rm -rf $(POLARIS_CACHE_DIR)

View file

@ -172,6 +172,9 @@ impl App {
pub async fn new(port: u16, paths: Paths) -> Result<Self, Error> {
let db = DB::new(&paths.db_file_path).await?;
fs::create_dir_all(&paths.data_dir_path)
.map_err(|e| Error::Io(paths.data_dir_path.clone(), e))?;
fs::create_dir_all(&paths.web_dir_path)
.map_err(|e| Error::Io(paths.web_dir_path.clone(), e))?;
@ -185,7 +188,7 @@ impl App {
fs::create_dir_all(&thumbnails_dir_path)
.map_err(|e| Error::Io(thumbnails_dir_path.clone(), e))?;
let ndb_manager = ndb::Manager::new(&paths.ndb_file_path)?;
let ndb_manager = ndb::Manager::new(&paths.data_dir_path)?;
let vfs_manager = vfs::Manager::new(db.clone());
let settings_manager = settings::Manager::new(db.clone());
let auth_secret = settings_manager.get_auth_secret().await?;

View file

@ -20,7 +20,9 @@ pub struct Manager {
}
impl Manager {
pub fn new(path: &Path) -> Result<Self, Error> {
pub fn new(directory: &Path) -> Result<Self, Error> {
std::fs::create_dir_all(directory).map_err(|e| Error::Io(directory.to_owned(), e))?;
let path = directory.join("polaris.ndb");
let database = native_db::Builder::new()
.create(&MODELS, path)
.map_err(|e| Error::NativeDatabaseCreationError(e))?;

View file

@ -127,9 +127,9 @@ fn main() -> Result<(), Error> {
daemonize(cli_options.foreground, &paths.pid_file_path)?;
info!("Cache files location is {:#?}", paths.cache_dir_path);
info!("Data files location is {:#?}", paths.data_dir_path);
info!("Config files location is {:#?}", paths.config_file_path);
info!("Database file location is {:#?}", paths.db_file_path);
info!("NativeDatabase file location is {:#?}", paths.ndb_file_path);
info!("Log file location is {:#?}", paths.log_file_path);
#[cfg(unix)]
if !cli_options.foreground {

View file

@ -10,6 +10,7 @@ pub struct CLIOptions {
pub config_file_path: Option<PathBuf>,
pub database_file_path: Option<PathBuf>,
pub cache_dir_path: Option<PathBuf>,
pub data_dir_path: Option<PathBuf>,
pub web_dir_path: Option<PathBuf>,
pub swagger_dir_path: Option<PathBuf>,
pub port: Option<u16>,
@ -42,6 +43,7 @@ impl Manager {
config_file_path: matches.opt_str("c").map(PathBuf::from),
database_file_path: matches.opt_str("d").map(PathBuf::from),
cache_dir_path: matches.opt_str("cache").map(PathBuf::from),
data_dir_path: matches.opt_str("data").map(PathBuf::from),
web_dir_path: matches.opt_str("w").map(PathBuf::from),
swagger_dir_path: matches.opt_str("s").map(PathBuf::from),
port: matches.opt_str("p").and_then(|p| p.parse().ok()),
@ -67,6 +69,12 @@ fn get_options() -> getopts::Options {
"set the directory to use as cache",
"DIRECTORY",
);
options.optopt(
"",
"data",
"set the directory for persistent data",
"DIRECTORY",
);
options.optopt("", "log", "set the path to the log file", "FILE");
options.optopt("", "pid", "set the path to the pid file", "FILE");
options.optopt(

View file

@ -5,8 +5,8 @@ use crate::options::CLIOptions;
pub struct Paths {
pub cache_dir_path: PathBuf,
pub config_file_path: Option<PathBuf>,
pub data_dir_path: PathBuf,
pub db_file_path: PathBuf,
pub ndb_file_path: PathBuf,
pub log_file_path: Option<PathBuf>,
#[cfg(unix)]
pub pid_file_path: PathBuf,
@ -22,8 +22,8 @@ impl Default for Paths {
Self {
cache_dir_path: ["."].iter().collect(),
config_file_path: None,
data_dir_path: ["."].iter().collect(),
db_file_path: [".", "db.sqlite"].iter().collect(),
ndb_file_path: [".", "polaris.ndb"].iter().collect(),
log_file_path: Some([".", "polaris.log"].iter().collect()),
pid_file_path: [".", "polaris.pid"].iter().collect(),
swagger_dir_path: [".", "docs", "swagger"].iter().collect(),
@ -41,8 +41,8 @@ impl Default for Paths {
Self {
cache_dir_path: install_directory.clone(),
config_file_path: None,
data_dir_path: install_directory.clone(),
db_file_path: install_directory.join("db.sqlite"),
ndb_file_path: install_directory.join("polaris.ndb"),
log_file_path: Some(install_directory.join("polaris.log")),
swagger_dir_path: install_directory.join("swagger"),
web_dir_path: install_directory.join("web"),
@ -58,14 +58,13 @@ impl Paths {
.map(PathBuf::from)
.map(|p| p.join("db.sqlite"))
.unwrap_or(defaults.db_file_path),
ndb_file_path: option_env!("POLARIS_DB_DIR")
.map(PathBuf::from)
.map(|p| p.join("polaris.ndb"))
.unwrap_or(defaults.ndb_file_path),
config_file_path: None,
cache_dir_path: option_env!("POLARIS_CACHE_DIR")
.map(PathBuf::from)
.unwrap_or(defaults.cache_dir_path),
data_dir_path: option_env!("POLARIS_DATA_DIR")
.map(PathBuf::from)
.unwrap_or(defaults.data_dir_path),
log_file_path: option_env!("POLARIS_LOG_DIR")
.map(PathBuf::from)
.map(|p| p.join("polaris.log"))
@ -92,6 +91,9 @@ impl Paths {
if let Some(path) = &cli_options.config_file_path {
paths.config_file_path = Some(path.clone());
}
if let Some(path) = &cli_options.data_dir_path {
path.clone_into(&mut paths.data_dir_path);
}
if let Some(path) = &cli_options.database_file_path {
path.clone_into(&mut paths.db_file_path);
}

View file

@ -24,8 +24,8 @@ impl TestService for AxumTestService {
let paths = Paths {
cache_dir_path: ["test-output", test_name].iter().collect(),
config_file_path: None,
data_dir_path: ["test-output", test_name].iter().collect(),
db_file_path: output_dir.join("db.sqlite"),
ndb_file_path: output_dir.join("polaris.ndb"),
#[cfg(unix)]
pid_file_path: output_dir.join("polaris.pid"),
log_file_path: None,