lib_dir is no longer saved in dbfile

This commit is contained in:
Mark
2023-10-04 13:57:55 +02:00
parent 4a729c596c
commit 3093ec1a25
8 changed files with 108 additions and 364 deletions

View File

@@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
axum = { version = "0.6.19", features = ["headers"] }
clap = { version = "4.4.6", features = ["derive"] }
futures = "0.3.28"
headers = "0.3.8"
musicdb-lib = { version = "0.1.0", path = "../musicdb-lib" }

View File

@@ -1,166 +1,68 @@
mod web;
use std::{
net::SocketAddr,
path::PathBuf,
process::exit,
sync::{Arc, Mutex},
thread,
};
use clap::Parser;
use musicdb_lib::server::run_server;
use musicdb_lib::data::database::Database;
/*
# Exit codes
0 => exited as requested by the user
1 => exit after printing help message
3 => error parsing cli arguments
10 => tried to start with a path that caused some io::Error
11 => tried to start with a path that does not exist (--init prevents this)
*/
#[derive(Parser, Debug)]
struct Args {
/// The file which contains information about the songs in your library
#[arg()]
dbfile: PathBuf,
/// The path containing your actual library.
#[arg()]
lib_dir: PathBuf,
/// skip reading the `dbfile` (because it doesn't exist yet)
#[arg(long)]
init: bool,
/// optional address for tcp connections to the server
#[arg(long)]
tcp: Option<SocketAddr>,
/// optional address on which to start a website which can be used on devices without `musicdb-client` to control playback.
/// requires the `assets/` folder to be present!
#[arg(long)]
web: Option<SocketAddr>,
}
#[tokio::main]
async fn main() {
// parse args
let mut args = std::env::args().skip(1);
let mut tcp_addr = None;
let mut web_addr = None;
let mut lib_dir_for_init = None;
let database = if let Some(path_s) = args.next() {
loop {
if let Some(arg) = args.next() {
if arg.starts_with("--") {
match &arg[2..] {
"init" => {
if let Some(lib_dir) = args.next() {
lib_dir_for_init = Some(lib_dir);
} else {
eprintln!(
"[EXIT]
missing argument: --init <lib path>"
);
exit(3);
}
}
"tcp" => {
if let Some(addr) = args.next() {
if let Ok(addr) = addr.parse() {
tcp_addr = Some(addr)
} else {
eprintln!(
"[EXIT]
bad argument: --tcp <addr:port>: couldn't parse <addr:port>"
);
exit(3);
}
} else {
eprintln!(
"[EXIT]
missing argument: --tcp <addr:port>"
);
exit(3);
}
}
"web" => {
if let Some(addr) = args.next() {
if let Ok(addr) = addr.parse() {
web_addr = Some(addr)
} else {
eprintln!(
"[EXIT]
bad argument: --web <addr:port>: couldn't parse <addr:port>"
);
exit(3);
}
} else {
eprintln!(
"[EXIT]
missing argument: --web <addr:port>"
);
exit(3);
}
}
o => {
eprintln!(
"[EXIT]
Unknown long argument --{o}"
);
exit(3);
}
}
} else if arg.starts_with("-") {
match &arg[1..] {
o => {
eprintln!(
"[EXIT]
Unknown short argument -{o}"
);
exit(3);
}
}
} else {
eprintln!(
"[EXIT]
Argument didn't start with - or -- ({arg})."
);
exit(3);
}
} else {
break;
}
}
let path = PathBuf::from(&path_s);
match path.try_exists() {
Ok(exists) => {
if let Some(lib_directory) = lib_dir_for_init {
Database::new_empty(path, lib_directory.into())
} else if exists {
Database::load_database(path).unwrap()
} else {
eprintln!(
"[EXIT]
The provided path does not exist."
);
exit(11);
}
}
Err(e) => {
eprintln!(
"[EXIT]
Error getting information about the provided path '{path_s}': {e}"
);
exit(10);
}
}
let args = Args::parse();
let database = if args.init {
Database::new_empty(args.dbfile, args.lib_dir)
} else {
eprintln!(
"[EXIT]
musicdb-server - help
musicdb-server <path to database file> <options> <options> <...>
options:
--init <lib directory>
--tcp <addr:port>
--web <addr:port>
this help was shown because no arguments were provided."
);
exit(1);
match Database::load_database(args.dbfile.clone(), args.lib_dir.clone()) {
Ok(db) => db,
Err(e) => {
eprintln!("Couldn't load database!");
eprintln!(" dbfile: {:?}", args.dbfile);
eprintln!(" libdir: {:?}", args.lib_dir);
eprintln!(" err: {}", e);
exit(1);
}
}
};
// database can be shared by multiple threads using Arc<Mutex<_>>
let database = Arc::new(Mutex::new(database));
if tcp_addr.is_some() || web_addr.is_some() {
if let Some(addr) = web_addr {
if args.tcp.is_some() || args.web.is_some() {
if let Some(addr) = &args.web {
let (s, mut r) = tokio::sync::mpsc::channel(2);
let db = Arc::clone(&database);
thread::spawn(move || run_server(database, tcp_addr, Some(s)));
thread::spawn(move || run_server(database, args.tcp, Some(s)));
if let Some(sender) = r.recv().await {
web::main(db, sender, addr).await;
web::main(db, sender, *addr).await;
}
} else {
run_server(database, tcp_addr, None);
run_server(database, args.tcp, None);
}
} else {
eprintln!("nothing to do, not starting the server.");

View File

@@ -438,7 +438,7 @@ async fn sse_handler(
.collect::<String>(),
)
}
Command::Save | Command::SetLibraryDirectory(_) => return Poll::Pending,
Command::Save | Command::InitComplete => return Poll::Pending,
}))
} else {
return Poll::Pending;
@@ -673,17 +673,15 @@ fn build_queue_content_build(
HtmlPart::Plain(v) => html.push_str(v),
HtmlPart::Insert(key) => match key.as_str() {
"path" => html.push_str(&path),
"content" => {
build_queue_content_build(
db,
state,
html,
&inner,
format!("{path}-0"),
current,
true,
)
}
"content" => build_queue_content_build(
db,
state,
html,
&inner,
format!("{path}-0"),
current,
true,
),
_ => {}
},
}