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

@ -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()