add static serving
Some checks failed
cargo_test_bench / Run Tests (push) Successful in 1m48s
cargo_test_bench / Run Benchmarks (push) Failing after 2m13s

This commit is contained in:
saji 2024-08-08 17:24:45 -05:00
parent 435337240a
commit 01df079b35
9 changed files with 53 additions and 17 deletions

20
Cargo.lock generated
View file

@ -1172,6 +1172,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "minijinja"
version = "2.1.1"
@ -1502,6 +1512,7 @@ dependencies = [
"include_dir",
"linux-embedded-hal",
"mime",
"mime_guess",
"minijinja",
"minijinja-embed",
"palette",
@ -2317,6 +2328,15 @@ dependencies = [
"thiserror",
]
[[package]]
name = "unicase"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"

View file

@ -16,6 +16,7 @@ image = "0.25.1"
include_dir = "0.7.4"
linux-embedded-hal = { version = "0.4.0"}
mime = "0.3.17"
mime_guess = "2.0.5"
minijinja = { version = "2.1.0", features = ["loader"] }
minijinja-embed = "2.1.1"
palette = "0.7.6"

View file

@ -2,7 +2,7 @@ use crate::api;
use crate::display::create_display_thread;
use crate::dither::{DitherMethod, DitheredImage};
use crate::eink::Palette;
use axum::extract::State;
use axum::extract::{Path, State};
use axum::http::{header, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
@ -11,6 +11,7 @@ use base64::{engine::general_purpose::STANDARD, Engine as _};
use image::imageops::{resize, FilterType};
use include_dir::{include_dir, Dir, DirEntry};
use minijinja::{context, Environment};
use mime_guess::Mime;
use std::io::Cursor;
use std::sync::mpsc;
use std::sync::Arc;
@ -108,7 +109,7 @@ pub fn make_app_router() -> Router {
.route("/app", get(app_handler))
.route("/app/preview", post(app_preview))
.nest("/api", api::router())
.route("/assets", get(asset_handler))
.route("/static/*path", get(asset_handler))
.with_state(AppState::default())
}
@ -153,8 +154,18 @@ async fn app_preview(
Ok((StatusCode::OK, headers, content))
}
async fn asset_handler() -> Result<impl IntoResponse, AppError> {
Ok(StatusCode::OK)
async fn asset_handler(Path(s): Path<String>) -> Result<impl IntoResponse, AppError> {
let Some(file) = ASSETS_DIR.get_file(&s) else {
panic!("fixme");
};
let mime = mime_guess::from_path(s).first_or_text_plain();
let headers = [
(header::CONTENT_TYPE, mime.essence_str().to_string())
];
Ok((StatusCode::OK, headers, file.contents()))
}
#[cfg(test)]

View file

@ -104,8 +104,7 @@ pub fn get_display() -> Box<dyn EInkPanel + Send> {
/// used in an async context since updating the display can take ~30 seconds.
#[must_use]
#[instrument(skip_all)]
pub fn create_display_thread(
) -> (thread::JoinHandle<()>, mpsc::Sender<Box<DitheredImage>>) {
pub fn create_display_thread() -> (thread::JoinHandle<()>, mpsc::Sender<Box<DitheredImage>>) {
let mut display = get_display();
let (tx, rx) = mpsc::channel::<Box<DitheredImage>>();
let handle = thread::spawn(move || {

View file

@ -84,9 +84,7 @@ async fn main() -> anyhow::Result<()> {
display.display(&eink_buf)?;
}
Command::Serve => {
let app = app::make_app_router()
.layer(TraceLayer::new_for_http());
let app = app::make_app_router().layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
info!("Listening on 0.0.0.0:3000");
axum::serve(listener, app).await?;

5
static/alpine.js Normal file

File diff suppressed because one or more lines are too long

1
static/htmx.js Normal file

File diff suppressed because one or more lines are too long

4
static/pico.css Normal file

File diff suppressed because one or more lines are too long

View file

@ -4,18 +4,15 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %} My Site {% endblock %}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
<script src="https://unpkg.com/htmx.org@2.0.1" integrity="sha384-QWGpdj554B4ETpJJC9z+ZHJcA/i59TyjxEPXiiUgN2WmTyV5OEZWCD6gQhgkdpB/" crossorigin="anonymous"></script>
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"> -->
<!-- <script src="https://unpkg.com/htmx.org@2.0.1" integrity="sha384-QWGpdj554B4ETpJJC9z+ZHJcA/i59TyjxEPXiiUgN2WmTyV5OEZWCD6gQhgkdpB/" crossorigin="anonymous"></script> -->
<link rel="stylesheet" href="/static/pico.css">
<script src="/static/htmx.js"></script>
<script src="/static/alpine.js"></script>
{% block head %}{% endblock %}
</head>
<body>
{% block content %}
<h1 class="title">
Hello World
</h1>
<p class="subtitle">
My first website with <strong>Bulma</strong>!
</p>
{% endblock %}
</body>
</html>