From a3e266b8b4419bc0c9edcc9e25dcaa76cd162837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Thu, 17 Mar 2022 15:54:08 +0300 Subject: [PATCH] refactor(server): define global constants for environment variables --- src/lib.rs | 6 ++++++ src/main.rs | 4 +--- src/server.rs | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9793cc6..b34766f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,3 +27,9 @@ pub mod mime; /// Helper functions. pub mod util; + +/// Environment variable for setting the configuration file path. +pub const CONFIG_ENV: &str = "CONFIG"; + +/// Environment variable for setting the authentication token. +pub const AUTH_TOKEN_ENV: &str = "AUTH_TOKEN"; diff --git a/src/main.rs b/src/main.rs index f9b17a4..2176f51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use hotwatch::{Event, Hotwatch}; use rustypaste::config::Config; use rustypaste::paste::PasteType; use rustypaste::server; +use rustypaste::CONFIG_ENV; use std::env; use std::fs; use std::io::Result as IoResult; @@ -13,9 +14,6 @@ use std::path::PathBuf; use std::sync::RwLock; use std::time::Duration; -/// Environment variable for setting the configuration file path. -const CONFIG_ENV: &str = "CONFIG"; - #[actix_web::main] async fn main() -> IoResult<()> { // Initialize logger. diff --git a/src/server.rs b/src/server.rs index d6fa086..690830f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -5,6 +5,7 @@ use crate::header::{self, ContentDisposition}; use crate::mime; use crate::paste::{Paste, PasteType}; use crate::util; +use crate::AUTH_TOKEN_ENV; use actix_files::NamedFile; use actix_multipart::Multipart; use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse, Responder}; @@ -94,7 +95,7 @@ async fn upload( auth::check( host, request.headers(), - env::var("AUTH_TOKEN").ok().or(config + env::var(AUTH_TOKEN_ENV).ok().or(config .read() .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))? .server