endstat/src/main.rs

79 lines
2.0 KiB
Rust
Raw Normal View History

2019-05-02 22:47:59 +00:00
#![forbid(unsafe_code)]
2019-05-01 06:32:41 +00:00
extern crate actix;
2019-04-30 07:53:13 +00:00
extern crate actix_web;
2019-05-01 21:16:54 +00:00
extern crate chrono;
2019-05-01 02:49:22 +00:00
extern crate env_logger;
2019-04-30 07:53:13 +00:00
extern crate reqwest;
extern crate ron;
extern crate serde;
2019-05-01 06:32:41 +00:00
extern crate tokio;
2019-05-01 02:49:22 +00:00
#[macro_use]
extern crate tera;
2019-05-02 07:21:18 +00:00
extern crate ring;
2019-04-30 07:53:13 +00:00
2019-05-01 05:28:57 +00:00
mod config;
2019-05-01 05:41:52 +00:00
mod handlers;
2019-05-03 04:54:43 +00:00
mod results;
2019-05-03 05:25:56 +00:00
mod updater;
2019-05-01 02:49:22 +00:00
2019-05-03 05:25:56 +00:00
use self::{config::*, handlers::*, results::QueryResults, updater::*};
2019-05-01 06:32:41 +00:00
use actix::System;
2019-05-01 05:41:52 +00:00
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
2019-04-30 05:44:26 +00:00
use ron::de::from_str;
2019-05-01 15:38:36 +00:00
use std::{
env::var,
2019-05-01 15:38:36 +00:00
fs::read_to_string,
2019-05-01 16:58:10 +00:00
sync::{Arc, RwLock},
2019-05-01 15:38:36 +00:00
time::Duration,
};
2019-05-01 18:13:46 +00:00
use tokio::{
prelude::{Future, Stream},
timer::Interval,
};
pub type State = Arc<RwLock<QueryResults>>;
2019-04-30 08:20:01 +00:00
2019-05-01 15:38:36 +00:00
fn main() {
System::run(|| {
let conf_file_loc =
2019-05-02 05:51:27 +00:00
var("ENDSTAT_CONF").unwrap_or_else(|_| String::from("./config/endstat_conf.ron"));
2019-05-01 16:58:10 +00:00
let config = from_str::<Config>(
&read_to_string(&conf_file_loc).expect(&format!("finding {}", conf_file_loc)),
2019-05-01 16:58:10 +00:00
)
.unwrap();
let bind_addr = &config.bind_address;
2019-05-01 15:38:36 +00:00
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let state = Arc::new(RwLock::new(QueryResults::new(config.clone())));
2019-05-01 20:01:27 +00:00
2019-05-01 16:58:10 +00:00
let clone_state = Arc::clone(&state);
2019-05-01 15:38:36 +00:00
2019-05-01 06:32:41 +00:00
HttpServer::new(move || {
2019-05-03 05:25:56 +00:00
let tera = compile_templates!("./templates/**/*");
2019-05-01 16:58:10 +00:00
let state = Arc::clone(&state);
2019-05-01 02:49:22 +00:00
2019-05-01 06:32:41 +00:00
App::new()
2019-05-01 16:58:10 +00:00
.data(state)
2019-05-01 06:32:41 +00:00
.data(tera)
.wrap(Logger::default())
.service(resource("/").to(index))
.service(resource("/api").to(json_endpoint))
})
.bind(&bind_addr)
.expect("Could not bind to address!")
.start();
tokio::spawn(
2019-05-01 18:13:46 +00:00
Interval::new_interval(Duration::from_secs(config.refresh_time))
2019-05-01 16:58:10 +00:00
.for_each(move |_| {
2019-05-01 21:16:54 +00:00
update_state(Arc::clone(&clone_state));
2019-05-01 06:32:41 +00:00
Ok(())
})
.map_err(|_| ()),
);
2019-05-01 16:58:10 +00:00
})
.expect("Could not run system!");
2019-04-30 06:47:35 +00:00
}