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-12 15:02:03 +02:00
|
|
|
use std::env;
|
2020-09-07 21:21:54 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
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};
|
2020-08-30 16:45:42 +02:00
|
|
|
use log::{info, warn};
|
2021-02-20 17:36:19 +01:00
|
|
|
use std::sync::{Arc, RwLock};
|
2020-08-27 01:07:25 +02:00
|
|
|
|
2020-09-07 21:21:54 +02:00
|
|
|
mod addressing;
|
2020-08-27 00:11:50 +02:00
|
|
|
mod database;
|
2020-09-07 21:21:54 +02:00
|
|
|
mod filesystem;
|
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-08-28 13:51:22 +02:00
|
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
fn main() -> Result<()> {
|
2020-08-27 00:11:50 +02:00
|
|
|
let env = env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info");
|
|
|
|
env_logger::init_from_env(env);
|
|
|
|
|
|
|
|
let app = ClapApp::new("upend")
|
|
|
|
.version(VERSION)
|
|
|
|
.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)
|
2021-06-19 12:32:05 +02:00
|
|
|
.help("path to sqlite db file (\"$VAULT_PATH/upend.sqlite\" by default)"),
|
|
|
|
)
|
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-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."),
|
|
|
|
)
|
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."),
|
2020-08-27 00:11:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let matches = app.get_matches();
|
|
|
|
|
|
|
|
info!("Starting UpEnd {}...", VERSION);
|
2020-08-30 22:11:32 +02:00
|
|
|
let sys = actix::System::new("upend");
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2021-07-26 21:14:12 +02:00
|
|
|
let job_container = Arc::new(RwLock::new(util::jobs::JobContainer::default()));
|
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-06-19 12:32:05 +02:00
|
|
|
let open_result = database::open_upend(
|
|
|
|
&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
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
let db_pool = open_result.pool;
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-08-30 16:45:42 +02:00
|
|
|
let bind: SocketAddr = matches
|
|
|
|
.value_of("BIND")
|
|
|
|
.unwrap()
|
|
|
|
.parse()
|
|
|
|
.expect("Incorrect bind format.");
|
2020-08-27 00:11:50 +02:00
|
|
|
info!("Starting server at: {}", &bind);
|
|
|
|
|
2020-08-30 22:11:32 +02:00
|
|
|
let state = routes::State {
|
2021-05-06 20:23:20 +02:00
|
|
|
vault_name: matches.value_of("VAULT_NAME").map(|s| s.to_string()),
|
2020-08-30 22:11:32 +02:00
|
|
|
directory: vault_path.clone(),
|
2020-09-15 19:26:47 +02:00
|
|
|
db_pool: db_pool.clone(),
|
2021-02-20 17:36:19 +01:00
|
|
|
job_container: job_container.clone(),
|
2020-08-30 22:11:32 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 00:11:50 +02:00
|
|
|
// Start HTTP server
|
2021-10-19 22:23:46 +02:00
|
|
|
let ui_enabled = !matches.is_present("NO_UI");
|
2020-08-27 00:11:50 +02:00
|
|
|
HttpServer::new(move || {
|
2021-10-19 22:23:46 +02:00
|
|
|
let app = App::new()
|
2020-08-30 22:11:32 +02:00
|
|
|
.data(state.clone())
|
2021-03-24 20:37:42 +01:00
|
|
|
.wrap(middleware::Logger::default().exclude("/api/jobs"))
|
2020-08-27 01:07:25 +02:00
|
|
|
.service(routes::get_raw)
|
2021-02-20 12:12:48 +01:00
|
|
|
.service(routes::get_query)
|
2020-09-13 13:20:35 +02:00
|
|
|
.service(routes::get_object)
|
2021-02-19 21:58:35 +01:00
|
|
|
.service(routes::put_object)
|
2021-02-20 12:12:48 +01:00
|
|
|
.service(routes::delete_object)
|
2020-08-27 01:07:25 +02:00
|
|
|
.service(routes::api_refresh)
|
2021-02-20 12:12:48 +01:00
|
|
|
.service(routes::list_hier)
|
2021-02-21 19:51:23 +01:00
|
|
|
.service(routes::latest_files)
|
2021-05-06 20:10:40 +02:00
|
|
|
.service(routes::get_file)
|
2021-02-20 17:36:19 +01:00
|
|
|
.service(routes::get_jobs)
|
2021-10-19 22:23:46 +02:00
|
|
|
.service(routes::get_info);
|
|
|
|
|
|
|
|
if ui_enabled {
|
|
|
|
app.service(
|
2020-08-30 23:13:09 +02:00
|
|
|
actix_files::Files::new(
|
|
|
|
"/",
|
|
|
|
env::current_exe().unwrap().parent().unwrap().join("webui"),
|
|
|
|
)
|
|
|
|
.index_file("index.html"),
|
|
|
|
)
|
2021-10-19 22:23:46 +02:00
|
|
|
} else {
|
|
|
|
app
|
|
|
|
}
|
2020-08-27 00:11:50 +02:00
|
|
|
})
|
|
|
|
.bind(&bind)?
|
|
|
|
.run();
|
|
|
|
|
2020-09-30 01:31:59 +02:00
|
|
|
if !matches.is_present("NO_INITIAL_UPDATE") {
|
|
|
|
info!("Running initial update...");
|
2021-03-06 22:14:17 +01:00
|
|
|
actix::spawn(filesystem::rescan_vault(db_pool, vault_path, job_container));
|
2020-08-30 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 22:23:46 +02:00
|
|
|
if !matches.is_present("NO_BROWSER") && ui_enabled {
|
2020-08-30 16:45:42 +02:00
|
|
|
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
|
2020-08-30 22:14:24 +02:00
|
|
|
if ui_result.is_err() {
|
2020-08-30 16:45:42 +02:00
|
|
|
warn!("Could not open UI in browser!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
Ok(sys.run()?)
|
2020-08-27 00:11:50 +02:00
|
|
|
}
|