refactor to use chrono
This commit is contained in:
parent
fef8fd66c7
commit
0e8abe2701
6 changed files with 16 additions and 60 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -426,6 +426,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -653,8 +654,8 @@ version = "0.1.0"
|
||||||
dependencies = [
|
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)",
|
||||||
|
"chrono 0.4.6 (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)",
|
||||||
|
|
|
@ -13,4 +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"
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::{config::*, utils::EpochTimestamp, State};
|
use crate::{config::*, State};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
error::ErrorInternalServerError, web::Data, Error as WebError, HttpResponse,
|
||||||
Result as WebResult,
|
Result as WebResult,
|
||||||
};
|
};
|
||||||
|
use chrono::prelude::*;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ pub struct Status {
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct QueryResults {
|
pub struct QueryResults {
|
||||||
pub last_update: EpochTimestamp,
|
pub last_update: DateTime<Utc>,
|
||||||
pub refresh_time: u64,
|
pub refresh_time: u64,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub statuses: Vec<Status>,
|
pub statuses: Vec<Status>,
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -7,15 +7,16 @@ extern crate serde;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate tera;
|
extern crate tera;
|
||||||
|
extern crate chrono;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
mod updater;
|
mod updater;
|
||||||
mod utils;
|
|
||||||
|
|
||||||
use self::{config::*, handlers::*, updater::update_state, utils::EpochTimestamp};
|
use self::{config::*, handlers::*, updater::*};
|
||||||
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 chrono::prelude::*;
|
||||||
use ron::de::from_str;
|
use ron::de::from_str;
|
||||||
use std::{
|
use std::{
|
||||||
fs::read_to_string,
|
fs::read_to_string,
|
||||||
|
@ -40,11 +41,12 @@ fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let state: State = Arc::new(RwLock::new(QueryResults {
|
let state: State = Arc::new(RwLock::new(QueryResults {
|
||||||
last_update: EpochTimestamp::now(),
|
last_update: Utc::now(),
|
||||||
refresh_time: config.refresh_time.clone(),
|
refresh_time: config.refresh_time.clone(),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
statuses: vec![],
|
statuses: update_status(&config),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let clone_state = Arc::clone(&state);
|
let clone_state = Arc::clone(&state);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
use crate::{config::*, handlers::Status, utils::EpochTimestamp, State};
|
use crate::{config::*, handlers::Status, State};
|
||||||
|
use chrono::prelude::*;
|
||||||
use reqwest::{Client, Url, UrlError};
|
use reqwest::{Client, Url, UrlError};
|
||||||
|
|
||||||
pub fn update_state(state: State) {
|
pub fn update_state(state: State) {
|
||||||
let new_statuses = { Some(update_status(&state.read().unwrap().config)) };
|
let new_statuses = { Some(update_status(&state.read().unwrap().config)) };
|
||||||
let mut write_state = state.try_write().expect("Could not unlock");
|
let mut write_state = state.try_write().expect("Could not unlock");
|
||||||
write_state.last_update = EpochTimestamp::now();
|
write_state.last_update = Utc::now();
|
||||||
write_state.statuses = new_statuses.unwrap();
|
write_state.statuses = new_statuses.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_status(config: &Config) -> Vec<Status> {
|
pub fn update_status(config: &Config) -> Vec<Status> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let mut results: Vec<Status> = vec![];
|
let mut results: Vec<Status> = vec![];
|
||||||
|
|
||||||
|
|
49
src/utils.rs
49
src/utils.rs
|
@ -1,49 +0,0 @@
|
||||||
use serde::{Serialize, Serializer};
|
|
||||||
use std::{
|
|
||||||
fmt::{Display, Formatter, Result as FmtResult},
|
|
||||||
ops::Sub,
|
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
|
|
||||||
pub struct EpochTimestamp(SystemTime);
|
|
||||||
|
|
||||||
impl EpochTimestamp {
|
|
||||||
pub fn now() -> Self {
|
|
||||||
EpochTimestamp(SystemTime::now())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for EpochTimestamp {
|
|
||||||
type Output = u64;
|
|
||||||
fn sub(self, other: EpochTimestamp) -> u64 {
|
|
||||||
self.0.duration_since(other.0).unwrap_or_default().as_secs()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for EpochTimestamp {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}",
|
|
||||||
self.0
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.unwrap_or_default()
|
|
||||||
.as_secs()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for EpochTimestamp {
|
|
||||||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
s.serialize_u64(
|
|
||||||
self.0
|
|
||||||
.duration_since(SystemTime::from(UNIX_EPOCH))
|
|
||||||
.unwrap_or_default()
|
|
||||||
.as_secs(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue