termstage 0.1.0

Local browser terminal presentation tool for live demos.
//! First-party browser terminal assets.
//!
//! The frontend bundle is generated by `make frontend-build` from
//! `apps/server/web`. The binary embeds the built files so production requests do
//! not fetch scripts or styles from a CDN.

use axum::{
    body::Body,
    http::{
        Response, StatusCode,
        header::{CONTENT_TYPE, HeaderValue},
    },
    response::IntoResponse,
};

const INDEX_HTML: &str = include_str!("../web/dist/index.html");
const APP_JS: &[u8] = include_bytes!("../web/dist/assets/index.js");
const APP_CSS: &str = include_str!("../web/dist/assets/index.css");

/// Serves the browser terminal HTML document.
#[must_use]
pub fn index_response() -> Response<Body> {
    response("text/html; charset=utf-8", Body::from(INDEX_HTML))
}

/// Serves a bundled static asset.
#[must_use]
pub fn asset_response(path: &str) -> Response<Body> {
    match path {
        "index.js" => response("text/javascript; charset=utf-8", Body::from(APP_JS)),
        "index.css" => response("text/css; charset=utf-8", Body::from(APP_CSS)),
        _ => StatusCode::NOT_FOUND.into_response(),
    }
}

fn response(content_type: &'static str, body: Body) -> Response<Body> {
    let mut response = Response::new(body);
    response
        .headers_mut()
        .insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
    response
}