2020-08-27 00:11:50 +02:00
|
|
|
#[macro_use]
|
2023-04-20 16:02:41 +02:00
|
|
|
extern crate upend;
|
2023-05-24 11:20:13 +02:00
|
|
|
use crate::common::{get_static_dir, REQWEST_CLIENT};
|
2022-09-08 21:41:18 +02:00
|
|
|
use actix_cors::Cors;
|
2023-05-22 19:29:08 +02:00
|
|
|
use actix_web::web::Data;
|
2020-08-27 01:07:25 +02:00
|
|
|
use actix_web::{middleware, App, HttpServer};
|
2020-09-15 19:26:47 +02:00
|
|
|
use anyhow::Result;
|
2023-04-25 19:27:31 +02:00
|
|
|
use clap::{Args, Parser, Subcommand, ValueEnum};
|
|
|
|
use filebuffer::FileBuffer;
|
2022-03-24 11:10:51 +01:00
|
|
|
use rand::{thread_rng, Rng};
|
2023-05-04 19:16:01 +02:00
|
|
|
use regex::Captures;
|
|
|
|
use regex::Regex;
|
2023-04-25 19:27:31 +02:00
|
|
|
use reqwest::Url;
|
|
|
|
use serde_json::json;
|
2023-05-04 19:32:08 +02:00
|
|
|
use std::collections::HashMap;
|
2023-04-25 19:27:31 +02:00
|
|
|
use std::net::SocketAddr;
|
2023-05-04 17:39:05 +02:00
|
|
|
use std::path::Path;
|
2023-04-25 19:27:31 +02:00
|
|
|
use std::path::PathBuf;
|
2022-03-02 01:14:23 +01:00
|
|
|
use std::sync::Arc;
|
2023-04-25 19:27:31 +02:00
|
|
|
use tracing::trace;
|
|
|
|
use tracing::{debug, error, info, warn};
|
2022-08-07 12:13:12 +02:00
|
|
|
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
|
2023-04-25 19:27:31 +02:00
|
|
|
use upend::addressing::Address;
|
|
|
|
use upend::database::entry::EntryValue;
|
|
|
|
use upend::util::hash::hash;
|
2020-08-27 01:07:25 +02:00
|
|
|
|
2023-04-20 16:02:41 +02:00
|
|
|
use upend::{
|
2023-05-24 11:20:13 +02:00
|
|
|
common::build,
|
2022-10-18 18:10:17 +02:00
|
|
|
config::UpEndConfig,
|
2022-08-19 14:04:18 +02:00
|
|
|
database::{
|
|
|
|
stores::{fs::FsStore, UpStore},
|
|
|
|
UpEndDatabase,
|
|
|
|
},
|
2023-04-20 16:02:41 +02:00
|
|
|
util::jobs::JobContainer,
|
2022-02-03 17:48:24 +01:00
|
|
|
};
|
2021-12-23 11:10:16 +01:00
|
|
|
|
2023-04-20 16:02:41 +02:00
|
|
|
use crate::util::exec::block_background;
|
|
|
|
|
2023-05-24 11:20:13 +02:00
|
|
|
mod common;
|
2020-08-27 01:07:25 +02:00
|
|
|
mod routes;
|
2020-09-14 21:18:53 +02:00
|
|
|
mod util;
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2023-04-20 16:02:41 +02:00
|
|
|
mod extractors;
|
|
|
|
mod previews;
|
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
#[command(name = "upend", author, version)]
|
|
|
|
struct Cli {
|
|
|
|
#[command(subcommand)]
|
|
|
|
command: Commands,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
enum Commands {
|
|
|
|
/// Perform a query against an UpEnd server instance.
|
|
|
|
Query {
|
|
|
|
/// URL of the UpEnd instance to query.
|
2023-04-25 19:33:57 +02:00
|
|
|
#[arg(short, long, default_value = "http://localhost:8093")]
|
2023-04-25 19:27:31 +02:00
|
|
|
url: Url,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// The query itself, in L-expression format; prefix a filepath by `@=` to insert its hash in its place.
|
2023-04-25 19:27:31 +02:00
|
|
|
query: String,
|
|
|
|
/// Output format
|
|
|
|
#[arg(short, long, default_value = "tsv")]
|
|
|
|
format: OutputFormat,
|
|
|
|
},
|
2023-05-04 19:47:45 +02:00
|
|
|
/// Insert an entry into an UpEnd server instance.
|
2023-04-25 19:27:31 +02:00
|
|
|
Insert {
|
|
|
|
/// URL of the UpEnd instance to query.
|
|
|
|
#[arg(short, long)]
|
|
|
|
url: Option<Url>,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// The address of the entity; prefix a filepath by `@` to insert its hash.
|
2023-04-25 19:27:31 +02:00
|
|
|
entity: String,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// The attribute of the entry.
|
2023-04-25 19:27:31 +02:00
|
|
|
attribute: String,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// The value; its type will be heurestically determined.
|
2023-05-04 18:54:17 +02:00
|
|
|
value: String,
|
2023-04-25 19:27:31 +02:00
|
|
|
/// Output format
|
|
|
|
#[arg(short, long, default_value = "tsv")]
|
|
|
|
format: OutputFormat,
|
|
|
|
},
|
2023-05-04 19:47:45 +02:00
|
|
|
/// Get the address of a file, attribute, or URL.
|
2023-04-25 19:27:31 +02:00
|
|
|
Address {
|
|
|
|
/// Type of input to be addressed
|
|
|
|
_type: AddressType,
|
|
|
|
/// Path to a file, hash...
|
|
|
|
input: String,
|
|
|
|
/// Output format
|
|
|
|
#[arg(short, long, default_value = "tsv")]
|
|
|
|
format: OutputFormat,
|
|
|
|
},
|
|
|
|
/// Start an UpEnd server instance.
|
|
|
|
Serve(ServeArgs),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)]
|
|
|
|
enum OutputFormat {
|
2023-05-04 19:47:45 +02:00
|
|
|
/// JSON
|
2023-04-25 19:27:31 +02:00
|
|
|
Json,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// Tab Separated Values
|
2023-04-25 19:27:31 +02:00
|
|
|
Tsv,
|
2023-05-04 19:47:45 +02:00
|
|
|
/// Raw, as received from the server
|
2023-04-25 19:27:31 +02:00
|
|
|
Raw,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)]
|
|
|
|
enum AddressType {
|
|
|
|
/// Hash a file and output its address.
|
|
|
|
File,
|
|
|
|
/// Compute an address from the output of `sha256sum`
|
|
|
|
Sha256sum,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Args)]
|
|
|
|
struct ServeArgs {
|
2023-04-24 23:00:21 +02:00
|
|
|
/// Directory to serve a vault from.
|
|
|
|
#[arg()]
|
|
|
|
directory: PathBuf,
|
|
|
|
|
|
|
|
/// Address and port to bind the Web interface on.
|
|
|
|
#[arg(long, default_value = "127.0.0.1:8093")]
|
|
|
|
bind: String,
|
|
|
|
|
|
|
|
/// Path to blob store ($VAULT_PATH by default).
|
|
|
|
#[arg(long)]
|
|
|
|
store_path: Option<PathBuf>,
|
|
|
|
|
|
|
|
/// Do not open a web browser with the UI.
|
|
|
|
#[arg(long)]
|
|
|
|
no_browser: bool,
|
|
|
|
|
|
|
|
/// Disable desktop features (web browser, native file opening).
|
|
|
|
#[arg(long)]
|
|
|
|
no_desktop: bool,
|
|
|
|
|
|
|
|
/// Trust the vault, and open local executable files.
|
|
|
|
#[arg(long)]
|
|
|
|
trust_executables: bool,
|
|
|
|
|
|
|
|
/// Do not serve the web UI.
|
|
|
|
#[arg(long)]
|
|
|
|
no_ui: bool,
|
|
|
|
|
|
|
|
/// Do not run a database update on start.
|
|
|
|
#[arg(long)]
|
|
|
|
no_initial_update: bool,
|
|
|
|
|
|
|
|
/// Clean up temporary files (e.g. previews) on start.
|
|
|
|
#[arg(long)]
|
|
|
|
clean: bool,
|
|
|
|
|
|
|
|
/// Delete and initialize database, if it exists already.
|
|
|
|
#[arg(long)]
|
|
|
|
reinitialize: bool,
|
|
|
|
|
|
|
|
/// Name of the vault.
|
|
|
|
#[arg(long)]
|
|
|
|
vault_name: Option<String>,
|
|
|
|
|
|
|
|
/// Secret to use for authentication.
|
|
|
|
#[arg(long, env = "UPEND_SECRET")]
|
|
|
|
secret: Option<String>,
|
|
|
|
|
|
|
|
/// Authentication key users must supply.
|
|
|
|
#[arg(long, env = "UPEND_KEY")]
|
|
|
|
key: Option<String>,
|
|
|
|
|
|
|
|
/// Allowed host/domain name the API can serve.
|
|
|
|
#[arg(long)]
|
|
|
|
allow_host: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2023-05-22 19:29:08 +02:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> Result<()> {
|
2023-04-25 19:27:31 +02:00
|
|
|
let args = Cli::parse();
|
2023-04-24 23:00:21 +02:00
|
|
|
|
2022-08-07 12:13:12 +02:00
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(
|
|
|
|
EnvFilter::builder()
|
|
|
|
.with_default_directive(LevelFilter::INFO.into())
|
|
|
|
.from_env_lossy(),
|
|
|
|
)
|
|
|
|
.init();
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
match args.command {
|
|
|
|
Commands::Query { url, query, format } => {
|
2023-05-04 19:16:01 +02:00
|
|
|
let re = Regex::new(r#"@(="([^"]+)"|=([^ ]+))"#).unwrap();
|
|
|
|
|
|
|
|
let query = re
|
|
|
|
.replace_all(&query, |caps: &Captures| {
|
|
|
|
if let Some(filepath_match) = caps.get(2).or_else(|| caps.get(3)) {
|
|
|
|
let address = hash_path(filepath_match.as_str()).unwrap();
|
|
|
|
format!("@{}", address)
|
|
|
|
} else {
|
|
|
|
panic!("Error preprocessing query. Captures: {:?}", caps)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
debug!("Final query: {}", query);
|
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let api_url = url.join("/api/query")?;
|
|
|
|
|
|
|
|
debug!("Querying \"{}\"", api_url);
|
2023-05-24 11:20:13 +02:00
|
|
|
let response = REQWEST_CLIENT.post(api_url).body(query).send()?;
|
2023-04-25 19:27:31 +02:00
|
|
|
|
2023-05-04 19:32:08 +02:00
|
|
|
response.error_for_status_ref()?;
|
|
|
|
|
|
|
|
match format {
|
|
|
|
OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text()?),
|
|
|
|
OutputFormat::Tsv => {
|
2023-05-19 22:46:25 +02:00
|
|
|
eprintln!("entity\tattribute\tvalue\ttimestamp\tprovenance");
|
2023-05-04 19:32:08 +02:00
|
|
|
response
|
|
|
|
.json::<HashMap<String, serde_json::Value>>()?
|
|
|
|
.iter()
|
|
|
|
.for_each(|(_, entry)| {
|
|
|
|
println!(
|
|
|
|
"{}\t{}\t{}\t{}\t{}",
|
|
|
|
entry.get("entity").and_then(|e| e.as_str()).unwrap(),
|
|
|
|
entry.get("attribute").and_then(|a| a.as_str()).unwrap(),
|
2023-05-19 22:46:25 +02:00
|
|
|
entry.get("value").and_then(|v| v.get("c")).unwrap(),
|
2023-05-04 19:32:08 +02:00
|
|
|
entry.get("timestamp").and_then(|t| t.as_str()).unwrap(),
|
|
|
|
entry.get("provenance").and_then(|p| p.as_str()).unwrap(),
|
|
|
|
)
|
|
|
|
})
|
2023-04-25 19:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-04 19:32:08 +02:00
|
|
|
|
|
|
|
Ok(())
|
2023-04-25 19:27:31 +02:00
|
|
|
}
|
|
|
|
Commands::Insert {
|
|
|
|
url,
|
|
|
|
entity,
|
|
|
|
attribute,
|
|
|
|
value,
|
|
|
|
format: _,
|
|
|
|
} => {
|
|
|
|
let url = url.unwrap_or("http://localhost:8093".parse().unwrap());
|
|
|
|
let api_url = url.join("/api/obj")?;
|
|
|
|
|
2023-05-04 17:39:05 +02:00
|
|
|
let entity = match entity {
|
2023-05-04 18:48:00 +02:00
|
|
|
entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(),
|
2023-05-19 17:30:09 +02:00
|
|
|
entity if entity.starts_with("http") => Address::Url(entity.parse()?).to_string(),
|
2023-05-04 17:39:05 +02:00
|
|
|
_ => entity,
|
|
|
|
};
|
|
|
|
|
2023-05-04 18:54:17 +02:00
|
|
|
let value = EntryValue::guess_from(value);
|
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let body = json!({
|
|
|
|
"entity": entity,
|
|
|
|
"attribute": attribute,
|
|
|
|
"value": value
|
|
|
|
});
|
2021-02-20 17:36:19 +01:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
debug!("Inserting {:?} at \"{}\"", body, api_url);
|
2023-05-24 11:20:13 +02:00
|
|
|
let response = REQWEST_CLIENT.put(api_url).json(&body).send()?;
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
match response.error_for_status_ref() {
|
|
|
|
Ok(_) => {
|
|
|
|
let data: Vec<String> = response.json()?;
|
|
|
|
Ok(println!("{}", data[0]))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("{}", response.text()?);
|
2023-04-25 19:33:57 +02:00
|
|
|
Err(err.into())
|
2023-04-25 19:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Commands::Address {
|
|
|
|
_type,
|
|
|
|
input,
|
|
|
|
format,
|
|
|
|
} => {
|
|
|
|
let address = match _type {
|
2023-05-04 18:48:00 +02:00
|
|
|
AddressType::File => hash_path(&input)?,
|
2023-04-25 19:27:31 +02:00
|
|
|
AddressType::Sha256sum => {
|
|
|
|
let digest = multibase::Base::Base16Lower.decode(input)?;
|
|
|
|
Address::Hash(upend::util::hash::Hash(digest))
|
|
|
|
}
|
|
|
|
};
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
match format {
|
|
|
|
OutputFormat::Json => Ok(println!("\"{}\"", address)),
|
|
|
|
OutputFormat::Tsv | OutputFormat::Raw => Ok(println!("{}", address)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Commands::Serve(args) => {
|
|
|
|
info!("Starting UpEnd {}...", build::PKG_VERSION);
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let job_container = JobContainer::new();
|
2023-04-24 23:00:21 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let vault_path = args.directory;
|
2022-02-03 16:26:57 +01:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let open_result = UpEndDatabase::open(&vault_path, args.reinitialize)
|
|
|
|
.expect("failed to open database!");
|
|
|
|
|
|
|
|
let upend = Arc::new(open_result.db);
|
|
|
|
let store = Arc::new(Box::new(
|
|
|
|
FsStore::from_path(args.store_path.unwrap_or_else(|| vault_path.clone())).unwrap(),
|
|
|
|
) as Box<dyn UpStore + Send + Sync>);
|
|
|
|
|
|
|
|
let ui_path = get_static_dir("webui");
|
|
|
|
if ui_path.is_err() {
|
|
|
|
warn!(
|
|
|
|
"Couldn't locate Web UI directory ({:?}), disabling...",
|
|
|
|
ui_path
|
|
|
|
);
|
2021-12-27 11:58:01 +01:00
|
|
|
}
|
2022-10-22 12:50:52 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
let ui_enabled = ui_path.is_ok() && !args.no_ui;
|
|
|
|
let browser_enabled = !args.no_desktop && ui_enabled && !args.no_browser;
|
|
|
|
|
|
|
|
let preview_path = upend.path.join("previews");
|
|
|
|
#[cfg(feature = "previews")]
|
|
|
|
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
|
|
|
|
preview_path.clone(),
|
|
|
|
store.clone(),
|
|
|
|
)));
|
|
|
|
#[cfg(feature = "previews")]
|
|
|
|
let preview_pool = Some(Arc::new(
|
|
|
|
rayon::ThreadPoolBuilder::new()
|
|
|
|
.num_threads(num_cpus::get() / 2)
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
));
|
|
|
|
|
|
|
|
if args.clean {
|
|
|
|
info!("Cleaning temporary directories...");
|
|
|
|
if preview_path.exists() {
|
|
|
|
std::fs::remove_dir_all(&preview_path).unwrap();
|
|
|
|
debug!("Removed {preview_path:?}");
|
|
|
|
} else {
|
|
|
|
debug!("No preview path exists, continuing...");
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 11:58:01 +01:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
#[cfg(not(feature = "previews"))]
|
|
|
|
let preview_store = None;
|
|
|
|
#[cfg(not(feature = "previews"))]
|
|
|
|
let preview_pool = None;
|
|
|
|
|
|
|
|
let mut bind: SocketAddr = args.bind.parse().expect("Incorrect bind format.");
|
|
|
|
|
|
|
|
let secret = args.secret.unwrap_or_else(|| {
|
|
|
|
warn!("No secret supplied, generating one at random.");
|
|
|
|
|
|
|
|
thread_rng()
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
|
|
.take(32)
|
|
|
|
.map(char::from)
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
let state = routes::State {
|
|
|
|
upend: upend.clone(),
|
|
|
|
store,
|
|
|
|
job_container: job_container.clone(),
|
|
|
|
preview_store,
|
|
|
|
preview_pool,
|
|
|
|
config: UpEndConfig {
|
|
|
|
vault_name: Some(args.vault_name.unwrap_or_else(|| {
|
|
|
|
vault_path
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.into_owned()
|
|
|
|
})),
|
|
|
|
desktop_enabled: !args.no_desktop,
|
|
|
|
trust_executables: args.trust_executables,
|
|
|
|
key: args.key,
|
|
|
|
secret,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start HTTP server
|
|
|
|
|
|
|
|
let mut cnt = 0;
|
|
|
|
let ui_path = ui_path.ok();
|
|
|
|
let server = loop {
|
|
|
|
let state = state.clone();
|
|
|
|
let ui_path = ui_path.clone();
|
|
|
|
let allowed_origins = args.allow_host.clone();
|
|
|
|
|
|
|
|
let server = HttpServer::new(move || {
|
|
|
|
let allowed_origins = allowed_origins.clone();
|
|
|
|
|
|
|
|
let cors = Cors::default()
|
|
|
|
.allowed_origin("http://localhost")
|
2023-05-03 16:06:29 +02:00
|
|
|
.allowed_origin("http://127.0.0.1")
|
2023-04-25 19:27:31 +02:00
|
|
|
.allowed_origin_fn(|origin, _req_head| {
|
|
|
|
origin.as_bytes().starts_with(b"http://localhost:")
|
2023-05-03 16:06:29 +02:00
|
|
|
|| origin.as_bytes().starts_with(b"http://127.0.0.1:")
|
2023-05-20 18:17:13 +02:00
|
|
|
|| origin.as_bytes().starts_with(b"moz-extension://")
|
2023-04-25 19:27:31 +02:00
|
|
|
})
|
|
|
|
.allowed_origin_fn(move |origin, _req_head| {
|
|
|
|
allowed_origins.iter().any(|allowed_origin| {
|
|
|
|
*allowed_origin == "*" || origin == allowed_origin
|
|
|
|
})
|
|
|
|
})
|
2023-05-27 16:27:05 +02:00
|
|
|
.allowed_header("content-type")
|
2023-04-25 19:27:31 +02:00
|
|
|
.allow_any_method();
|
|
|
|
|
|
|
|
let app = App::new()
|
|
|
|
.wrap(cors)
|
|
|
|
.app_data(actix_web::web::PayloadConfig::new(4_294_967_296))
|
2023-05-22 19:29:08 +02:00
|
|
|
.app_data(Data::new(state.clone()))
|
2023-04-25 19:27:31 +02:00
|
|
|
.wrap(middleware::Logger::default().exclude("/api/jobs"))
|
|
|
|
.service(routes::login)
|
|
|
|
.service(routes::get_raw)
|
|
|
|
.service(routes::get_thumbnail)
|
|
|
|
.service(routes::get_query)
|
|
|
|
.service(routes::get_object)
|
|
|
|
.service(routes::put_object)
|
|
|
|
.service(routes::put_blob)
|
|
|
|
.service(routes::put_object_attribute)
|
|
|
|
.service(routes::delete_object)
|
|
|
|
.service(routes::get_address)
|
|
|
|
.service(routes::get_all_attributes)
|
|
|
|
.service(routes::api_refresh)
|
|
|
|
.service(routes::list_hier)
|
|
|
|
.service(routes::list_hier_roots)
|
|
|
|
.service(routes::store_info)
|
|
|
|
.service(routes::get_jobs)
|
|
|
|
.service(routes::get_info);
|
|
|
|
|
|
|
|
if ui_enabled {
|
|
|
|
if let Some(ui_path) = &ui_path {
|
|
|
|
return app.service(
|
|
|
|
actix_files::Files::new("/", ui_path).index_file("index.html"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 18:22:24 +02:00
|
|
|
#[actix_web::get("/")]
|
|
|
|
async fn unavailable_index() -> actix_web::HttpResponse {
|
|
|
|
actix_web::HttpResponse::ServiceUnavailable().body("Web UI not enabled.")
|
|
|
|
}
|
|
|
|
|
2023-05-21 21:48:21 +02:00
|
|
|
app.service(unavailable_index)
|
2023-04-25 19:27:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let bind_result = server.bind(&bind);
|
|
|
|
if let Ok(server) = bind_result {
|
|
|
|
break server;
|
|
|
|
} else {
|
|
|
|
warn!("Failed to bind at {:?}, trying next port number...", bind);
|
|
|
|
bind.set_port(bind.port() + 1);
|
|
|
|
}
|
2021-12-27 11:58:01 +01:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
if cnt > 32 {
|
|
|
|
panic!("Couldn't start server.")
|
|
|
|
} else {
|
|
|
|
cnt += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if !args.no_initial_update {
|
|
|
|
info!("Running initial update...");
|
|
|
|
let initial = open_result.new;
|
|
|
|
block_background::<_, _, anyhow::Error>(move || {
|
|
|
|
let _ = state.store.update(&upend, job_container.clone(), initial);
|
|
|
|
let _ = extractors::extract_all(upend, state.store, job_container);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
2020-08-30 22:11:32 +02:00
|
|
|
|
2023-04-25 19:27:31 +02:00
|
|
|
#[cfg(feature = "desktop")]
|
|
|
|
{
|
|
|
|
if browser_enabled && ui_enabled {
|
|
|
|
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
|
|
|
|
if ui_result.is_err() {
|
|
|
|
warn!("Could not open UI in browser!");
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 20:09:44 +01:00
|
|
|
}
|
2023-04-25 19:27:31 +02:00
|
|
|
|
2023-05-22 19:29:08 +02:00
|
|
|
info!("Starting server at: {}", &bind);
|
|
|
|
server.run().await?;
|
|
|
|
Ok(())
|
2020-08-30 16:45:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 00:11:50 +02:00
|
|
|
}
|
2023-05-04 18:48:00 +02:00
|
|
|
|
|
|
|
fn hash_path<P: AsRef<Path>>(filepath: P) -> Result<Address> {
|
|
|
|
let filepath = filepath.as_ref();
|
|
|
|
debug!("Hashing {:?}...", filepath);
|
|
|
|
let fbuffer = FileBuffer::open(filepath)?;
|
|
|
|
let digest = hash(&fbuffer);
|
|
|
|
trace!("Finished hashing {:?}...", filepath);
|
|
|
|
Ok(Address::Hash(digest))
|
|
|
|
}
|