made type for state
This commit is contained in:
parent
cef4ce19f0
commit
53b4cd70b0
2 changed files with 24 additions and 20 deletions
|
@ -20,17 +20,32 @@ pub struct Status {
|
|||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct FetchResults {
|
||||
pub struct QueryResults {
|
||||
pub last_update: EpochTimestamp,
|
||||
pub refresh_time: u64,
|
||||
pub config: Config,
|
||||
pub statuses: Vec<Status>,
|
||||
}
|
||||
|
||||
type StatusState = Arc<Mutex<FetchResults>>;
|
||||
pub struct State(Arc<Mutex<QueryResults>>);
|
||||
|
||||
pub fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpResponse, WebError> {
|
||||
let state = update_state(state.lock().unwrap());
|
||||
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> {
|
||||
let state = update_state(state.lock());
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("results", &*state);
|
||||
let s = tmpl.render("index.html", &ctx).map_err(|e| {
|
||||
|
@ -40,12 +55,12 @@ pub fn index(tmpl: Data<Tera>, state: Data<StatusState>) -> WebResult<HttpRespon
|
|||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
pub fn json_endpoint(state: Data<StatusState>) -> HttpResponse {
|
||||
let state = update_state(state.lock().unwrap());
|
||||
pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
||||
let state = update_state(state.lock());
|
||||
HttpResponse::Ok().json(&state.statuses)
|
||||
}
|
||||
|
||||
pub fn update_state(mut state: MutexGuard<FetchResults>) -> MutexGuard<FetchResults> {
|
||||
fn update_state(mut state: MutexGuard<QueryResults>) -> MutexGuard<QueryResults> {
|
||||
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
||||
state.last_update = EpochTimestamp::now();
|
||||
state.statuses = update_status(&state.config);
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -12,14 +12,9 @@ mod utils;
|
|||
|
||||
use self::config::*;
|
||||
use self::handlers::*;
|
||||
use self::utils::EpochTimestamp;
|
||||
use actix_web::{middleware::Logger, web::resource, App, HttpServer};
|
||||
use ron::de::from_str;
|
||||
use std::{
|
||||
error::Error,
|
||||
fs::read_to_string,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::{error::Error, fs::read_to_string};
|
||||
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
let config = from_str::<Config>(&read_to_string("./endstat_conf.ron")?)?;
|
||||
|
@ -28,16 +23,10 @@ fn main() -> Result<(), Box<Error>> {
|
|||
env_logger::init();
|
||||
|
||||
HttpServer::new(move || {
|
||||
let state = Arc::from(Mutex::from(FetchResults {
|
||||
last_update: EpochTimestamp::now(),
|
||||
refresh_time: config.refresh_time.clone(),
|
||||
config: config.clone(),
|
||||
statuses: vec![],
|
||||
}));
|
||||
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
||||
|
||||
App::new()
|
||||
.data(state)
|
||||
.data(State::new(&config))
|
||||
.data(tera)
|
||||
.wrap(Logger::default())
|
||||
.service(resource("/").to(index))
|
||||
|
|
Loading…
Reference in a new issue