- 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,4 +1,11 @@
# Example .env file
# In theory this file can be used for _both_ the rtc-server and the json-api server
# This field is required by the json-api binary to actually find the database
DATABASE_URL=mysql://freechat_dev:password@localhost:3306/freechat DATABASE_URL=mysql://freechat_dev:password@localhost:3306/freechat
# The fields below are unused but are here in case you're confused as to the
# structure of the url above
# It is perfectly safe to delete these
DATABASE_NAME=freechat DATABASE_NAME=freechat
DATABASE_PASS=password DATABASE_PASS=password
DATABASE_USER=freechat_dev DATABASE_USER=freechat_dev
@ -11,9 +18,14 @@ HMAC_PATH=hmac.secret
WSS_HMAC_PATH=wss-hmac.secret WSS_HMAC_PATH=wss-hmac.secret
# Server meta things # Public name that your server has
SERVER_NAME="Freechat Dev Server" SERVER_NAME="Freechat Dev Server"
# Public description of what youre server is about
SERVER_DESCRIPTION="Server for sick development things" SERVER_DESCRIPTION="Server for sick development things"
# NOTE: most clients shouldn't expect these to end with a slash
# These are the actual url's which are sent to/used by clients to communicate with
# your instance. If you have a reverse proxy setup then you can configure it so that
# the port numbers are not in the URL(suggested)
PUBLIC_URL=http://localhost:4536 PUBLIC_URL=http://localhost:4536
PUBLIC_WS_URL=ws://localhost:5648 PUBLIC_WS_URL=ws://localhost:5648

70
json-api/Cargo.lock generated
View File

@ -12,15 +12,6 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e" checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]]
name = "aho-corasick"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.15" version = "0.7.15"
@ -272,15 +263,6 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]]
name = "dotenv"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "400b347fe65ccfbd8f545c9d9a75d04b0caf23fec49aaa838a9a05398f94c019"
dependencies = [
"regex 0.2.11",
]
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.20" version = "1.0.20"
@ -606,7 +588,6 @@ dependencies = [
"bcrypt", "bcrypt",
"clap", "clap",
"db", "db",
"dotenv",
"futures", "futures",
"getrandom 0.1.16", "getrandom 0.1.16",
"hyper", "hyper",
@ -800,7 +781,7 @@ dependencies = [
"num-bigint 0.3.2", "num-bigint 0.3.2",
"num-traits", "num-traits",
"rand 0.8.3", "rand 0.8.3",
"regex 1.4.5", "regex",
"rust_decimal", "rust_decimal",
"serde", "serde",
"serde_json", "serde_json",
@ -967,7 +948,7 @@ checksum = "fd56cbd21fea48d0c440b41cd69c589faacade08c992d9a54e471b79d0fd13eb"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"once_cell", "once_cell",
"regex 1.4.5", "regex",
] ]
[[package]] [[package]]
@ -1146,37 +1127,15 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "regex"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
dependencies = [
"aho-corasick 0.6.10",
"memchr",
"regex-syntax 0.5.6",
"thread_local",
"utf8-ranges",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.4.5" version = "1.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19" checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19"
dependencies = [ dependencies = [
"aho-corasick 0.7.15", "aho-corasick",
"memchr", "memchr",
"regex-syntax 0.6.23", "regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
dependencies = [
"ucd-util",
] ]
[[package]] [[package]]
@ -1524,15 +1483,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.43" version = "0.1.43"
@ -1757,12 +1707,6 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
[[package]]
name = "ucd-util"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85f514e095d348c279b1e5cd76795082cf15bd59b93207832abe0b1d8fed236"
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.5" version = "0.3.5"
@ -1817,12 +1761,6 @@ version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7"
[[package]]
name = "utf8-ranges"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "0.8.2" version = "0.8.2"

View File

@ -18,8 +18,6 @@ futures = "0.3"
url = "2.2.1" url = "2.2.1"
dotenv = "0.9.0"
# Crypto things # Crypto things
getrandom = "0.1" getrandom = "0.1"
bcrypt = "0.8" bcrypt = "0.8"

18
json-api/config.json Normal file
View File

@ -0,0 +1,18 @@
{
"#": "The fields below are required and can not be empty",
"#": "All other fields are ignored completely",
"database_url": "mysql://freechat_dev:password@localhost:3306/freechat",
"hmac_path": "hmac.secret",
"wss_hmac_path": "wss-hmac.secret",
"name": "Freechat Server",
"description": "Chatting server",
"public_url": "http://localhost:4536",
"public_ws_url": "ws://localhost:5648",
"tags": [
"Freechat",
"Libre software",
"Chatting"
]
}

View File

@ -1,6 +1,5 @@
extern crate db; extern crate db;
extern crate clap; extern crate clap;
extern crate dotenv;
extern crate getrandom; extern crate getrandom;
extern crate bcrypt; extern crate bcrypt;
extern crate base64; extern crate base64;
@ -23,8 +22,8 @@ use hyper::{
HeaderMap HeaderMap
}; };
use mysql_async::Pool; use mysql_async::Pool;
use serde::Deserialize;
use dotenv::dotenv;
use clap::{Arg, App}; use clap::{Arg, App};
use auth::AuthReason; use auth::AuthReason;
@ -193,14 +192,51 @@ async fn attempt_owner_creation(name: &str) {
let _ = p.disconnect().await; 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] #[tokio::main]
async fn main() -> Result<(), u16>{ async fn main() -> Result<(), u16>{
let mut main_ret: u16 = 0; let mut main_ret: u16 = 0; let d_result = init_config();
let d_result = dotenv();
// check for a database_url before the override we get from the cmd line // 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") { if let Err(_e) = env::var("DATABASE_URL") {
main_ret |= CONFIG_ERR; main_ret |= CONFIG_ERR;
} }
@ -243,9 +279,6 @@ async fn main() -> Result<(), u16>{
.get_matches(); .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 // safe because we have a default value set in code
let port = args.value_of("port").unwrap().to_string(); let port = args.value_of("port").unwrap().to_string();
@ -255,7 +288,7 @@ async fn main() -> Result<(), u16>{
attempt_owner_creation(owner_name).await; 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") { if let Some(hmac) = args.value_of("hmac") {
std::env::set_var("HMAC_PATH", hmac); std::env::set_var("HMAC_PATH", hmac);
} }