Skip to content

Commit

Permalink
feat: display monitored urls using askama template
Browse files Browse the repository at this point in the history
  • Loading branch information
piny4man committed Oct 14, 2024
1 parent 5d4a4d8 commit 5625ddf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
askama = "0.12.1"
askama = { version = "0.12.1", features = ["with-axum"] }
askama_axum = "0.4.0"
axum = { version = "0.7.5", features = ["json"] }
chrono = { version = "0.4.38", features = ["serde"] }
Expand All @@ -13,11 +13,11 @@ reqwest = { version = "0.12.5", features = ["json"] }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.122"
sqlx = { version = "0.8.0", features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"chrono",
"json",
"runtime-tokio",
"tls-rustls",
"postgres",
"chrono",
"json",
] }
tokio = { version = "1.39.2", features = ["full"] }
tokio-postgres = "0.7.11"
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ ARG APP_PORT
ARG DATABASE_PUBLIC_URL

RUN apt-get update \
&& apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*
&& apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY static /app/static
COPY templates /app/templates
COPY --from=builder /app/target/release/spyhole /usr/local/bin/spyhole

ENV RUST_LOG=$RUST_LOG
Expand Down
2 changes: 0 additions & 2 deletions askama.toml

This file was deleted.

24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ struct UrlItem {
status: bool,
}

#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate;

#[derive(Template)]
#[template(path = "urls.html")]
struct UrlsTemplate<'a> {
Expand Down Expand Up @@ -119,15 +123,6 @@ async fn get_monitored_urls(State(pool): State<PgPool>) -> impl IntoResponse {
.await
.expect("Failed to fetch monitored URLs");

// let mut response_html = Vec::new();
// for url in monitored_urls {
// response_html.push(url &format!(
// "<li class=\"monitored-url\"><a href=\"{0}\" target=\"_blank\" rel=\"noreferrer\">{0}</a><span class=\"status-badge up-{1}\"/></li>",
// url.url,
// url.status.unwrap_or(false)
// ));
// }
//
let response_urls: Vec<UrlItem> = monitored_urls
.iter()
.map(|url| UrlItem {
Expand All @@ -136,10 +131,14 @@ async fn get_monitored_urls(State(pool): State<PgPool>) -> impl IntoResponse {
})
.collect();

let response_html = UrlsTemplate {
let template = UrlsTemplate {
urls: &response_urls,
};
response_html.render().unwrap()
askama_axum::IntoResponse::into_response(template)
}

async fn index() -> impl IntoResponse {
IndexTemplate.into_response()
}

#[tokio::main]
Expand Down Expand Up @@ -169,7 +168,8 @@ async fn main() {
}

let app = Router::new()
.nest_service("/", ServeDir::new("static"))
.route("/", get(index))
.nest_service("/assets", ServeDir::new("static/assets"))
.route("/monitor", post(monitor_service))
.route("/monitored_urls", get(get_monitored_urls))
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
Expand Down
20 changes: 0 additions & 20 deletions static/urls.html

This file was deleted.

10 changes: 6 additions & 4 deletions static/index.html → templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<title>Spyhole</title>
<script src="https://unpkg.com/[email protected]"></script>
<link rel="stylesheet" href="/assets/styles.css" />
{% block head %}{% endblock %}
</head>

<body>
<main>
<header>
Expand Down Expand Up @@ -36,9 +36,11 @@ <h1>SPYHOLE - Simple down detector</h1>
</svg>
</button>
</header>
{% block content %}
<p>Loading monitored urls...</p>
{% endblock %}
<article>
<ul id="list-container" hx-get="/monitored_urls" hx-trigger="load" hx-swap="innerHTML">
<li class="loading">Loading monitored URLs...</li>
</ul>
</article>
</main>
</body>
</html>
6 changes: 6 additions & 0 deletions templates/urls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% for url in urls %}
<li class="monitored-url">
<a href="{{ url.url }}" target="_blank" rel="noreferrer">{{ url.url }}</a>
<span class="status-badge up-{{ url.status }}"></span>
</li>
{% endfor %}

0 comments on commit 5625ddf

Please sign in to comment.