Support for using a master key (#83)

* Support for using a master key

* Expand scope of PR, add wrapper struct, add files to intitial versions/mods

* Change changelog path, run formatter

* Split file changes into different PR

* Formatting, rename main variable

Co-authored-by: Aeledfyr <aeledfyr@gmail.com>
This commit is contained in:
Geometrically
2020-10-19 14:23:05 -07:00
committed by GitHub
parent e0b972f6d6
commit c886e7949e
8 changed files with 75 additions and 25 deletions

View File

@@ -3,7 +3,7 @@ pub mod curseforge_import;
pub mod local_import;
pub mod queue;
use crate::search::UploadSearchMod;
use crate::search::{SearchConfig, UploadSearchMod};
use curseforge_import::index_curseforge;
use local_import::index_local;
use meilisearch_sdk::client::Client;
@@ -56,7 +56,11 @@ impl IndexingSettings {
}
}
pub async fn index_mods(pool: PgPool, settings: IndexingSettings) -> Result<(), IndexingError> {
pub async fn index_mods(
pool: PgPool,
settings: IndexingSettings,
config: &SearchConfig,
) -> Result<(), IndexingError> {
let mut docs_to_add: Vec<UploadSearchMod> = vec![];
if settings.index_local {
@@ -73,14 +77,13 @@ pub async fn index_mods(pool: PgPool, settings: IndexingSettings) -> Result<(),
// Write Indices
add_mods(docs_to_add).await?;
add_mods(docs_to_add, config).await?;
Ok(())
}
pub async fn reset_indices() -> Result<(), IndexingError> {
let address = &*dotenv::var("MEILISEARCH_ADDR")?;
let client = Client::new(address, "");
pub async fn reset_indices(config: &SearchConfig) -> Result<(), IndexingError> {
let client = Client::new(&*config.address, &*config.key);
client.delete_index("relevance_mods").await?;
client.delete_index("downloads_mods").await?;
@@ -89,9 +92,8 @@ pub async fn reset_indices() -> Result<(), IndexingError> {
Ok(())
}
pub async fn reconfigure_indices() -> Result<(), IndexingError> {
let address = &*dotenv::var("MEILISEARCH_ADDR")?;
let client = Client::new(address, "");
pub async fn reconfigure_indices(config: &SearchConfig) -> Result<(), IndexingError> {
let client = Client::new(&*config.address, &*config.key);
// Relevance Index
update_index(&client, "relevance_mods", {
@@ -184,9 +186,11 @@ async fn add_to_index(index: Index<'_>, mods: &[UploadSearchMod]) -> Result<(),
Ok(())
}
pub async fn add_mods(mods: Vec<UploadSearchMod>) -> Result<(), IndexingError> {
let address = &*dotenv::var("MEILISEARCH_ADDR")?;
let client = Client::new(address, "");
pub async fn add_mods(
mods: Vec<UploadSearchMod>,
config: &SearchConfig,
) -> Result<(), IndexingError> {
let client = Client::new(&*config.address, &*config.key);
// Relevance Index
let relevance_index = create_index(&client, "relevance_mods", || {

View File

@@ -1,4 +1,5 @@
use super::{add_mods, IndexingError, UploadSearchMod};
use crate::search::SearchConfig;
use std::sync::Mutex;
pub struct CreationQueue {
@@ -25,7 +26,10 @@ impl CreationQueue {
}
}
pub async fn index_queue(queue: &CreationQueue) -> Result<(), IndexingError> {
pub async fn index_queue(
queue: &CreationQueue,
config: &SearchConfig,
) -> Result<(), IndexingError> {
let queue = queue.take();
add_mods(queue).await
add_mods(queue, config).await
}

View File

@@ -52,6 +52,12 @@ impl actix_web::ResponseError for SearchError {
}
}
#[derive(Clone)]
pub struct SearchConfig {
pub address: String,
pub key: String,
}
/// A mod document used for uploading mods to meilisearch's indices.
/// This contains some extra data that is not returned by search results.
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -133,9 +139,11 @@ impl Document for ResultSearchMod {
}
}
pub async fn search_for_mod(info: &SearchRequest) -> Result<SearchResults, SearchError> {
let address = &*dotenv::var("MEILISEARCH_ADDR")?;
let client = Client::new(address, "");
pub async fn search_for_mod(
info: &SearchRequest,
config: &SearchConfig,
) -> Result<SearchResults, SearchError> {
let client = Client::new(&*config.key, &*config.address);
let filters: Cow<_> = match (info.filters.as_deref(), info.version.as_deref()) {
(Some(f), Some(v)) => format!("({}) AND ({})", f, v).into(),