2020-08-27 00:11:50 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
2021-03-15 22:32:04 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-07 21:21:54 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-09-08 21:41:18 +02:00
|
|
|
use actix_cors::Cors;
|
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;
|
2020-08-27 00:11:50 +02:00
|
|
|
use clap::{App as ClapApp, Arg};
|
2022-02-03 16:26:57 +01:00
|
|
|
use log::{debug, info, warn};
|
2022-03-24 11:10:51 +01:00
|
|
|
use rand::{thread_rng, Rng};
|
2022-03-02 01:14:23 +01:00
|
|
|
use std::sync::Arc;
|
2022-08-07 12:13:12 +02:00
|
|
|
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
|
2020-08-27 01:07:25 +02:00
|
|
|
|
2022-02-03 17:48:24 +01:00
|
|
|
use crate::{
|
|
|
|
common::{get_static_dir, PKG_VERSION},
|
2022-08-19 14:04:18 +02:00
|
|
|
database::{
|
|
|
|
stores::{fs::FsStore, UpStore},
|
|
|
|
UpEndDatabase,
|
|
|
|
},
|
2022-03-02 01:14:23 +01:00
|
|
|
util::{exec::block_background, jobs::JobContainer},
|
2022-02-03 17:48:24 +01:00
|
|
|
};
|
2021-12-23 11:10:16 +01:00
|
|
|
|
2020-09-07 21:21:54 +02:00
|
|
|
mod addressing;
|
2022-01-31 15:17:59 +01:00
|
|
|
mod common;
|
2020-08-27 00:11:50 +02:00
|
|
|
mod database;
|
2022-02-10 11:38:45 +01:00
|
|
|
mod extractors;
|
2022-01-07 00:52:09 +01:00
|
|
|
mod previews;
|
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
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
fn main() -> Result<()> {
|
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
|
|
|
|
|
|
|
let app = ClapApp::new("upend")
|
2022-01-31 15:17:59 +01:00
|
|
|
.version(PKG_VERSION)
|
2020-08-27 00:11:50 +02:00
|
|
|
.author("Tomáš Mládek <t@mldk.cz>")
|
|
|
|
.arg(Arg::with_name("DIRECTORY").required(true).index(1))
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("BIND")
|
|
|
|
.long("bind")
|
|
|
|
.default_value("127.0.0.1:8093")
|
|
|
|
.help("address and port to bind the Web interface on")
|
|
|
|
.required(true),
|
2020-08-30 16:45:42 +02:00
|
|
|
)
|
2021-06-19 12:32:05 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("DB_PATH")
|
|
|
|
.long("db-path")
|
2021-06-19 13:11:27 +02:00
|
|
|
.takes_value(true)
|
2022-03-28 20:06:00 +02:00
|
|
|
.help(r#"path to sqlite db file ("$VAULT_PATH/.upend" by default)"#),
|
2021-06-19 12:32:05 +02:00
|
|
|
)
|
2020-08-30 16:45:42 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("NO_BROWSER")
|
|
|
|
.long("no-browser")
|
|
|
|
.help("Do not open web browser with the UI."),
|
2020-09-12 15:02:03 +02:00
|
|
|
)
|
2021-12-19 22:38:41 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("NO_DESKTOP")
|
|
|
|
.long("no-desktop")
|
|
|
|
.help("Disable desktop features (webbrowser, native file opening)"),
|
|
|
|
)
|
2021-10-19 22:23:46 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("NO_UI")
|
|
|
|
.long("no-ui")
|
|
|
|
.help("Do not serve the web UI."),
|
|
|
|
)
|
2020-09-30 01:31:59 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("NO_INITIAL_UPDATE")
|
|
|
|
.long("no-initial-update")
|
|
|
|
.help("Don't run a database update on start."),
|
|
|
|
)
|
2022-02-03 16:26:57 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("CLEAN")
|
|
|
|
.long("clean")
|
|
|
|
.help("Clean up temporary files (e.g. previews) on start."),
|
|
|
|
)
|
2020-09-12 15:02:03 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("REINITIALIZE")
|
|
|
|
.long("reinitialize")
|
|
|
|
.help("Delete and initialize database, if it exists already."),
|
2021-05-06 20:23:20 +02:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("VAULT_NAME")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("name")
|
|
|
|
.help("Name of the vault."),
|
2022-03-24 11:10:51 +01:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("SECRET")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("secret")
|
|
|
|
.env("UPEND_SECRET")
|
|
|
|
.help("Secret to use for authentication."),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("KEY")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("key")
|
|
|
|
.env("UPEND_KEY")
|
|
|
|
.help("Authentication key users must supply."),
|
2020-08-27 00:11:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let matches = app.get_matches();
|
|
|
|
|
2022-01-31 15:17:59 +01:00
|
|
|
info!("Starting UpEnd {}...", PKG_VERSION);
|
2021-12-10 14:57:46 +01:00
|
|
|
let sys = actix::System::new("upend");
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2022-03-02 01:14:23 +01:00
|
|
|
let job_container = JobContainer::new();
|
2021-02-20 17:36:19 +01:00
|
|
|
|
2020-08-30 22:11:32 +02:00
|
|
|
let vault_path = PathBuf::from(matches.value_of("DIRECTORY").unwrap());
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let open_result = UpEndDatabase::open(
|
2021-06-19 12:32:05 +02:00
|
|
|
&vault_path,
|
|
|
|
matches.value_of("DB_PATH").map(PathBuf::from),
|
|
|
|
matches.is_present("REINITIALIZE"),
|
|
|
|
)
|
|
|
|
.expect("failed to open database!");
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let upend = Arc::new(open_result.db);
|
2022-08-19 14:04:18 +02:00
|
|
|
let store = Arc::new(FsStore::from_path(vault_path.clone()).unwrap());
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2022-02-03 17:48:24 +01:00
|
|
|
let ui_path = get_static_dir("webui");
|
|
|
|
if ui_path.is_err() {
|
|
|
|
warn!(
|
|
|
|
"Couldn't locate Web UI directory ({:?}), disabling...",
|
|
|
|
ui_path
|
|
|
|
);
|
2022-01-19 20:42:36 +01:00
|
|
|
}
|
|
|
|
let desktop_enabled = !matches.is_present("NO_DESKTOP");
|
2022-02-03 17:48:24 +01:00
|
|
|
let ui_enabled = ui_path.is_ok() && !matches.is_present("NO_UI");
|
2022-01-19 20:42:36 +01:00
|
|
|
let browser_enabled = desktop_enabled && !matches.is_present("NO_BROWSER");
|
|
|
|
|
2022-08-19 14:04:18 +02:00
|
|
|
let preview_dir = tempfile::tempdir().unwrap();
|
2021-12-27 12:40:02 +01:00
|
|
|
#[cfg(feature = "previews")]
|
|
|
|
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
|
2022-08-19 14:04:18 +02:00
|
|
|
preview_dir.path(),
|
|
|
|
store.clone(),
|
2021-12-23 23:45:46 +01:00
|
|
|
)));
|
|
|
|
|
2022-08-19 14:04:18 +02:00
|
|
|
// if matches.is_present("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...");
|
|
|
|
// }
|
|
|
|
// }
|
2022-02-03 16:26:57 +01:00
|
|
|
|
2021-12-27 12:40:02 +01:00
|
|
|
#[cfg(not(feature = "previews"))]
|
|
|
|
let preview_store = None;
|
2021-12-23 23:45:46 +01:00
|
|
|
|
2021-12-27 11:58:01 +01:00
|
|
|
let mut bind: SocketAddr = matches
|
2020-08-30 16:45:42 +02:00
|
|
|
.value_of("BIND")
|
|
|
|
.unwrap()
|
|
|
|
.parse()
|
|
|
|
.expect("Incorrect bind format.");
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2022-03-24 11:10:51 +01:00
|
|
|
let secret = matches
|
|
|
|
.value_of("SECRET")
|
|
|
|
.map(String::from)
|
|
|
|
.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 key = matches.value_of("KEY").map(String::from);
|
|
|
|
|
2020-08-30 22:11:32 +02:00
|
|
|
let state = routes::State {
|
2021-12-23 11:10:16 +01:00
|
|
|
upend: upend.clone(),
|
2021-12-19 18:53:10 +01:00
|
|
|
vault_name: Some(
|
|
|
|
matches
|
|
|
|
.value_of("VAULT_NAME")
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
vault_path
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.into_owned()
|
|
|
|
}),
|
|
|
|
),
|
2022-08-19 14:04:18 +02:00
|
|
|
store,
|
2021-02-20 17:36:19 +01:00
|
|
|
job_container: job_container.clone(),
|
2021-12-27 12:40:02 +01:00
|
|
|
preview_store,
|
2022-01-19 20:42:36 +01:00
|
|
|
desktop_enabled,
|
2022-03-24 11:10:51 +01:00
|
|
|
secret,
|
|
|
|
key,
|
2020-08-30 22:11:32 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 00:11:50 +02:00
|
|
|
// Start HTTP server
|
2021-12-27 11:58:01 +01:00
|
|
|
|
|
|
|
let mut cnt = 0;
|
2022-02-03 17:48:24 +01:00
|
|
|
let ui_path = ui_path.ok();
|
2021-12-27 11:58:01 +01:00
|
|
|
let server = loop {
|
|
|
|
let state = state.clone();
|
|
|
|
let ui_path = ui_path.clone();
|
|
|
|
|
|
|
|
let server = HttpServer::new(move || {
|
2022-09-08 21:41:18 +02:00
|
|
|
let cors = Cors::default()
|
|
|
|
.allowed_origin("http://localhost")
|
|
|
|
.allowed_origin_fn(|origin, _req_head| {
|
|
|
|
origin.as_bytes().starts_with(b"http://localhost:")
|
|
|
|
})
|
|
|
|
.allow_any_method();
|
|
|
|
|
2021-12-27 11:58:01 +01:00
|
|
|
let app = App::new()
|
2022-09-08 21:41:18 +02:00
|
|
|
.wrap(cors)
|
2022-01-18 16:59:46 +01:00
|
|
|
.app_data(actix_web::web::PayloadConfig::new(4_294_967_296))
|
2021-12-27 11:58:01 +01:00
|
|
|
.data(state.clone())
|
|
|
|
.wrap(middleware::Logger::default().exclude("/api/jobs"))
|
2022-03-24 11:10:51 +01:00
|
|
|
.service(routes::login)
|
2021-12-27 11:58:01 +01:00
|
|
|
.service(routes::get_raw)
|
|
|
|
.service(routes::get_thumbnail)
|
|
|
|
.service(routes::get_query)
|
|
|
|
.service(routes::get_object)
|
|
|
|
.service(routes::put_object)
|
2022-02-02 00:45:05 +01:00
|
|
|
.service(routes::put_object_attribute)
|
2021-12-27 11:58:01 +01:00
|
|
|
.service(routes::delete_object)
|
2022-01-04 21:58:23 +01:00
|
|
|
.service(routes::get_all_attributes)
|
2021-12-27 11:58:01 +01:00
|
|
|
.service(routes::api_refresh)
|
|
|
|
.service(routes::list_hier)
|
|
|
|
.service(routes::list_hier_roots)
|
2022-02-27 15:03:15 +01:00
|
|
|
.service(routes::store_info)
|
2021-12-27 11:58:01 +01:00
|
|
|
.service(routes::get_jobs)
|
|
|
|
.service(routes::get_info);
|
|
|
|
|
2022-02-03 17:48:24 +01:00
|
|
|
if let Some(ui_path) = &ui_path {
|
|
|
|
app.service(actix_files::Files::new("/", ui_path).index_file("index.html"))
|
2021-12-27 11:58:01 +01:00
|
|
|
} else {
|
|
|
|
app
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt > 10 {
|
|
|
|
panic!("Couldn't start server.")
|
2021-10-19 22:23:46 +02:00
|
|
|
} else {
|
2021-12-27 11:58:01 +01:00
|
|
|
cnt += 1;
|
2021-10-19 22:23:46 +02:00
|
|
|
}
|
2021-12-27 11:58:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
info!("Starting server at: {}", &bind);
|
|
|
|
server.run();
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-30 01:31:59 +02:00
|
|
|
if !matches.is_present("NO_INITIAL_UPDATE") {
|
|
|
|
info!("Running initial update...");
|
2022-08-19 14:04:18 +02:00
|
|
|
// let new = open_result.new;
|
2022-03-02 01:14:46 +01:00
|
|
|
block_background::<_, _, anyhow::Error>(move || {
|
2022-08-19 14:04:18 +02:00
|
|
|
state.store.update(upend.clone(), job_container.clone());
|
|
|
|
let _ = extractors::extract_all(upend, state.store, job_container);
|
2022-03-02 01:14:46 +01:00
|
|
|
Ok(())
|
|
|
|
})
|
2020-08-30 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 20:09:44 +01:00
|
|
|
#[cfg(feature = "desktop")]
|
|
|
|
{
|
2022-01-19 20:42:36 +01:00
|
|
|
if browser_enabled && ui_enabled {
|
2021-12-19 20:09:44 +01:00
|
|
|
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
|
|
|
|
if ui_result.is_err() {
|
|
|
|
warn!("Could not open UI in browser!");
|
|
|
|
}
|
2020-08-30 16:45:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
Ok(sys.run()?)
|
2020-08-27 00:11:50 +02:00
|
|
|
}
|