Skip to content

Commit

Permalink
Add index page
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed May 14, 2024
1 parent 76bcc99 commit f0f1fc2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 13 deletions.
30 changes: 20 additions & 10 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,30 @@ pub enum CrawlerError {
}

#[derive(Clone, Debug)]
enum CrawledInstanceStatus {
pub enum CrawledInstanceStatus {
Ok(Duration),
TimedOut,
Unknown,
}

impl std::fmt::Display for CrawledInstanceStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Clone, Debug)]
struct CrawledInstance {
url: Url,
status: CrawledInstanceStatus,
pub struct CrawledInstance {
pub url: Url,
pub status: CrawledInstanceStatus,
}

#[derive(Clone, Debug)]
struct CrawledService {
name: String,
fallback_url: Url,
aliases: HashSet<String>,
instances: Vec<CrawledInstance>,
pub struct CrawledService {
pub name: String,
pub fallback_url: Url,
pub aliases: HashSet<String>,
pub instances: Vec<CrawledInstance>,
}

impl CrawledService {
Expand All @@ -58,7 +64,7 @@ impl CrawledService {
}

#[derive(Clone, Debug)]
struct CrawledServices(Vec<CrawledService>);
pub struct CrawledServices(pub Vec<CrawledService>);

impl CrawledServices {
fn get_service_by_alias(&self, alias: &str) -> Option<&CrawledService> {
Expand Down Expand Up @@ -90,6 +96,10 @@ impl Crawler {
}
}

pub async fn read(&self) -> tokio::sync::RwLockReadGuard<Option<CrawledServices>> {
self.data.read().await
}

pub async fn get_redirect_url_for_service(
&self,
alias: &str,
Expand Down
22 changes: 19 additions & 3 deletions src/routes/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use url::Url;

use crate::{
config::AppConfig,
crawler::{Crawler, CrawlerError},
crawler::{CrawledService, Crawler, CrawlerError},
errors::impl_api_error,
};

Expand All @@ -39,9 +39,25 @@ impl_api_error!(RedirectError,
}
);

#[derive(Template)]
#[template(path = "index.html")]
pub struct IndexTemplate<'a> {
pub services: &'a Vec<CrawledService>,
}

#[get("/")]
async fn index() -> actix_web::Result<impl Responder> {
Ok(Redirect::to("https://github.com/cofob/fastside").permanent())
async fn index(crawler: web::Data<Arc<Crawler>>) -> actix_web::Result<impl Responder> {
let data = crawler.read().await;
let Some(services) = data.as_ref() else {
return Err(RedirectError::from(CrawlerError::CrawlerNotFetchedYet))?;
};
let template = IndexTemplate {
services: &services.0,
};

Ok(actix_web::HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(template.render().expect("failed to render error page")))
}

#[derive(Template)]
Expand Down
76 changes: 76 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<head>
<title>Fastside</title>
<style>
html {
font-family: monospace;
font-size: 16px;
color: #66397C;
}

#parent-div {
text-align: center;
}

#child-div {
text-align: left;
width: 50%;
display: inline-block;
}

hr {
border: 1px dashed;
}

a:link,
a:visited {
color: #66397C;
}

@media only screen and (max-width: 1000px) {
#child-div {
width: 90%;
}
}

ul {
margin: 10px;
}

@media (prefers-color-scheme: dark) {
html {
color: #fff;
background: #121517;
}

a:link,
a:visited {
color: #AA8AC1;
}
}
</style>
</head>

<body>
<div id="parent-div">
<div id="child-div">
<h1>Fastside [<a href="https://github.com/cofob/fastside">GitHub</a>]</h1>
<hr>
<div>
<ul>
{% for service in services %}
<li><a href="/{{ service.name }}/">{{ service.name }}</a> Aliases: [{% for alias in service.aliases
%}<code>{{ alias }}, </code>{% endfor %}]</li>
<ul>
{% for instance in service.instances %}
<li>
<a href="{{ instance.url }}">{{ instance.url }}</a> <span>Status:
<code>{{ instance.status }}</code></span>
</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</div>
</div>
</div>
</body>

0 comments on commit f0f1fc2

Please sign in to comment.