Initial commit for desktop configuration. Broken now

This commit is contained in:
Igor Katson 2023-12-06 14:30:32 +00:00
parent dd355b0a74
commit a3475784e9
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
8 changed files with 304 additions and 57 deletions

View file

@ -26,6 +26,7 @@ pub type Result<T> = std::result::Result<T, ApiError>;
/// Library API for use in different web frameworks.
/// Contains all methods you might want to expose with (de)serializable inputs/outputs.
#[derive(Clone)]
pub struct Api {
session: Arc<Session>,
rust_log_reload_tx: Option<UnboundedSender<String>>,

View file

@ -27,6 +27,14 @@ impl ApiError {
}
}
pub const fn new_from_text(status: StatusCode, text: &'static str) -> Self {
Self {
status: Some(status),
kind: ApiErrorKind::Text(text),
plaintext: false,
}
}
#[allow(dead_code)]
pub fn not_implemented(msg: &str) -> Self {
Self {
@ -69,6 +77,7 @@ impl ApiError {
enum ApiErrorKind {
TorrentNotFound(usize),
DhtDisabled,
Text(&'static str),
Other(anyhow::Error),
}
@ -91,6 +100,7 @@ impl Serialize for ApiError {
ApiErrorKind::TorrentNotFound(_) => "torrent_not_found",
ApiErrorKind::DhtDisabled => "dht_disabled",
ApiErrorKind::Other(_) => "internal_error",
ApiErrorKind::Text(_) => "internal_error",
},
human_readable: format!("{self}"),
status: self.status().as_u16(),
@ -130,6 +140,7 @@ impl std::fmt::Display for ApiError {
ApiErrorKind::TorrentNotFound(idx) => write!(f, "torrent {idx} not found"),
ApiErrorKind::Other(err) => write!(f, "{err:?}"),
ApiErrorKind::DhtDisabled => write!(f, "DHT is disabled"),
ApiErrorKind::Text(t) => write!(f, "{t}"),
}
}
}

View file

@ -373,6 +373,11 @@ impl Session {
Self::new_with_opts(output_folder, SessionOptions::default()).await
}
pub fn default_persistence_filename() -> anyhow::Result<PathBuf> {
let dir = get_configuration_directory("session")?;
Ok(dir.data_dir().join("session.json"))
}
/// Create a new session with options.
pub async fn new_with_opts(
output_folder: PathBuf,
@ -405,9 +410,7 @@ impl Session {
let peer_opts = opts.peer_opts.unwrap_or_default();
let persistence_filename = match opts.persistence_filename {
Some(filename) => filename,
None => get_configuration_directory("session")?
.data_dir()
.join("session.json"),
None => Self::default_persistence_filename()?,
};
let spawner = BlockingSpawner::default();
@ -608,7 +611,8 @@ impl Session {
}
}
fn spawn(
/// Spawn a task in the context of the session.
pub fn spawn(
&self,
name: &str,
span: tracing::Span,
@ -626,7 +630,9 @@ impl Session {
});
}
pub fn stop(&self) {
/// Stop the session and all managed tasks.
// TODO: this probably doesn't kill everything properly.
pub async fn stop(&self) {
let _ = self.cancel_tx.send(());
}