Read from environment configuration
This commit is contained in:
69
src/main.rs
69
src/main.rs
@@ -6,8 +6,8 @@ extern crate actix_web;
|
|||||||
// Logging & Console output.
|
// Logging & Console output.
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate pretty_env_logger;
|
|
||||||
|
|
||||||
|
use std::process::exit;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
@@ -18,15 +18,70 @@ fn into_bool(string: &str) -> bool {
|
|||||||
string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")
|
string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// std::env::set_var("RUST_LOG", "info");
|
// If RUST_LOG is unset, set it to INFO, otherwise keep it as-is.
|
||||||
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
|
std::env::set_var("RUST_LOG", "info");
|
||||||
|
}
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
//TODO Retrieve from environment values, with fall back to defaults if unset.
|
if std::env::var("MERCATOR_HOST").is_err() {
|
||||||
let hostname = "0.0.0.0";
|
std::env::set_var("MERCATOR_HOST", "0.0.0.0");
|
||||||
let base = "/spatial-search";
|
}
|
||||||
let port = 8888;
|
|
||||||
|
if std::env::var("MERCATOR_PORT").is_err() {
|
||||||
|
std::env::set_var("MERCATOR_PORT", "8888");
|
||||||
|
}
|
||||||
|
|
||||||
|
if std::env::var("MERCATOR_BASE").is_err() {
|
||||||
|
std::env::set_var("MERCATOR_BASE", "/spatial-search");
|
||||||
|
}
|
||||||
|
|
||||||
|
if std::env::var("MERCATOR_DATA").is_err() {
|
||||||
|
std::env::set_var("MERCATOR_DATA", ".");
|
||||||
|
}
|
||||||
|
let hostname;
|
||||||
|
let port;
|
||||||
|
let base;
|
||||||
|
let data;
|
||||||
|
|
||||||
|
match std::env::var("MERCATOR_HOST") {
|
||||||
|
Ok(val) => hostname = val,
|
||||||
|
Err(val) => {
|
||||||
|
error!("Invalid environment {} : `{}`", "MERCATOR_HOST", val);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match std::env::var("MERCATOR_PORT") {
|
||||||
|
Ok(val) => match val.parse::<u16>() {
|
||||||
|
Ok(v) => port = v,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not convert to u16 {} : `{}`", "MERCATOR_PORT", e);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(val) => {
|
||||||
|
error!("Could not fetch {} : `{}`", "MERCATOR_PORT", val);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match std::env::var("MERCATOR_BASE") {
|
||||||
|
Ok(val) => base = val,
|
||||||
|
Err(val) => {
|
||||||
|
error!("Could not fetch {} : `{}`", "MERCATOR_BASE", val);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match std::env::var("MERCATOR_DATA") {
|
||||||
|
Ok(val) => data = val,
|
||||||
|
Err(val) => {
|
||||||
|
error!("Could not fetch {} : `{}`", "MERCATOR_DATA", val);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
rest_api::run(hostname, port, base, Arc::new(RwLock::new(0)));
|
rest_api::run(hostname, port, base, Arc::new(RwLock::new(0)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,13 +41,24 @@ mod spatial_objects;
|
|||||||
|
|
||||||
mod default;
|
mod default;
|
||||||
|
|
||||||
fn get_app(
|
// From: https://stackoverflow.com/a/52367953
|
||||||
prefix: &'static str,
|
fn into_static<S>(s: S) -> &'static str
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
Box::leak(s.into().into_boxed_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_app<S>(
|
||||||
|
prefix: S,
|
||||||
state: Arc<RwLock<SharedState>>,
|
state: Arc<RwLock<SharedState>>,
|
||||||
) -> Vec<Box<HttpHandler<Task = Box<HttpHandlerTask>>>> {
|
) -> Vec<Box<HttpHandler<Task = Box<HttpHandlerTask>>>>
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
vec![
|
vec![
|
||||||
App::with_state(AppState { shared: state })
|
App::with_state(AppState { shared: state })
|
||||||
.prefix(format!("{}", prefix))
|
.prefix(format!("{}", into_static(prefix)))
|
||||||
// ACTIONS -------------------------------------------------------------------
|
// ACTIONS -------------------------------------------------------------------
|
||||||
.resource("/health", |r| {
|
.resource("/health", |r| {
|
||||||
r.method(Method::GET).f(actions::health);
|
r.method(Method::GET).f(actions::health);
|
||||||
@@ -119,15 +130,15 @@ fn get_app(
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
pub fn run<S>(host: S, port: u16, prefix: S, state: Arc<RwLock<SharedState>>) -> ()
|
||||||
host: &'static str,
|
where
|
||||||
port: u16,
|
S: Into<String>,
|
||||||
prefix: &'static str,
|
{
|
||||||
state: Arc<RwLock<SharedState>>,
|
|
||||||
) -> () {
|
|
||||||
info!("Initializing server...");
|
info!("Initializing server...");
|
||||||
|
|
||||||
let sys = actix::System::new("spatial-search");
|
let sys = actix::System::new("spatial-search");
|
||||||
|
let prefix = into_static(prefix);
|
||||||
|
let host = host.into();
|
||||||
|
|
||||||
server::new(move || get_app(prefix, state.clone()))
|
server::new(move || get_app(prefix, state.clone()))
|
||||||
.bind(format!("{}:{}", host, port))
|
.bind(format!("{}:{}", host, port))
|
||||||
|
|||||||
Reference in New Issue
Block a user