mirror of
https://github.com/storytold/artcraft.git
synced 2026-10-31 07:27:51 +00:00
This is a fresh start for the `artcraft` repo. The old monorepo, which included our web API gateway, job services, SRE tools, various frontends, and more has moved to `artcraft-services` You can find the full git history here: https://github.com/storytold/artcraft-services This change was motivated by the fact that the entire git history was hundreds of gigabytes in size, which is unwieldy for anyone not in the core team that doesn't care about support services outside of the core artcraft app. We wanted to keep this repo in its well-known place since we already have users and activity here. Please update your remotes to either `artcraft-services` or make a fresh clone of this repo. History can be found at the artcraft-services repo.
97 lines
3.0 KiB
Rust
97 lines
3.0 KiB
Rust
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//! Access the HTTP client written in Rust.
|
|
|
|
pub use reqwest;
|
|
use tauri::{
|
|
plugin::{Builder, TauriPlugin},
|
|
Manager, Runtime,
|
|
};
|
|
|
|
pub use error::{Error, Result};
|
|
|
|
// NB(bt): We're making everything public so we can access it.
|
|
pub mod commands;
|
|
pub mod error;
|
|
#[cfg(feature = "cookies")]
|
|
pub mod reqwest_cookie_store;
|
|
pub mod scope;
|
|
|
|
#[cfg(feature = "cookies")]
|
|
const COOKIES_FILENAME: &str = ".cookies";
|
|
|
|
// NB(bt): We've vendored in this library so we can poke at the cookie data store.
|
|
pub struct Http {
|
|
#[cfg(feature = "cookies")]
|
|
pub cookies_jar: std::sync::Arc<crate::reqwest_cookie_store::CookieStoreMutex>,
|
|
}
|
|
|
|
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
|
Builder::<R>::new("http")
|
|
.setup(|app, _| {
|
|
#[cfg(feature = "cookies")]
|
|
let cookies_jar = {
|
|
use crate::reqwest_cookie_store::*;
|
|
use std::fs::File;
|
|
use std::io::BufReader;
|
|
|
|
let cache_dir = app.path().app_cache_dir()?;
|
|
std::fs::create_dir_all(&cache_dir)?;
|
|
|
|
let path = cache_dir.join(COOKIES_FILENAME);
|
|
|
|
// NB(bt): Let's always log this.
|
|
println!("Tauri plugin http: using cookie store at {}", path.display());
|
|
|
|
let file = File::options()
|
|
.create(true)
|
|
.append(true)
|
|
.read(true)
|
|
.open(&path)?;
|
|
|
|
let reader = BufReader::new(file);
|
|
CookieStoreMutex::load(path.clone(), reader).unwrap_or_else(|_e| {
|
|
#[cfg(feature = "tracing")]
|
|
tracing::warn!(
|
|
"failed to load cookie store: {_e}, falling back to empty store"
|
|
);
|
|
CookieStoreMutex::new(path, Default::default())
|
|
})
|
|
};
|
|
|
|
let state = Http {
|
|
#[cfg(feature = "cookies")]
|
|
cookies_jar: std::sync::Arc::new(cookies_jar),
|
|
};
|
|
|
|
app.manage(state);
|
|
|
|
Ok(())
|
|
})
|
|
.on_event(|app, event| {
|
|
#[cfg(feature = "cookies")]
|
|
if let tauri::RunEvent::Exit = event {
|
|
let state = app.state::<Http>();
|
|
|
|
match state.cookies_jar.request_save() {
|
|
Ok(rx) => {
|
|
let _ = rx.recv();
|
|
}
|
|
Err(_e) => {
|
|
#[cfg(feature = "tracing")]
|
|
tracing::error!("failed to save cookie jar: {_e}");
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.invoke_handler(tauri::generate_handler![
|
|
commands::fetch,
|
|
commands::fetch_cancel,
|
|
commands::fetch_send,
|
|
commands::fetch_read_body
|
|
])
|
|
.build()
|
|
}
|