- Removing dotenv from required dependancies

+ Leveraging serde_json to use a config.json
This lets us use the same config for both the json-api and the rtc-server without adding dependancies
This commit is contained in:
shockrah
2021-05-05 15:50:39 -07:00
parent 5d0f02507b
commit a628d56e25
5 changed files with 78 additions and 79 deletions

View File

@@ -1,6 +1,5 @@
extern crate db;
extern crate clap;
extern crate dotenv;
extern crate getrandom;
extern crate bcrypt;
extern crate base64;
@@ -23,8 +22,8 @@ use hyper::{
HeaderMap
};
use mysql_async::Pool;
use serde::Deserialize;
use dotenv::dotenv;
use clap::{Arg, App};
use auth::AuthReason;
@@ -193,14 +192,51 @@ async fn attempt_owner_creation(name: &str) {
let _ = p.disconnect().await;
}
fn init_config() -> Result<(), Box<dyn std::error::Error>> {
#[derive(Deserialize, Debug)]
struct RequiredFields {
pub database_url: String,
pub hmac_path: String,
pub wss_hmac_path: String,
pub name: String,
pub description: Option<String>,
pub public_url: String,
pub public_ws_url: String,
pub tags: Option<Vec<String>>
}
use std::fs::File;
use std::io::BufReader;
let file = File::open("config.json")?;
let reader = BufReader::new(file);
let fields: RequiredFields = serde_json::from_reader(reader)?;
// Now we can setup each environment variable for this process from config.json
// Note that we only have to do this once since all of these are read from
// lazy statics so the cost is very minimal
set_var("DATABASE_URL", fields.database_url);
set_var("HMAC_PATH", fields.hmac_path);
set_var("WSS_HMAC_PATH", fields.wss_hmac_path);
set_var("SERVER_NAME", fields.name);
set_var("SERVER_DESCRIPTION", fields.description.unwrap_or("".into()));
set_var("PUBLIC_URL", fields.public_url);
set_var("PUBLIC_WS_URL", fields.public_ws_url);
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), u16>{
let mut main_ret: u16 = 0;
let d_result = dotenv();
let mut main_ret: u16 = 0; let d_result = init_config();
// check for a database_url before the override we get from the cmd line
if let Err(_d) = d_result {
if let Err(d) = d_result {
eprintln!("Config error: {}", d);
if let Err(_e) = env::var("DATABASE_URL") {
main_ret |= CONFIG_ERR;
}
@@ -243,9 +279,6 @@ async fn main() -> Result<(), u16>{
.get_matches();
if let Some(db_url) = args.value_of("db-url") {
set_var("DATABASE_URL", db_url);
}
// safe because we have a default value set in code
let port = args.value_of("port").unwrap().to_string();
@@ -255,7 +288,7 @@ async fn main() -> Result<(), u16>{
attempt_owner_creation(owner_name).await;
}
// This check overrides the value set in the .env since this
// Here we override some of the config.json variables
if let Some(hmac) = args.value_of("hmac") {
std::env::set_var("HMAC_PATH", hmac);
}