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.
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::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.

View file

@ -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