mirror of
https://codeberg.org/icewind/ptouch-api.git
synced 2026-06-03 10:54:07 +02:00
initial webui work
This commit is contained in:
parent
f7910f967b
commit
71cf4dbcb2
4 changed files with 248 additions and 4 deletions
40
src/main.rs
40
src/main.rs
|
|
@ -1,11 +1,12 @@
|
|||
mod config;
|
||||
mod status;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use crate::config::{Config, ListenConfig};
|
||||
use axum::body::Bytes;
|
||||
use axum::body::{Body, Bytes};
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::http::{header, HeaderValue, StatusCode};
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, put};
|
||||
use axum::{Json, Router};
|
||||
use clap::Parser;
|
||||
|
|
@ -51,12 +52,15 @@ async fn main() -> MainResult {
|
|||
|
||||
let mut printer = Printer::open().await?;
|
||||
printer.reload_status().await?;
|
||||
|
||||
|
||||
let state = App {
|
||||
printer: Arc::new(Mutex::new(printer)),
|
||||
};
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/label.js", get(js))
|
||||
.route("/style.css", get(css))
|
||||
.route("/status", get(status))
|
||||
.route("/print", put(print))
|
||||
.with_state(state.clone());
|
||||
|
|
@ -173,6 +177,34 @@ impl IntoResponse for ApiError {
|
|||
}
|
||||
}
|
||||
|
||||
async fn index() -> Html<Cow<'static, str>> {
|
||||
#[cfg(debug_assertions)]
|
||||
let html = Cow::Owned(std::fs::read_to_string("web/index.html").unwrap());
|
||||
#[cfg(not(debug_assertions))]
|
||||
let html = Cow::Borrowed(include_str!("../web/index.html"));
|
||||
Html(html)
|
||||
}
|
||||
|
||||
async fn js() -> Response {
|
||||
#[cfg(debug_assertions)]
|
||||
let html: Cow<'static, str> = Cow::Owned(std::fs::read_to_string("web/label.js").unwrap());
|
||||
#[cfg(not(debug_assertions))]
|
||||
let html: Cow<'static, str> = Cow::Borrowed(include_str!("../web/label.js"));
|
||||
let mut response = Response::new(Body::from(html));
|
||||
response.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/javascript"));
|
||||
response
|
||||
}
|
||||
|
||||
async fn css() -> Response {
|
||||
#[cfg(debug_assertions)]
|
||||
let html: Cow<'static, str> = Cow::Owned(std::fs::read_to_string("web/style.css").unwrap());
|
||||
#[cfg(not(debug_assertions))]
|
||||
let html: Cow<'static, str> = Cow::Borrowed(include_str!("../web/style.css"));
|
||||
let mut response = Response::new(Body::from(html));
|
||||
response.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("text/css"));
|
||||
response
|
||||
}
|
||||
|
||||
async fn status(State(state): State<App>) -> Result<Json<PStatus>, ApiError> {
|
||||
let mut printer = state.printer.lock().await;
|
||||
debug!("reloading printer status");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue