non-working code

master
Edward Shen 2019-05-01 11:38:36 -04:00
parent 92036fe55f
commit d92a5b7004
Signed by: edward
GPG Key ID: F350507060ED6C90
6 changed files with 44 additions and 28 deletions

1
Cargo.lock generated
View File

@ -654,6 +654,7 @@ dependencies = [
"actix 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -13,3 +13,4 @@ actix = "0.8"
tokio = "0.1" tokio = "0.1"
tera = "0.11" tera = "0.11"
env_logger = "0.6" env_logger = "0.6"
lazy_static = "1.3.0"

View File

@ -16,7 +16,7 @@ pub struct WebsiteConfig {
pub endpoints: Vec<EndpointConfig>, pub endpoints: Vec<EndpointConfig>,
} }
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct Config { pub struct Config {
pub refresh_time: u64, pub refresh_time: u64,
pub bind_address: String, pub bind_address: String,

View File

@ -27,25 +27,25 @@ pub struct QueryResults {
pub statuses: Vec<Status>, pub statuses: Vec<Status>,
} }
pub struct State(Arc<Mutex<QueryResults>>); type State = Arc<Mutex<QueryResults>>;
impl State { // impl State {
pub fn new(config: &Config) -> Self { // pub fn new(config: &Config) -> Self {
State(Arc::from(Mutex::from(QueryResults { // State(Arc::from(Mutex::from(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![],
}))) // })))
} // }
fn lock(&self) -> MutexGuard<QueryResults> { // fn lock(&self) -> MutexGuard<QueryResults> {
self.0.lock().unwrap() // 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()); let state = update_state(state.lock().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,7 +56,7 @@ 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()); let state = update_state(state.lock().unwrap());
HttpResponse::Ok().json(&state.statuses) HttpResponse::Ok().json(&state.statuses)
} }

View File

@ -14,25 +14,40 @@ mod utils;
use self::config::*; use self::config::*;
use self::handlers::*; use self::handlers::*;
use self::utils::EpochTimestamp;
use actix::System; 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::{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::prelude::{Future, Stream};
use tokio::timer::Interval; use tokio::timer::Interval;
fn main() -> Result<(), Box<Error>> { fn main() {
let config = from_str::<Config>(&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();
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 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 || { HttpServer::new(move || {
let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
App::new() App::new()
.data(State::new(&config)) .data(state1)
.data(tera) .data(tera)
.wrap(Logger::default()) .wrap(Logger::default())
.service(resource("/").to(index)) .service(resource("/").to(index))
@ -50,7 +65,5 @@ fn main() -> Result<(), Box<Error>> {
}) })
.map_err(|_| ()), .map_err(|_| ()),
); );
})?; }).expect("Could not run system!");
Ok(())
} }

View File

@ -42,6 +42,7 @@
<body> <body>
<main> <main>
<h1>Welcome!</h1> <h1>Welcome!</h1>
<h1>{{ results.last_update }}</h1>
{% for status in results.statuses -%} {% for status in results.statuses -%}
<section> <section>
<p>{{ status.domain }}</p> <p>{{ status.domain }}</p>