Skip to content

Commit

Permalink
Add last synced
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed May 14, 2024
1 parent 2adf049 commit 183075c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tokio = { version = "1.37.0", features = ["full"] } # async
futures = "0.3.30" # async
num_cpus = "1.16.0" # get number of cpus
rand = "0.8.5" # random
chrono = "0.4.38" # datetime

[profile.release]
overflow-checks = true
Expand Down
13 changes: 10 additions & 3 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
time::{Duration, SystemTime, UNIX_EPOCH},
};

use chrono::{DateTime, Utc};
use rand::seq::SliceRandom;
use reqwest::StatusCode;
use thiserror::Error;
Expand Down Expand Up @@ -75,11 +76,14 @@ impl CrawledService {
}

#[derive(Clone, Debug)]
pub struct CrawledServices(pub Vec<CrawledService>);
pub struct CrawledServices {
pub services: Vec<CrawledService>,
pub time: DateTime<Utc>,
}

impl CrawledServices {
fn get_service_by_alias(&self, alias: &str) -> Option<&CrawledService> {
self.0
self.services
.iter()
.find(|&service| service.aliases.contains(alias))
}
Expand Down Expand Up @@ -244,7 +248,10 @@ impl Crawler {
if data.is_none() {
info!("Finished initial crawl, we are ready to serve requests");
}
data.replace(CrawledServices(crawled_services));
data.replace(CrawledServices {
services: crawled_services,
time: Utc::now(),
});

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion src/routes/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use actix_web::{
HttpRequest, Responder, Scope,
};
use askama::Template;
use chrono::{DateTime, Utc};
use thiserror::Error;
use url::Url;

Expand Down Expand Up @@ -43,6 +44,7 @@ impl_api_error!(RedirectError,
#[template(path = "index.html")]
pub struct IndexTemplate<'a> {
pub services: &'a Vec<CrawledService>,
pub time: &'a DateTime<Utc>,
}

mod filters {
Expand All @@ -62,7 +64,8 @@ async fn index(crawler: web::Data<Arc<Crawler>>) -> actix_web::Result<impl Respo
return Err(RedirectError::from(CrawlerError::CrawlerNotFetchedYet))?;
};
let template = IndexTemplate {
services: &services.0,
services: &services.services,
time: &services.time,
};

Ok(actix_web::HttpResponse::Ok()
Expand Down
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<div id="child-div">
<h1>Fastside [<a href="https://github.com/cofob/fastside">GitHub</a>]</h1>
<hr>
<h3>Last synced {{ time }}</h3>
<div>
<ul>
{% for service in services %}
Expand Down

0 comments on commit 183075c

Please sign in to comment.