IT WORKS
This commit is contained in:
parent
d92a5b7004
commit
f357316797
3 changed files with 21 additions and 33 deletions
|
@ -7,10 +7,10 @@ use actix_web::{
|
||||||
use reqwest::{Client, Url, UrlError};
|
use reqwest::{Client, Url, UrlError};
|
||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::sync::{Arc, MutexGuard, RwLock, RwLockWriteGuard};
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Default)]
|
#[derive(Clone, Serialize, Default, Debug)]
|
||||||
pub struct Status {
|
pub struct Status {
|
||||||
status: u8,
|
status: u8,
|
||||||
location: String,
|
location: String,
|
||||||
|
@ -19,7 +19,7 @@ pub struct Status {
|
||||||
error: Option<String>,
|
error: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct QueryResults {
|
pub struct QueryResults {
|
||||||
pub last_update: EpochTimestamp,
|
pub last_update: EpochTimestamp,
|
||||||
pub refresh_time: u64,
|
pub refresh_time: u64,
|
||||||
|
@ -27,25 +27,10 @@ pub struct QueryResults {
|
||||||
pub statuses: Vec<Status>,
|
pub statuses: Vec<Status>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type State = Arc<Mutex<QueryResults>>;
|
type State = Arc<RwLock<QueryResults>>;
|
||||||
|
|
||||||
// impl State {
|
|
||||||
// pub fn new(config: &Config) -> Self {
|
|
||||||
// State(Arc::from(Mutex::from(QueryResults {
|
|
||||||
// last_update: EpochTimestamp::now(),
|
|
||||||
// refresh_time: config.refresh_time.clone(),
|
|
||||||
// config: config.clone(),
|
|
||||||
// statuses: vec![],
|
|
||||||
// })))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fn lock(&self) -> MutexGuard<QueryResults> {
|
|
||||||
// self.0.lock().unwrap()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, WebError> {
|
||||||
let state = update_state(state.lock().unwrap());
|
let state = state.read().unwrap();
|
||||||
let mut ctx = Context::new();
|
let mut ctx = Context::new();
|
||||||
ctx.insert("results", &*state);
|
ctx.insert("results", &*state);
|
||||||
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
||||||
|
@ -56,11 +41,11 @@ pub fn index(tmpl: Data<Tera>, state: Data<State>) -> WebResult<HttpResponse, We
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
||||||
let state = update_state(state.lock().unwrap());
|
let state = state.read().unwrap();
|
||||||
HttpResponse::Ok().json(&state.statuses)
|
HttpResponse::Ok().json(&state.statuses)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_state(mut state: MutexGuard<QueryResults>) -> MutexGuard<QueryResults> {
|
pub fn update_state(mut state: RwLockWriteGuard<QueryResults>) -> RwLockWriteGuard<QueryResults> {
|
||||||
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
||||||
state.last_update = EpochTimestamp::now();
|
state.last_update = EpochTimestamp::now();
|
||||||
state.statuses = update_status(&state.config);
|
state.statuses = update_status(&state.config);
|
||||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -19,9 +19,8 @@ use actix::System;
|
||||||
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
|
||||||
fs::read_to_string,
|
fs::read_to_string,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, RwLock},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tokio::prelude::{Future, Stream};
|
use tokio::prelude::{Future, Stream};
|
||||||
|
@ -29,25 +28,28 @@ use tokio::timer::Interval;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
System::run(move || {
|
System::run(move || {
|
||||||
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron").expect("Could not find config file")).unwrap();
|
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();
|
let bind_addr = config.bind_address.clone();
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let state = Arc::from(Mutex::from(QueryResults {
|
let state = Arc::new(RwLock::new(QueryResults {
|
||||||
last_update: EpochTimestamp::now(),
|
last_update: EpochTimestamp::now(),
|
||||||
refresh_time: config.refresh_time.clone(),
|
refresh_time: config.refresh_time.clone(),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
statuses: vec![],
|
statuses: vec![],
|
||||||
}));
|
}));
|
||||||
let state1 = state.clone();
|
let clone_state = Arc::clone(&state);
|
||||||
let state2 = state.clone();
|
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
||||||
|
let state = Arc::clone(&state);
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.data(state1)
|
.data(state)
|
||||||
.data(tera)
|
.data(tera)
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.service(resource("/").to(index))
|
.service(resource("/").to(index))
|
||||||
|
@ -59,11 +61,12 @@ fn main() {
|
||||||
|
|
||||||
tokio::spawn(
|
tokio::spawn(
|
||||||
Interval::new_interval(Duration::from_millis(5000))
|
Interval::new_interval(Duration::from_millis(5000))
|
||||||
.for_each(|_| {
|
.for_each(move |_| {
|
||||||
println!("Every 5 seconds");
|
let _ = update_state(clone_state.write().unwrap());
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|_| ()),
|
.map_err(|_| ()),
|
||||||
);
|
);
|
||||||
}).expect("Could not run system!");
|
})
|
||||||
|
.expect("Could not run system!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::{
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(PartialEq, PartialOrd, Copy, Clone)]
|
#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
|
||||||
pub struct EpochTimestamp(SystemTime);
|
pub struct EpochTimestamp(SystemTime);
|
||||||
|
|
||||||
impl EpochTimestamp {
|
impl EpochTimestamp {
|
||||||
|
|
Loading…
Reference in a new issue