mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-23 06:28:23 -05:00
♻️ refactor: initialize & store the config & cache structs as a constant (#486)
- initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route.
This commit is contained in:
parent
280c7e2b5e
commit
1a7675f779
@ -5,7 +5,7 @@
|
|||||||
#[cfg(not(feature = "dhat-heap"))]
|
#[cfg(not(feature = "dhat-heap"))]
|
||||||
use mimalloc::MiMalloc;
|
use mimalloc::MiMalloc;
|
||||||
|
|
||||||
use std::net::TcpListener;
|
use std::{net::TcpListener, sync::OnceLock};
|
||||||
use websurfx::{cache::cacher::create_cache, config::parser::Config, run};
|
use websurfx::{cache::cacher::create_cache, config::parser::Config, run};
|
||||||
|
|
||||||
/// A dhat heap memory profiler
|
/// A dhat heap memory profiler
|
||||||
@ -17,6 +17,9 @@ static ALLOC: dhat::Alloc = dhat::Alloc;
|
|||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL: MiMalloc = MiMalloc;
|
static GLOBAL: MiMalloc = MiMalloc;
|
||||||
|
|
||||||
|
/// A static constant for holding the parsed config.
|
||||||
|
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||||
|
|
||||||
/// The function that launches the main server and registers all the routes of the website.
|
/// The function that launches the main server and registers all the routes of the website.
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
@ -29,10 +32,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
#[cfg(feature = "dhat-heap")]
|
#[cfg(feature = "dhat-heap")]
|
||||||
let _profiler = dhat::Profiler::new_heap();
|
let _profiler = dhat::Profiler::new_heap();
|
||||||
|
|
||||||
// Initialize the parsed config file.
|
// Initialize the parsed config globally.
|
||||||
let config = Config::parse(false).unwrap();
|
let config = CONFIG.get_or_init(|| Config::parse(false).unwrap());
|
||||||
|
|
||||||
let cache = create_cache(&config).await;
|
let cache = create_cache(config).await;
|
||||||
|
|
||||||
log::info!(
|
log::info!(
|
||||||
"started server on port {} and IP {}",
|
"started server on port {} and IP {}",
|
||||||
|
13
src/lib.rs
13
src/lib.rs
@ -14,7 +14,7 @@ pub mod results;
|
|||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod templates;
|
pub mod templates;
|
||||||
|
|
||||||
use std::net::TcpListener;
|
use std::{net::TcpListener, sync::OnceLock};
|
||||||
|
|
||||||
use crate::server::router;
|
use crate::server::router;
|
||||||
|
|
||||||
@ -31,6 +31,9 @@ use cache::cacher::{Cacher, SharedCache};
|
|||||||
use config::parser::Config;
|
use config::parser::Config;
|
||||||
use handler::{file_path, FileType};
|
use handler::{file_path, FileType};
|
||||||
|
|
||||||
|
/// A static constant for holding the cache struct.
|
||||||
|
static SHARED_CACHE: OnceLock<SharedCache> = OnceLock::new();
|
||||||
|
|
||||||
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
|
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
@ -57,14 +60,14 @@ use handler::{file_path, FileType};
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn run(
|
pub fn run(
|
||||||
listener: TcpListener,
|
listener: TcpListener,
|
||||||
config: Config,
|
config: &'static Config,
|
||||||
cache: impl Cacher + 'static,
|
cache: impl Cacher + 'static,
|
||||||
) -> std::io::Result<Server> {
|
) -> std::io::Result<Server> {
|
||||||
let public_folder_path: &str = file_path(FileType::Theme)?;
|
let public_folder_path: &str = file_path(FileType::Theme)?;
|
||||||
|
|
||||||
let cloned_config_threads_opt: u8 = config.threads;
|
let cloned_config_threads_opt: u8 = config.threads;
|
||||||
|
|
||||||
let cache = web::Data::new(SharedCache::new(cache));
|
let cache = SHARED_CACHE.get_or_init(|| SharedCache::new(cache));
|
||||||
|
|
||||||
let server = HttpServer::new(move || {
|
let server = HttpServer::new(move || {
|
||||||
let cors: Cors = Cors::default()
|
let cors: Cors = Cors::default()
|
||||||
@ -81,8 +84,8 @@ pub fn run(
|
|||||||
// Compress the responses provided by the server for the client requests.
|
// Compress the responses provided by the server for the client requests.
|
||||||
.wrap(Compress::default())
|
.wrap(Compress::default())
|
||||||
.wrap(Logger::default()) // added logging middleware for logging.
|
.wrap(Logger::default()) // added logging middleware for logging.
|
||||||
.app_data(web::Data::new(config.clone()))
|
.app_data(web::Data::new(config))
|
||||||
.app_data(cache.clone())
|
.app_data(web::Data::new(cache))
|
||||||
.wrap(cors)
|
.wrap(cors)
|
||||||
.wrap(Governor::new(
|
.wrap(Governor::new(
|
||||||
&GovernorConfigBuilder::default()
|
&GovernorConfigBuilder::default()
|
||||||
|
@ -11,7 +11,9 @@ use std::fs::read_to_string;
|
|||||||
|
|
||||||
/// Handles the route of index page or main page of the `websurfx` meta search engine website.
|
/// Handles the route of index page or main page of the `websurfx` meta search engine website.
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
pub async fn index(
|
||||||
|
config: web::Data<&'static Config>,
|
||||||
|
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
||||||
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
||||||
crate::templates::views::index::index(
|
crate::templates::views::index::index(
|
||||||
&config.style.colorscheme,
|
&config.style.colorscheme,
|
||||||
@ -25,7 +27,7 @@ pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn st
|
|||||||
/// Handles the route of any other accessed route/page which is not provided by the
|
/// Handles the route of any other accessed route/page which is not provided by the
|
||||||
/// website essentially the 404 error page.
|
/// website essentially the 404 error page.
|
||||||
pub async fn not_found(
|
pub async fn not_found(
|
||||||
config: web::Data<Config>,
|
config: web::Data<&'static Config>,
|
||||||
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
||||||
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
||||||
crate::templates::views::not_found::not_found(
|
crate::templates::views::not_found::not_found(
|
||||||
@ -49,7 +51,9 @@ pub async fn robots_data(_req: HttpRequest) -> Result<HttpResponse, Box<dyn std:
|
|||||||
|
|
||||||
/// Handles the route of about page of the `websurfx` meta search engine website.
|
/// Handles the route of about page of the `websurfx` meta search engine website.
|
||||||
#[get("/about")]
|
#[get("/about")]
|
||||||
pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
pub async fn about(
|
||||||
|
config: web::Data<&'static Config>,
|
||||||
|
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
||||||
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
||||||
crate::templates::views::about::about(
|
crate::templates::views::about::about(
|
||||||
&config.style.colorscheme,
|
&config.style.colorscheme,
|
||||||
@ -63,7 +67,7 @@ pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn st
|
|||||||
/// Handles the route of settings page of the `websurfx` meta search engine website.
|
/// Handles the route of settings page of the `websurfx` meta search engine website.
|
||||||
#[get("/settings")]
|
#[get("/settings")]
|
||||||
pub async fn settings(
|
pub async fn settings(
|
||||||
config: web::Data<Config>,
|
config: web::Data<&'static Config>,
|
||||||
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
||||||
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
|
||||||
crate::templates::views::settings::settings(
|
crate::templates::views::settings::settings(
|
||||||
|
@ -37,8 +37,8 @@ use tokio::join;
|
|||||||
#[get("/search")]
|
#[get("/search")]
|
||||||
pub async fn search(
|
pub async fn search(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
config: web::Data<Config>,
|
config: web::Data<&'static Config>,
|
||||||
cache: web::Data<SharedCache>,
|
cache: web::Data<&'static SharedCache>,
|
||||||
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
let params = web::Query::<SearchParams>::from_query(req.query_string())?;
|
let params = web::Query::<SearchParams>::from_query(req.query_string())?;
|
||||||
@ -158,8 +158,8 @@ pub async fn search(
|
|||||||
/// It returns the `SearchResults` struct if the search results could be successfully fetched from
|
/// 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.
|
/// the cache or from the upstream search engines otherwise it returns an appropriate error.
|
||||||
async fn results(
|
async fn results(
|
||||||
config: &Config,
|
config: &'static Config,
|
||||||
cache: &web::Data<SharedCache>,
|
cache: &'static SharedCache,
|
||||||
query: &str,
|
query: &str,
|
||||||
page: u32,
|
page: u32,
|
||||||
search_settings: &server_models::Cookie<'_>,
|
search_settings: &server_models::Cookie<'_>,
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
use std::net::TcpListener;
|
use std::{net::TcpListener, sync::OnceLock};
|
||||||
|
|
||||||
use websurfx::{config::parser::Config, run, templates::views};
|
use websurfx::{config::parser::Config, run, templates::views};
|
||||||
|
|
||||||
|
/// A static constant for holding the parsed config.
|
||||||
|
static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||||
|
|
||||||
// Starts a new instance of the HTTP server, bound to a random available port
|
// Starts a new instance of the HTTP server, bound to a random available port
|
||||||
async fn spawn_app() -> String {
|
async fn spawn_app() -> String {
|
||||||
// Binding to port 0 will trigger the OS to assign a port for us.
|
// Binding to port 0 will trigger the OS to assign a port for us.
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||||
let port = listener.local_addr().unwrap().port();
|
let port = listener.local_addr().unwrap().port();
|
||||||
let config = Config::parse(false).unwrap();
|
let config = CONFIG.get_or_init(|| Config::parse(false).unwrap());
|
||||||
let cache = websurfx::cache::cacher::create_cache(&config).await;
|
let cache = websurfx::cache::cacher::create_cache(config).await;
|
||||||
let server = run(listener, config, cache).expect("Failed to bind address");
|
let server = run(listener, config, cache).expect("Failed to bind address");
|
||||||
|
|
||||||
tokio::spawn(server);
|
tokio::spawn(server);
|
||||||
|
Loading…
Reference in New Issue
Block a user