endstat/src/main.rs

82 lines
2.2 KiB
Rust
Raw Normal View History

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-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-01 18:13:46 +00:00
mod updater;
2019-05-01 02:49:22 +00:00
2019-05-01 20:01:27 +00:00
use self::{config::*, handlers::*, 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-05-01 20:01:27 +00:00
use chrono::prelude::*;
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() {
2019-05-01 06:32:41 +00:00
System::run(move || {
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();
2019-05-01 15:38:36 +00:00
let bind_addr = config.bind_address.clone();
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
2019-05-01 18:13:46 +00:00
let state: State = Arc::new(RwLock::new(QueryResults {
2019-05-01 20:15:18 +00:00
timestamp: Utc::now(),
timestamp_str: Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
2019-05-01 15:38:36 +00:00
refresh_time: config.refresh_time.clone(),
config: config.clone(),
2019-05-01 20:59:55 +00:00
groups: update_status(&config),
2019-05-01 15:38:36 +00:00
}));
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-02 05:51:27 +00:00
let tera = compile_templates!("./config/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
}