diff --git a/json-api/.env b/json-api/.env index 04d1e15..17d799e 100644 --- a/json-api/.env +++ b/json-api/.env @@ -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 +# 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_PASS=password DATABASE_USER=freechat_dev @@ -11,9 +18,14 @@ HMAC_PATH=hmac.secret WSS_HMAC_PATH=wss-hmac.secret -# Server meta things +# Public name that your server has SERVER_NAME="Freechat Dev Server" +# Public description of what youre server is about 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_WS_URL=ws://localhost:5648 + diff --git a/json-api/Cargo.lock b/json-api/Cargo.lock index 6fa65be..bea1027 100644 --- a/json-api/Cargo.lock +++ b/json-api/Cargo.lock @@ -12,15 +12,6 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "aho-corasick" version = "0.7.15" @@ -272,15 +263,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "flate2" version = "1.0.20" @@ -606,7 +588,6 @@ dependencies = [ "bcrypt", "clap", "db", - "dotenv", "futures", "getrandom 0.1.16", "hyper", @@ -800,7 +781,7 @@ dependencies = [ "num-bigint 0.3.2", "num-traits", "rand 0.8.3", - "regex 1.4.5", + "regex", "rust_decimal", "serde", "serde_json", @@ -967,7 +948,7 @@ checksum = "fd56cbd21fea48d0c440b41cd69c589faacade08c992d9a54e471b79d0fd13eb" dependencies = [ "base64 0.13.0", "once_cell", - "regex 1.4.5", + "regex", ] [[package]] @@ -1146,37 +1127,15 @@ dependencies = [ "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]] name = "regex" version = "1.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19" dependencies = [ - "aho-corasick 0.7.15", + "aho-corasick", "memchr", - "regex-syntax 0.6.23", -] - -[[package]] -name = "regex-syntax" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" -dependencies = [ - "ucd-util", + "regex-syntax", ] [[package]] @@ -1524,15 +1483,6 @@ dependencies = [ "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]] name = "time" version = "0.1.43" @@ -1757,12 +1707,6 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" -[[package]] -name = "ucd-util" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c85f514e095d348c279b1e5cd76795082cf15bd59b93207832abe0b1d8fed236" - [[package]] name = "unicode-bidi" version = "0.3.5" @@ -1817,12 +1761,6 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" -[[package]] -name = "utf8-ranges" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" - [[package]] name = "uuid" version = "0.8.2" diff --git a/json-api/Cargo.toml b/json-api/Cargo.toml index 5d7c03e..b8b2b29 100644 --- a/json-api/Cargo.toml +++ b/json-api/Cargo.toml @@ -18,8 +18,6 @@ futures = "0.3" url = "2.2.1" -dotenv = "0.9.0" - # Crypto things getrandom = "0.1" bcrypt = "0.8" diff --git a/json-api/config.json b/json-api/config.json new file mode 100644 index 0000000..f200e31 --- /dev/null +++ b/json-api/config.json @@ -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" + ] +} diff --git a/json-api/src/main.rs b/json-api/src/main.rs index cd5b611..a1e1a38 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -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> { + #[derive(Deserialize, Debug)] + struct RequiredFields { + pub database_url: String, + pub hmac_path: String, + pub wss_hmac_path: String, + pub name: String, + pub description: Option, + pub public_url: String, + pub public_ws_url: String, + pub tags: Option> + } + 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); }