From 1a07cc672b4b118a4f850a5cfb55c8af7b987d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Wed, 16 Mar 2022 16:53:21 +0300 Subject: [PATCH] fix(server): use actix data to manage the config --- src/main.rs | 11 ++++++----- src/server.rs | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88559a0..f9b17a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use actix_web::middleware::Logger; +use actix_web::web::Data; use actix_web::{App, HttpServer}; use awc::ClientBuilder; use hotwatch::{Event, Hotwatch}; @@ -9,7 +10,7 @@ use std::env; use std::fs; use std::io::Result as IoResult; use std::path::PathBuf; -use std::sync::{Arc, RwLock}; +use std::sync::RwLock; use std::time::Duration; /// Environment variable for setting the configuration file path. @@ -49,8 +50,8 @@ async fn main() -> IoResult<()> { .expect("failed to initialize configuration file watcher"); // Hot-reload the configuration file. - let config = Arc::new(RwLock::new(config)); - let cloned_config = Arc::clone(&config); + let config = Data::new(RwLock::new(config)); + let cloned_config = Data::clone(&config); let config_watcher = move |event: Event| { if let Event::Write(path) = event { match Config::parse(&path) { @@ -84,8 +85,8 @@ async fn main() -> IoResult<()> { .disable_redirects() .finish(); App::new() - .app_data(Arc::clone(&config)) - .app_data(http_client) + .app_data(Data::clone(&config)) + .app_data(Data::new(http_client)) .wrap(Logger::default()) .configure(server::configure_routes) }) diff --git a/src/server.rs b/src/server.rs index 5404d57..4eb800c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -14,7 +14,7 @@ use futures_util::stream::StreamExt; use std::convert::TryFrom; use std::env; use std::fs; -use std::sync::{Arc, RwLock}; +use std::sync::RwLock; /// Shows the landing page. #[get("/")] @@ -29,7 +29,7 @@ async fn index() -> impl Responder { async fn serve( request: HttpRequest, file: web::Path, - config: web::Data>>, + config: web::Data>, ) -> Result { let config = config .read() @@ -85,7 +85,7 @@ async fn upload( request: HttpRequest, mut payload: Multipart, client: web::Data, - config: web::Data>>, + config: web::Data>, ) -> Result { let connection = request.connection_info().clone(); let host = connection.peer_addr().unwrap_or("unknown host");