fix "new" db detection, add initial update
parent
ec0f4f5ec6
commit
6b32235896
|
@ -152,7 +152,7 @@ pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<OpenResult> {
|
|||
embed_migrations!("./migrations/upend/");
|
||||
|
||||
let database_path: PathBuf = dirpath.as_ref().join("upend.sqlite3");
|
||||
let new = database_path.exists();
|
||||
let new = !database_path.exists();
|
||||
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(database_path.to_str().unwrap());
|
||||
let pool = r2d2::Pool::builder()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::models::NewFile;
|
||||
use anyhow::Result;
|
||||
use filebuffer::FileBuffer;
|
||||
use log::info;
|
||||
use log::{info, warn};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
@ -79,6 +79,17 @@ pub async fn update_directory<T: AsRef<Path>>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_directory_bg(
|
||||
directory: PathBuf,
|
||||
db_executor: Addr<crate::database::DbExecutor>,
|
||||
hasher_worker: Addr<HasherWorker>,
|
||||
) {
|
||||
let result = update_directory(directory, &db_executor, &hasher_worker).await;
|
||||
if !result.is_ok() {
|
||||
warn!("Update did not succeed!");
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HasherWorker;
|
||||
|
||||
impl Actor for HasherWorker {
|
||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -46,17 +46,16 @@ fn main() -> std::io::Result<()> {
|
|||
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 open_result = database::open_upend(&dirname).expect("failed to open database!");
|
||||
|
||||
let sys = actix::System::new("upend");
|
||||
|
||||
let vault_path = PathBuf::from(matches.value_of("DIRECTORY").unwrap());
|
||||
|
||||
let _ = fs::remove_file(&vault_path.join("upend.sqlite3")); // TODO REMOVE!!!
|
||||
let open_result = database::open_upend(&vault_path).expect("failed to open database!");
|
||||
|
||||
let pool = open_result.pool;
|
||||
let db_addr = SyncArbiter::start(3, move || database::DbExecutor(pool.clone()));
|
||||
let hash_addr = SyncArbiter::start(4, move || dataops::HasherWorker);
|
||||
let hash_addr = SyncArbiter::start(4, || dataops::HasherWorker);
|
||||
|
||||
let bind: SocketAddr = matches
|
||||
.value_of("BIND")
|
||||
|
@ -65,14 +64,16 @@ fn main() -> std::io::Result<()> {
|
|||
.expect("Incorrect bind format.");
|
||||
info!("Starting server at: {}", &bind);
|
||||
|
||||
let state = routes::State {
|
||||
directory: vault_path.clone(),
|
||||
db: db_addr.clone(),
|
||||
hasher: hash_addr.clone(),
|
||||
};
|
||||
|
||||
// Start HTTP server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(routes::State {
|
||||
directory: path.clone(),
|
||||
db: db_addr.clone(),
|
||||
hasher: hash_addr.clone(),
|
||||
})
|
||||
.data(state.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(routes::get_raw)
|
||||
.service(routes::get_lookup)
|
||||
|
@ -81,6 +82,15 @@ fn main() -> std::io::Result<()> {
|
|||
.bind(&bind)?
|
||||
.run();
|
||||
|
||||
if open_result.new {
|
||||
info!("The vault has been just created, running initial update...");
|
||||
actix::spawn(dataops::update_directory_bg(
|
||||
vault_path.clone(),
|
||||
db_addr.clone(),
|
||||
hash_addr.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
// TODO REMOVE
|
||||
if !matches.is_present("NO_BROWSER") && false {
|
||||
let ui_result = webbrowser::open(&format!("http://localhost:{}", bind.port()));
|
||||
|
|
|
@ -4,6 +4,7 @@ use actix_web::{error, get, post, web, Error, HttpResponse};
|
|||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub directory: PathBuf,
|
||||
pub db: Addr<crate::database::DbExecutor>,
|
||||
|
|
Loading…
Reference in New Issue