2020-08-27 00:11:50 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
2020-08-27 01:07:25 +02:00
|
|
|
use actix_web::{middleware, App, HttpServer};
|
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};
|
2020-08-27 00:11:50 +02:00
|
|
|
use std::env;
|
2020-08-27 01:07:25 +02:00
|
|
|
use std::fs;
|
2020-08-30 16:45:42 +02:00
|
|
|
use std::net::SocketAddr;
|
2020-08-27 01:07:25 +02:00
|
|
|
use std::path::PathBuf;
|
2020-08-30 16:45:42 +02:00
|
|
|
use webbrowser;
|
2020-08-27 01:07:25 +02:00
|
|
|
|
2020-08-27 00:11:50 +02:00
|
|
|
mod database;
|
|
|
|
mod dataops;
|
|
|
|
mod models;
|
2020-08-27 01:07:25 +02:00
|
|
|
mod routes;
|
2020-08-27 00:11:50 +02:00
|
|
|
mod schema;
|
|
|
|
|
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() -> std::io::Result<()> {
|
|
|
|
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
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("NO_BROWSER")
|
|
|
|
.long("no-browser")
|
|
|
|
.help("Do not open web browser with the UI."),
|
2020-08-27 00:11:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let matches = app.get_matches();
|
|
|
|
|
|
|
|
info!("Starting UpEnd {}...", VERSION);
|
|
|
|
let dirname = matches.value_of("DIRECTORY").unwrap();
|
|
|
|
let path = PathBuf::from(dirname);
|
|
|
|
|
|
|
|
let _ = fs::remove_file(format!("{}/upend.sqlite3", dirname)); // TODO REMOVE!!!
|
|
|
|
let db_pool = database::open_upend(&dirname).expect("failed to open database!");
|
|
|
|
|
|
|
|
let sys = actix::System::new("upend");
|
|
|
|
|
|
|
|
// let connection = db_pool.get().expect("Could not get SQL connection.");
|
|
|
|
let db_addr = SyncArbiter::start(3, move || database::DbExecutor(db_pool.clone()));
|
|
|
|
let hash_addr = SyncArbiter::start(4, move || dataops::HasherWorker);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Start HTTP server
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2020-08-27 01:07:25 +02:00
|
|
|
.data(routes::State {
|
2020-08-27 00:11:50 +02:00
|
|
|
directory: path.clone(),
|
|
|
|
db: db_addr.clone(),
|
|
|
|
hasher: hash_addr.clone(),
|
|
|
|
})
|
|
|
|
.wrap(middleware::Logger::default())
|
2020-08-27 01:07:25 +02:00
|
|
|
.service(routes::get_raw)
|
2020-08-27 01:29:44 +02:00
|
|
|
.service(routes::get_lookup)
|
2020-08-27 01:07:25 +02:00
|
|
|
.service(routes::api_refresh)
|
2020-08-27 00:11:50 +02:00
|
|
|
})
|
|
|
|
.bind(&bind)?
|
|
|
|
.run();
|
|
|
|
|
2020-08-30 16:45:42 +02:00
|
|
|
// TODO REMOVE
|
|
|
|
if !matches.is_present("NO_BROWSER") && false {
|
|
|
|
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
|
|
|
|
if !ui_result.is_ok() {
|
|
|
|
warn!("Could not open UI in browser!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:11:50 +02:00
|
|
|
sys.run()
|
|
|
|
}
|