diff --git a/server/src/bin/server.rs b/server/src/bin/server.rs index 3e31bd1..80f1cbf 100644 --- a/server/src/bin/server.rs +++ b/server/src/bin/server.rs @@ -168,6 +168,13 @@ fn graphiql() -> content::RawHtml { #[rocket::post("/create-news-db")] fn create_news_db(config: &State) -> Result> { + create_news_db_impl(config)?; + Ok(format!( + "DB created in {}\n", + config.newsreader_tantivy_db_path + )) +} +fn create_news_db_impl(config: &Config) -> Result<(), ServerError> { std::fs::remove_dir_all(&config.newsreader_tantivy_db_path).map_err(ServerError::from)?; std::fs::create_dir_all(&config.newsreader_tantivy_db_path).map_err(ServerError::from)?; use tantivy::schema::*; @@ -183,10 +190,7 @@ fn create_news_db(config: &State) -> Result> let schema = schema_builder.build(); Index::create_in_dir(&config.newsreader_tantivy_db_path, schema).map_err(ServerError::from)?; - Ok(format!( - "DB created in {}\n", - config.newsreader_tantivy_db_path - )) + Ok(()) } #[rocket::post("/reindex-news-db")] @@ -348,7 +352,13 @@ async fn main() -> Result<(), Box> { std::fs::create_dir_all(&config.slurp_cache_path)?; } let pool = PgPool::connect(&config.newsreader_database_url).await?; - let tantivy_newsreader_index = Index::open_in_dir(&config.newsreader_tantivy_db_path)?; + let tantivy_newsreader_index = match Index::open_in_dir(&config.newsreader_tantivy_db_path) { + Ok(idx) => idx, + Err(_) => { + create_news_db_impl(&config)?; + Index::open_in_dir(&config.newsreader_tantivy_db_path)? + } + }; let tantivy_newsreader_reader = tantivy_newsreader_index.reader()?; let schema = Schema::build(QueryRoot, Mutation, EmptySubscription) .data(Notmuch::default())