added support for templating and tailwindcss
This commit is contained in:
parent
a87ae3dcd3
commit
dc3b994247
16 changed files with 1668 additions and 70 deletions
|
|
@ -1,12 +1,16 @@
|
|||
use axum::{routing::get, Router};
|
||||
use axum::{routing::get, Router,response::{Html, IntoResponse, Response},http::StatusCode,};
|
||||
use hyper::{Server, http, body};
|
||||
use std::net::SocketAddr;
|
||||
use prometheus::{Encoder, TextEncoder, Registry};
|
||||
use std::sync::Arc;
|
||||
use tower_http::services::ServeDir;
|
||||
use askama::Template;
|
||||
|
||||
pub async fn run_metrics_server(addr: SocketAddr, registry: Arc<Registry>) {
|
||||
let assets_path = std::env::current_dir().unwrap();
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.nest_service("/assets",ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())))
|
||||
.route("/health", get(|| async { "OK" }))
|
||||
.route("/metrics", get(move || {
|
||||
let registry = Arc::clone(®istry);
|
||||
|
|
@ -26,47 +30,34 @@ pub async fn run_metrics_server(addr: SocketAddr, registry: Arc<Registry>) {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
async fn root() -> http::Response<hyper::Body> {
|
||||
let html = r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Library</title>
|
||||
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 >Welcome to the SK Collectors Companion</h1>
|
||||
<p id="data">Click the button below to get the metrics</p>
|
||||
<br />
|
||||
<button id="submit" hx-get="/metrics" hx-target='#data' hx-swap="innerHTML">Get Data</button>
|
||||
</body>
|
||||
<style>
|
||||
body {
|
||||
background-color: #3d3d3d;
|
||||
}
|
||||
h1 {
|
||||
color: white;
|
||||
}
|
||||
#data {
|
||||
color: white;
|
||||
}
|
||||
#submit {
|
||||
background-color: #4CAF50;
|
||||
border: none;
|
||||
color: white;
|
||||
padding: 15px 32px;
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
"#;
|
||||
|
||||
http::Response::builder()
|
||||
.status(http::StatusCode::OK)
|
||||
.header(http::header::CONTENT_TYPE, "text/html")
|
||||
.body(hyper::Body::from(html))
|
||||
.unwrap()
|
||||
async fn root() -> impl IntoResponse {
|
||||
let template = RootTemplate {};
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "root.html")]
|
||||
struct RootTemplate;
|
||||
|
||||
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
||||
/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
// Attempt to render the template with askama
|
||||
match self.0.render() {
|
||||
// If we're able to successfully parse and aggregate the template, serve it
|
||||
Ok(html) => Html(html).into_response(),
|
||||
// If we're not, return an error or some bit of fallback HTML
|
||||
Err(err) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to render template. Error: {}", err),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/styles/tailwind.css
Normal file
3
src/styles/tailwind.css
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
Loading…
Add table
Add a link
Reference in a new issue