sql-web 0.3.1

A web-based database browser for SQLite, MySQL, and PostgreSQL written in Rust using Axum, React, and SQLx.
use axum::{
    body::Body,
    http::{HeaderMap, HeaderValue, StatusCode, Uri, header},
    response::{IntoResponse, Response},
};
use include_dir::{Dir, include_dir};

static DIST: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/frontend/dist");

pub async fn serve(uri: Uri) -> Response {
    let path = uri.path().trim_start_matches('/');

    if path.starts_with("api/") {
        return StatusCode::NOT_FOUND.into_response();
    }

    if let Some(file) = DIST.get_file(path) {
        return file_response(path, file.contents());
    }

    if let Some(file) = DIST.get_file("index.html") {
        return file_response("index.html", file.contents());
    }

    StatusCode::NOT_FOUND.into_response()
}

fn file_response(path: &str, contents: &[u8]) -> Response {
    let mime = mime_guess::from_path(path).first_or_octet_stream();
    let mut headers = HeaderMap::new();
    headers.insert(
        header::CONTENT_TYPE,
        HeaderValue::from_str(mime.as_ref())
            .unwrap_or_else(|_| HeaderValue::from_static("application/octet-stream")),
    );

    (headers, Body::from(contents.to_vec())).into_response()
}