proper config base

This commit is contained in:
Milim 2024-08-15 07:53:26 +02:00
parent c9fe79eb12
commit 99a1089df9
No known key found for this signature in database
10 changed files with 114 additions and 203 deletions

View file

@ -1,25 +1,23 @@
//! This module provides the functionality to cache the aggregated results fetched and aggregated
//! from the upstream search engines in a json format.
use error_stack::Report;
use mini_moka::sync::Cache as MokaCache;
use mini_moka::sync::ConcurrentCacheExt;
use std::time::Duration;
use crate::{config::parser::Config, models::aggregation_models::SearchResults};
use crate::{config::Config, models::aggregation_models::SearchResults};
impl Into<SearchResults> for Vec<u8> {
fn into(self) -> SearchResults {
serde_json::from_slice(&self)
impl From<Vec<u8>> for SearchResults {
fn from(v: Vec<u8>) -> SearchResults {
serde_json::from_slice(&v)
.expect("well, this can only be caused by memory corruption so good luck")
}
}
impl Into<Vec<u8>> for &SearchResults {
fn into(self) -> Vec<u8> {
serde_json::to_vec(self).expect("somehow failed to serialize search results")
impl From<&SearchResults> for Vec<u8> {
fn from(v: &SearchResults) -> Vec<u8> {
serde_json::to_vec(v).expect("somehow failed to serialize search results")
}
}

View file

@ -1,11 +1,12 @@
//! This module provides the functionality to parse the lua config and convert the config options
//! into rust readable form.
//! Config module
use figment::{providers::Serialized, Figment};
use serde::{Deserialize, Serialize};
use crate::models::parser_models::Style;
use std::collections::HashMap;
/// A named struct which stores the parsed config file options.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
/// It stores the parsed port number option on which the server should launch.
pub port: u16,
@ -58,6 +59,9 @@ impl Default for Config {
impl Config {
/// Creates a new config based on the environment variables.
pub fn parse() -> Self {
Self::default()
Figment::from(Serialized::defaults(Config::default()))
.merge(figment::providers::Env::prefixed("SEARCH"))
.extract()
.unwrap()
}
}

View file

@ -1,4 +0,0 @@
//! This module provides the modules which handles the functionality to parse the lua config
//! and convert the config options into rust readable form.
pub mod parser;

View file

@ -26,7 +26,7 @@ use actix_web::{
middleware::{Compress, Logger},
web, App, HttpServer,
};
use config::parser::Config;
use config::Config;
use handler::{file_path, FileType};
/// Runs the web server

View file

@ -1,6 +1,8 @@
//! This module provides public models for handling, storing and serializing parsed config file
//! options from config.lua by grouping them together.
use serde::{Deserialize, Serialize};
/// A named struct which stores,deserializes, serializes and groups the parsed config file options
/// of theme and colorscheme names into the Style struct which derives the `Clone`, `Serialize`
/// and Deserialize traits where the `Clone` trait is derived for allowing the struct to be
@ -10,7 +12,7 @@
/// order to allow the deserializing the json back to struct in aggregate function in
/// aggregator.rs and create a new struct out of it and then serialize it back to json and pass
/// it to the template files.
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct Style {
/// It stores the parsed theme option used to set a theme for the website.
pub theme: String,

View file

@ -2,7 +2,7 @@
//! search engines and then removes duplicate results.
use super::user_agent::random_user_agent;
use crate::config::parser::Config;
use crate::config::Config;
use crate::models::{
aggregation_models::{EngineErrorInfo, SearchResult, SearchResults},
engine_models::{EngineError, EngineHandler},
@ -69,7 +69,7 @@ type FutureVec =
pub async fn aggregate(
query: &str,
page: u32,
config: actix_web::web::Data<crate::config::parser::Config>,
config: actix_web::web::Data<Config>,
upstream_search_engines: &[EngineHandler],
) -> Result<SearchResults, Box<dyn std::error::Error>> {
let client = CLIENT.get_or_init(|| {

View file

@ -3,7 +3,7 @@
//! when requested.
use crate::{
config::parser::Config,
config::Config,
handler::{file_path, FileType},
};
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};

View file

@ -2,7 +2,7 @@
use crate::{
cache::Cache,
config::parser::Config,
config::Config,
models::{
aggregation_models::SearchResults,
engine_models::EngineHandler,
@ -144,7 +144,7 @@ pub async fn search(
/// It returns the `SearchResults` struct if the search results could be successfully fetched from
/// the cache or from the upstream search engines otherwise it returns an appropriate error.
async fn results(
config: web::Data<crate::config::parser::Config>,
config: web::Data<Config>,
cache: web::Data<crate::cache::Cache>,
query: &str,
page: u32,