fix(server): use actix data to manage the config
This commit is contained in:
parent
55ec642b10
commit
1a07cc672b
11
src/main.rs
11
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use actix_web::middleware::Logger;
|
use actix_web::middleware::Logger;
|
||||||
|
use actix_web::web::Data;
|
||||||
use actix_web::{App, HttpServer};
|
use actix_web::{App, HttpServer};
|
||||||
use awc::ClientBuilder;
|
use awc::ClientBuilder;
|
||||||
use hotwatch::{Event, Hotwatch};
|
use hotwatch::{Event, Hotwatch};
|
||||||
|
@ -9,7 +10,7 @@ use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Result as IoResult;
|
use std::io::Result as IoResult;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::RwLock;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Environment variable for setting the configuration file path.
|
/// Environment variable for setting the configuration file path.
|
||||||
|
@ -49,8 +50,8 @@ async fn main() -> IoResult<()> {
|
||||||
.expect("failed to initialize configuration file watcher");
|
.expect("failed to initialize configuration file watcher");
|
||||||
|
|
||||||
// Hot-reload the configuration file.
|
// Hot-reload the configuration file.
|
||||||
let config = Arc::new(RwLock::new(config));
|
let config = Data::new(RwLock::new(config));
|
||||||
let cloned_config = Arc::clone(&config);
|
let cloned_config = Data::clone(&config);
|
||||||
let config_watcher = move |event: Event| {
|
let config_watcher = move |event: Event| {
|
||||||
if let Event::Write(path) = event {
|
if let Event::Write(path) = event {
|
||||||
match Config::parse(&path) {
|
match Config::parse(&path) {
|
||||||
|
@ -84,8 +85,8 @@ async fn main() -> IoResult<()> {
|
||||||
.disable_redirects()
|
.disable_redirects()
|
||||||
.finish();
|
.finish();
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(Arc::clone(&config))
|
.app_data(Data::clone(&config))
|
||||||
.app_data(http_client)
|
.app_data(Data::new(http_client))
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.configure(server::configure_routes)
|
.configure(server::configure_routes)
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,7 +14,7 @@ use futures_util::stream::StreamExt;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::RwLock;
|
||||||
|
|
||||||
/// Shows the landing page.
|
/// Shows the landing page.
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -29,7 +29,7 @@ async fn index() -> impl Responder {
|
||||||
async fn serve(
|
async fn serve(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
file: web::Path<String>,
|
file: web::Path<String>,
|
||||||
config: web::Data<Arc<RwLock<Config>>>,
|
config: web::Data<RwLock<Config>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let config = config
|
let config = config
|
||||||
.read()
|
.read()
|
||||||
|
@ -85,7 +85,7 @@ async fn upload(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
mut payload: Multipart,
|
mut payload: Multipart,
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
config: web::Data<Arc<RwLock<Config>>>,
|
config: web::Data<RwLock<Config>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let connection = request.connection_info().clone();
|
let connection = request.connection_info().clone();
|
||||||
let host = connection.peer_addr().unwrap_or("unknown host");
|
let host = connection.peer_addr().unwrap_or("unknown host");
|
||||||
|
|
Loading…
Reference in New Issue