refactor(server): define global constants for environment variables

This commit is contained in:
Orhun Parmaksız 2022-03-17 15:54:08 +03:00
parent c48e45d68c
commit a3e266b8b4
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
3 changed files with 9 additions and 4 deletions

View File

@ -27,3 +27,9 @@ pub mod mime;
/// Helper functions. /// Helper functions.
pub mod util; 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";

View File

@ -6,6 +6,7 @@ use hotwatch::{Event, Hotwatch};
use rustypaste::config::Config; use rustypaste::config::Config;
use rustypaste::paste::PasteType; use rustypaste::paste::PasteType;
use rustypaste::server; use rustypaste::server;
use rustypaste::CONFIG_ENV;
use std::env; use std::env;
use std::fs; use std::fs;
use std::io::Result as IoResult; use std::io::Result as IoResult;
@ -13,9 +14,6 @@ use std::path::PathBuf;
use std::sync::RwLock; use std::sync::RwLock;
use std::time::Duration; use std::time::Duration;
/// Environment variable for setting the configuration file path.
const CONFIG_ENV: &str = "CONFIG";
#[actix_web::main] #[actix_web::main]
async fn main() -> IoResult<()> { async fn main() -> IoResult<()> {
// Initialize logger. // Initialize logger.

View File

@ -5,6 +5,7 @@ use crate::header::{self, ContentDisposition};
use crate::mime; use crate::mime;
use crate::paste::{Paste, PasteType}; use crate::paste::{Paste, PasteType};
use crate::util; use crate::util;
use crate::AUTH_TOKEN_ENV;
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse, Responder}; use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse, Responder};
@ -94,7 +95,7 @@ async fn upload(
auth::check( auth::check(
host, host,
request.headers(), request.headers(),
env::var("AUTH_TOKEN").ok().or(config env::var(AUTH_TOKEN_ENV).ok().or(config
.read() .read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))? .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?
.server .server