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

@ -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)
}