Add /index.html and /image.html

This commit is contained in:
Joscha 2024-03-10 00:52:49 +01:00
parent 3cec866ba2
commit a51f3fa9b5
7 changed files with 241 additions and 1 deletions

View file

@ -10,7 +10,9 @@ clap = { version = "4.5.1", features = ["derive", "deprecated"] }
cosmic-text.workspace = true
escpos = { version = "0.7.2", features = ["full"] }
image.workspace = true
mime_guess = "2.0.4"
palette.workspace = true
rust-embed = "8.3.0"
serde = { version = "1.0.197", features = ["derive"] }
showbits-common.workspace = true
taffy.workspace = true

View file

@ -1,7 +1,9 @@
mod r#static;
use axum::{
extract::{DefaultBodyLimit, Multipart, State},
http::StatusCode,
routing::post,
routing::{get, post},
Form, Router,
};
use serde::Deserialize;
@ -22,6 +24,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/text", post(post_text))
.route("/image", post(post_image))
.route("/chat_message", post(post_chat_message))
.fallback(get(r#static::get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
.with_state(Server { tx });

View file

@ -0,0 +1,28 @@
use axum::{
http::{header, StatusCode, Uri},
response::{IntoResponse, Response},
};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "static"]
struct StaticFiles;
struct StaticFile(pub String);
impl IntoResponse for StaticFile {
fn into_response(self) -> Response {
match StaticFiles::get(&self.0) {
None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(),
Some(file) => {
let mime = mime_guess::from_path(self.0).first_or_octet_stream();
([(header::CONTENT_TYPE, mime.as_ref())], file.data).into_response()
}
}
}
}
pub async fn get_static_file(uri: Uri) -> impl IntoResponse {
let path = uri.path().trim_start_matches('/').to_string();
StaticFile(path)
}

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TP: Image</title>
</head>
<body>
<h1>Upload an image</h1>
<form action="image" method="post" enctype="multipart/form-data">
<ol>
<li><input type="file" name="image" /></li>
<li><button>Print!</button></li>
<li>Come back to this page</li>
</ol>
</form>
</body>
</html>

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>TP: Index</title>
</head>
<body>
<h1>Thermal Printer Control</h1>
<ul>
<a href="image.html">Upload an image</a>
</ul>
</body>
</html>