endstat/src/main.rs

70 lines
1.9 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 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 02:49:22 +00:00
mod utils;
2019-05-01 05:28:57 +00:00
use self::config::*;
2019-05-01 05:41:52 +00:00
use self::handlers::*;
2019-05-01 15:38:36 +00:00
use self::utils::EpochTimestamp;
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::{
error::Error,
fs::read_to_string,
sync::{Arc, Mutex},
time::Duration,
};
2019-05-01 06:32:41 +00:00
use tokio::prelude::{Future, Stream};
use tokio::timer::Interval;
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 || {
2019-05-01 15:38:36 +00:00
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::from(Mutex::from(QueryResults {
last_update: EpochTimestamp::now(),
refresh_time: config.refresh_time.clone(),
config: config.clone(),
statuses: vec![],
}));
let state1 = state.clone();
let state2 = state.clone();
2019-05-01 06:32:41 +00:00
HttpServer::new(move || {
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
2019-05-01 02:49:22 +00:00
2019-05-01 06:32:41 +00:00
App::new()
2019-05-01 15:38:36 +00:00
.data(state1)
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(
Interval::new_interval(Duration::from_millis(5000))
.for_each(|_| {
println!("Every 5 seconds");
Ok(())
})
.map_err(|_| ()),
);
2019-05-01 15:38:36 +00:00
}).expect("Could not run system!");
2019-04-30 06:47:35 +00:00
}