diff --git a/Cargo.lock b/Cargo.lock index 477ecee..a1054af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -462,6 +462,24 @@ dependencies = [ "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "envconfig" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "envconfig_derive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "error-chain" version = "0.8.1" @@ -931,10 +949,13 @@ dependencies = [ "actix-redis 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "envconfig 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "envconfig_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "redis-async 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "whoami 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1742,6 +1763,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "whoami" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "widestring" version = "0.2.2" @@ -1863,6 +1892,8 @@ dependencies = [ "checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" "checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" "checksum env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "afb070faf94c85d17d50ca44f6ad076bce18ae92f0037d350947240a36e9d42e" +"checksum envconfig 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a960f46b5328db8969c203af0ed2982c46991e54c283a8a99a71c5f35b5b6e31" +"checksum envconfig_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bcb67ba951435ee553f2b294e1b4b77a3ff77611a4884ebe96883e3b4a1a2e45" "checksum error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6930e04918388a9a2e41d518c25cf679ccafe26733fb4127dbf21993f2575d46" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" @@ -2005,6 +2036,7 @@ dependencies = [ "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum whoami 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1b9c210f9606a8560c558ce6fe3b1a6d10ade8549ac14f5fdee4b0bd8efb2bf4" "checksum widestring 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7157704c2e12e3d2189c507b7482c52820a16dfa4465ba91add92f266667cadb" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" diff --git a/Cargo.toml b/Cargo.toml index d5c5012..e69edd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,10 @@ actix = "0.7.9" actix-redis = "0.5.1" redis-async = "0.4.4" uuid = "0.7.2" +envconfig = "0.5.0" +envconfig_derive = "0.5.0" +whoami = "0.4.1" + +[features] +default = [ "production" ] +production = [] diff --git a/src/api/ws/mod.rs b/src/api/ws.rs similarity index 95% rename from src/api/ws/mod.rs rename to src/api/ws.rs index f5a12fe..c0de2cb 100644 --- a/src/api/ws/mod.rs +++ b/src/api/ws.rs @@ -16,7 +16,7 @@ impl Actor for WebsocketActor { impl StreamHandler for WebsocketActor { fn started(&mut self, ctx: &mut Self::Context) { ctx.run_interval(Duration::from_secs(HEARTBEAT_INTERVAL_SECONDS), |_, inner_ctx| { - inner_ctx.ping("0123456789abcdef"); + inner_ctx.ping(""); }); } diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..c9607be --- /dev/null +++ b/src/env.rs @@ -0,0 +1,60 @@ +use envconfig::Envconfig; +use std::net::IpAddr; + +/// Returns the current users username. +/// TODO: Find a way to do this cross-platform +pub fn current_user() -> String { + whoami::username() +} + +#[cfg(feature = "production")] +#[derive(Envconfig)] +/// Production DB configuration +pub struct DbConfig { + #[envconfig(from = "DB_USER", default = "mastodon")] + user: String, + #[envconfig(from = "DB_PASS", default = "")] + password: String, + #[envconfig(from = "DB_NAME", default = "mastodon_production")] + database: String, + #[envconfig(from = "DB_HOST", default = "localhost")] + host: String, + #[envconfig(from = "DB_PORT", default = "5432")] + port: u16, +} + +#[cfg(not(feature = "production"))] +#[derive(Envconfig)] +/// Development DB configuration +pub struct DbConfig { + #[envconfig(from = "DB_USER", default = current_user())] + pub user: String, + #[envconfig(from = "DB_PASS", default = "")] + pub password: String, + #[envconfig(from = "DB_NAME", default = "mastodon_development")] + pub database: String, + #[envconfig(from = "DB_HOST", default = "localhost")] + pub host: String, + #[envconfig(from = "DB_PORT", default = "5432")] + pub port: u16, +} + +#[derive(Envconfig)] +pub struct ServerConfig { + #[envconfig(from = "BIND", default = "0.0.0.0")] + pub address: IpAddr, + #[envconfig(from = "PORT", default = "4000")] + pub port: u16, +} + +#[derive(Envconfig)] +pub struct RedisConfig { + #[envconfig(from = "REDIS_HOST", default = "127.0.0.1")] + pub host: IpAddr, + #[envconfig(from = "REDIS_PORT", default = "6379")] + pub port: u16, + #[envconfig(from = "REDIS_DB", default = "0")] + pub db: u16, + #[envconfig(from = "REDIS_PASSWORD", default = "")] + pub password: String, +} diff --git a/src/main.rs b/src/main.rs index 10c1ffd..42dd0df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,22 @@ +#[macro_use] +extern crate envconfig_derive; + mod api; mod common; +mod env; mod middleware; use actix::prelude::*; use actix_redis::RedisActor; -use actix_web::{ - http::{header, Method}, - middleware::cors::Cors, - server, App, HttpResponse, -}; +use actix_web::{http::header, middleware::cors::Cors, server, App, HttpResponse}; +use env::ServerConfig; use env_logger::Builder; +use envconfig::Envconfig; use log::info; use std::net::SocketAddr; -use structopt::StructOpt; const ENV_LOG_VARIABLE: &str = "STREAMING_API_LOG"; -#[derive(StructOpt)] -struct Opt { - #[structopt(short, long, default_value = "3666")] - port: u16, -} - #[derive(Clone)] pub struct AppState { redis: Addr, @@ -30,11 +25,10 @@ pub struct AppState { fn main() { Builder::from_env(ENV_LOG_VARIABLE).init(); - let args = Opt::from_args(); - info!("starting streaming api server"); - let addr: SocketAddr = ([127, 0, 0, 1], args.port).into(); + let server_cfg = ServerConfig::init().expect("failed to obtain server environment"); + let addr = SocketAddr::new(server_cfg.address, server_cfg.port); let sys = System::new("streaming-api-server");