From d92a5b7004b62cd999f35ce94c7e86f10b1d7213 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Wed, 1 May 2019 11:38:36 -0400 Subject: [PATCH] non-working code --- Cargo.lock | 1 + Cargo.toml | 1 + src/config.rs | 2 +- src/handlers.rs | 32 ++++++++++++++++---------------- src/main.rs | 35 ++++++++++++++++++++++++----------- templates/index.html | 1 + 6 files changed, 44 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bcf80e4..e552698 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,6 +654,7 @@ dependencies = [ "actix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 1.0.0-beta.2 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)", "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 1288c82..dafc294 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ actix = "0.8" tokio = "0.1" tera = "0.11" env_logger = "0.6" +lazy_static = "1.3.0" diff --git a/src/config.rs b/src/config.rs index 2a7aacd..45e8144 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,7 +16,7 @@ pub struct WebsiteConfig { pub endpoints: Vec, } -#[derive(Deserialize, Serialize, Debug, Clone)] +#[derive(Deserialize, Serialize, Debug, Clone, Default)] pub struct Config { pub refresh_time: u64, pub bind_address: String, diff --git a/src/handlers.rs b/src/handlers.rs index 10b22bc..f55a15c 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -27,25 +27,25 @@ pub struct QueryResults { pub statuses: Vec, } -pub struct State(Arc>); +type State = Arc>; -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![], - }))) - } +// 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 { - self.0.lock().unwrap() - } -} +// fn lock(&self) -> MutexGuard { +// self.0.lock().unwrap() +// } +// } pub fn index(tmpl: Data, state: Data) -> WebResult { - let state = update_state(state.lock()); + let state = update_state(state.lock().unwrap()); let mut ctx = Context::new(); ctx.insert("results", &*state); let s = tmpl.render("index.html", &ctx).map_err(|e| { @@ -56,7 +56,7 @@ pub fn index(tmpl: Data, state: Data) -> WebResult) -> HttpResponse { - let state = update_state(state.lock()); + let state = update_state(state.lock().unwrap()); HttpResponse::Ok().json(&state.statuses) } diff --git a/src/main.rs b/src/main.rs index 4dc73f4..df31677 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,25 +14,40 @@ 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::{error::Error, fs::read_to_string, time::Duration}; +use std::{ + error::Error, + fs::read_to_string, + sync::{Arc, Mutex}, + time::Duration, +}; use tokio::prelude::{Future, Stream}; use tokio::timer::Interval; -fn main() -> Result<(), Box> { - let config = from_str::(&read_to_string("./endstat_conf.ron")?)?; - let bind_addr = config.bind_address.clone(); - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); - +fn main() { System::run(move || { + let config = from_str::(&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(); + HttpServer::new(move || { let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); App::new() - .data(State::new(&config)) + .data(state1) .data(tera) .wrap(Logger::default()) .service(resource("/").to(index)) @@ -50,7 +65,5 @@ fn main() -> Result<(), Box> { }) .map_err(|_| ()), ); - })?; - - Ok(()) + }).expect("Could not run system!"); } diff --git a/templates/index.html b/templates/index.html index d459209..e2142ab 100644 --- a/templates/index.html +++ b/templates/index.html @@ -42,6 +42,7 @@

Welcome!

+

{{ results.last_update }}

{% for status in results.statuses -%}

{{ status.domain }}