upend/src/main.rs

207 lines
6.1 KiB
Rust
Raw Normal View History

2020-08-27 00:11:50 +02:00
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[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};
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};
use std::sync::{Arc, RwLock};
2020-08-27 01:07:25 +02:00
use crate::database::UpEndDatabase;
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;
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-08-28 13:51:22 +02:00
const VERSION: &str = env!("CARGO_PKG_VERSION");
2020-08-27 00:11:50 +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-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."),
)
.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."),
)
.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);
let sys = actix::System::new("upend");
2020-08-27 00:11:50 +02:00
let job_container = Arc::new(RwLock::new(util::jobs::JobContainer::default()));
let vault_path = PathBuf::from(matches.value_of("DIRECTORY").unwrap());
2020-08-27 00:11:50 +02: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
let upend = Arc::new(open_result.db);
2020-08-27 00:11:50 +02:00
2021-12-27 12:40:02 +01:00
#[cfg(feature = "previews")]
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
upend.db_path.join("previews"),
upend.clone(),
)));
2021-12-27 12:40:02 +01:00
#[cfg(not(feature = "previews"))]
let preview_store = None;
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
let state = routes::State {
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()
}),
),
job_container: job_container.clone(),
2021-12-27 12:40:02 +01:00
preview_store,
};
2020-08-27 00:11:50 +02:00
// Start HTTP server
let ui_path = env::current_exe().unwrap().parent().unwrap().join("webui");
if !ui_path.exists() {
warn!("No Web UI directory present ({:?}), disabling...", ui_path);
}
let ui_enabled = ui_path.exists() && !matches.is_present("NO_UI");
2021-12-27 11:58:01 +01:00
let mut cnt = 0;
let server = loop {
let state = state.clone();
let ui_path = ui_path.clone();
let server = HttpServer::new(move || {
let app = App::new()
.data(state.clone())
.wrap(middleware::Logger::default().exclude("/api/jobs"))
.service(routes::get_raw)
.service(routes::get_thumbnail)
.service(routes::get_query)
.service(routes::get_object)
.service(routes::put_object)
.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)
.service(routes::latest_files)
.service(routes::get_file)
.service(routes::get_jobs)
.service(routes::get_info);
if ui_enabled {
app.service(actix_files::Files::new("/", &ui_path).index_file("index.html"))
} 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
if !matches.is_present("NO_INITIAL_UPDATE") {
info!("Running initial update...");
actix::spawn(filesystem::rescan_vault(upend, job_container));
}
2021-12-19 20:09:44 +01:00
#[cfg(feature = "desktop")]
{
if !matches.is_present("NO_BROWSER") && ui_enabled {
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
}
}
Ok(sys.run()?)
2020-08-27 00:11:50 +02:00
}