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 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 06:32:41 +00:00
|
|
|
use std::{error::Error, fs::read_to_string, time::Duration};
|
|
|
|
use tokio::prelude::{Future, Stream};
|
|
|
|
use tokio::timer::Interval;
|
2019-04-30 08:20:01 +00:00
|
|
|
|
2019-04-30 05:44:26 +00:00
|
|
|
fn main() -> Result<(), Box<Error>> {
|
|
|
|
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?;
|
2019-05-01 02:49:22 +00:00
|
|
|
let bind_addr = config.bind_address.clone();
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
env_logger::init();
|
2019-04-30 06:47:35 +00:00
|
|
|
|
2019-05-01 06:32:41 +00:00
|
|
|
System::run(move || {
|
|
|
|
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()
|
|
|
|
.data(State::new(&config))
|
|
|
|
.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-04-30 06:47:35 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|