Desktop: run HTTP API, show version

This commit is contained in:
Igor Katson 2023-12-06 12:14:26 +00:00
parent 3a69f04782
commit 2d43f41664
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
6 changed files with 71 additions and 14 deletions

View file

@ -53,3 +53,8 @@ pub use clone_to_owned::CloneToOwned;
pub use librqbit_core::magnet::*;
pub use librqbit_core::peer_id::*;
pub use librqbit_core::torrent_metainfo::*;
/// The cargo version of librqbit.
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

View file

@ -178,7 +178,7 @@ enum SubCommand {
Download(DownloadOpts),
}
// Iint logging and make a channel to send new RUST_LOG values to.
// Init logging and make a channel to send new RUST_LOG values to.
fn init_logging(opts: &Opts) -> tokio::sync::mpsc::UnboundedSender<String> {
let default_rust_log = match opts.log_level.as_ref() {
Some(level) => match level {

View file

@ -3008,7 +3008,7 @@ dependencies = [
[[package]]
name = "rqbit-desktop"
version = "0.0.0"
version = "5.0.0-beta.1"
dependencies = [
"anyhow",
"base64 0.21.5",
@ -3020,6 +3020,7 @@ dependencies = [
"tauri",
"tauri-build",
"tokio",
"tracing",
"tracing-subscriber",
]

View file

@ -1,6 +1,6 @@
[package]
name = "rqbit-desktop"
version = "0.0.0"
version = "5.0.0-beta.1"
description = "rqbit torrent client"
authors = ["you"]
license = ""
@ -22,7 +22,8 @@ anyhow = "1.0.75"
base64 = "0.21.5"
http = "1.0.0"
directories = "5.0.1"
tracing-subscriber = "0.3.18"
tracing-subscriber = {version = "0.3.18", features = ["env-filter"] }
tracing = "0.1"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem

View file

@ -8,8 +8,9 @@ use librqbit::{
ApiAddTorrentResponse, EmptyJsonResponse, TorrentDetailsResponse, TorrentListResponse,
TorrentStats,
},
AddTorrent, AddTorrentOptions, Api, ApiError, Session, SessionOptions,
librqbit_spawn, AddTorrent, AddTorrentOptions, Api, ApiError, Session, SessionOptions,
};
use tracing::error_span;
struct State {
api: Api,
@ -97,7 +98,44 @@ async fn torrent_action_start(
state.api.api_torrent_action_start(id)
}
#[tauri::command]
fn get_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
fn init_logging() -> tokio::sync::mpsc::UnboundedSender<String> {
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
let (stderr_filter, reload_stderr_filter) =
tracing_subscriber::reload::Layer::new(EnvFilter::builder().parse("info").unwrap());
let layered = tracing_subscriber::registry().with(fmt::layer().with_filter(stderr_filter));
layered.init();
let (reload_tx, mut reload_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
librqbit_spawn(
"fmt_filter_reloader",
error_span!("fmt_filter_reloader"),
async move {
while let Some(rust_log) = reload_rx.recv().await {
let stderr_env_filter = match EnvFilter::builder().parse(&rust_log) {
Ok(f) => f,
Err(e) => {
eprintln!("can't parse env filter {:?}: {:#?}", rust_log, e);
continue;
}
};
eprintln!("setting RUST_LOG to {:?}", rust_log);
let _ = reload_stderr_filter.reload(stderr_env_filter);
}
Ok(())
},
);
reload_tx
}
async fn start_session() {
let rust_log_reload_tx = init_logging();
tauri::async_runtime::set(tokio::runtime::Handle::current());
let download_folder = directories::UserDirs::new()
@ -119,7 +157,14 @@ async fn start_session() {
.await
.expect("couldn't set up librqbit session");
let api = Api::new(session, None);
let api = Api::new(session.clone(), None);
librqbit_spawn(
"http api",
error_span!("http_api"),
librqbit::http_api::HttpApi::new(session, Some(rust_log_reload_tx))
.make_http_api_and_run("127.0.0.1:3000".parse().unwrap(), false),
);
tauri::Builder::default()
.manage(State { api })
@ -133,13 +178,13 @@ async fn start_session() {
torrent_action_forget,
torrent_action_start,
torrent_create_from_base64_file,
get_version
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
fn main() {
tracing_subscriber::fmt::init();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()

View file

@ -2,13 +2,18 @@ import { StrictMode } from "react";
import ReactDOM from 'react-dom/client';
import { APIContext, RqbitWebUI } from "./rqbit-webui-src/rqbit-web";
import { API } from "./api";
import { invoke } from "@tauri-apps/api";
let version = invoke<string>("get_version").then((version) => {
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<StrictMode>
<APIContext.Provider value={API}>
<RqbitWebUI title={`Rqbit Desktop v${version}`} />
</APIContext.Provider>
</StrictMode>
);
});
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<StrictMode>
<APIContext.Provider value={API}>
<RqbitWebUI title="Rqbit Desktop v5.0.0-beta.0" />
</APIContext.Provider>
</StrictMode>
);