73 lines
2.0 KiB
Rust
73 lines
2.0 KiB
Rust
extern crate actix;
|
|
extern crate actix_web;
|
|
extern crate env_logger;
|
|
extern crate reqwest;
|
|
extern crate ron;
|
|
extern crate serde;
|
|
extern crate tokio;
|
|
#[macro_use]
|
|
extern crate tera;
|
|
|
|
mod config;
|
|
mod handlers;
|
|
mod utils;
|
|
|
|
use self::config::*;
|
|
use self::handlers::*;
|
|
use self::utils::EpochTimestamp;
|
|
use actix::System;
|
|
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
|
use ron::de::from_str;
|
|
use std::{
|
|
fs::read_to_string,
|
|
sync::{Arc, RwLock},
|
|
time::Duration,
|
|
};
|
|
use tokio::prelude::{Future, Stream};
|
|
use tokio::timer::Interval;
|
|
|
|
fn main() {
|
|
System::run(move || {
|
|
let config = from_str::<Config>(
|
|
&read_to_string("./endstat_conf.ron").expect("Could not find config file"),
|
|
)
|
|
.unwrap();
|
|
let bind_addr = config.bind_address.clone();
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
env_logger::init();
|
|
|
|
let state = Arc::new(RwLock::new(QueryResults {
|
|
last_update: EpochTimestamp::now(),
|
|
refresh_time: config.refresh_time.clone(),
|
|
config: config.clone(),
|
|
statuses: vec![],
|
|
}));
|
|
let clone_state = Arc::clone(&state);
|
|
|
|
HttpServer::new(move || {
|
|
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
|
let state = Arc::clone(&state);
|
|
|
|
App::new()
|
|
.data(state)
|
|
.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(
|
|
Interval::new_interval(Duration::from_millis(5000))
|
|
.for_each(move |_| {
|
|
let _ = update_state(clone_state.write().unwrap());
|
|
Ok(())
|
|
})
|
|
.map_err(|_| ()),
|
|
);
|
|
})
|
|
.expect("Could not run system!");
|
|
}
|