[web] [WIP] Restore static asset serving
This commit is contained in:
parent
dd5b4c5238
commit
5d47635ce8
|
|
@ -3,6 +3,7 @@ pub mod contrast;
|
||||||
pub mod statics;
|
pub mod statics;
|
||||||
pub mod configuration;
|
pub mod configuration;
|
||||||
|
|
||||||
|
use crate::statics::StaticAsset;
|
||||||
use smol::io::AsyncWriteExt;
|
use smol::io::AsyncWriteExt;
|
||||||
use smol::stream::StreamExt;
|
use smol::stream::StreamExt;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
@ -195,7 +196,11 @@ fn route_request(req: &ScgiRequest) -> Route {
|
||||||
#[cfg(feature = "embed_static_assets")]
|
#[cfg(feature = "embed_static_assets")]
|
||||||
// If the route is /static/, check to see if it matches an asset first
|
// If the route is /static/, check to see if it matches an asset first
|
||||||
if segments.get(0).map(Deref::deref) == Some("static") {
|
if segments.get(0).map(Deref::deref) == Some("static") {
|
||||||
// TODO try serve static files
|
for asset in statics::STATIC_ASSETS {
|
||||||
|
if Some(asset.filename) == segments.get(1).map(Deref::deref) {
|
||||||
|
return Route::SendStatic(asset);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determines if we need to respond with a thumbnail or a web page
|
// Determines if we need to respond with a thumbnail or a web page
|
||||||
|
|
@ -258,7 +263,7 @@ enum Route {
|
||||||
/// Respond to the request with one of the static files embeded in the application
|
/// Respond to the request with one of the static files embeded in the application
|
||||||
///
|
///
|
||||||
/// The wrapped pointer is a pointer to those bytes in memory.
|
/// The wrapped pointer is a pointer to those bytes in memory.
|
||||||
SendStatic(&'static [u8]),
|
SendStatic(&'static StaticAsset),
|
||||||
|
|
||||||
/// Respond with an HTML pronoun page.
|
/// Respond with an HTML pronoun page.
|
||||||
///
|
///
|
||||||
|
|
@ -301,12 +306,7 @@ impl Route {
|
||||||
/// Actually perform the action that each route entails
|
/// Actually perform the action that each route entails
|
||||||
fn generate_response(self, settings: &InstanceSettings) -> Response {
|
fn generate_response(self, settings: &InstanceSettings) -> Response {
|
||||||
let result = match self {
|
let result = match self {
|
||||||
Route::SendStatic(data) => Ok(Response {
|
Route::SendStatic(data) => Ok(data.generate_response()),
|
||||||
status: 200,
|
|
||||||
headers: Cow::Borrowed(&[
|
|
||||||
]),
|
|
||||||
body: data.into(),
|
|
||||||
}),
|
|
||||||
Route::SendPronounPage(name, prefs) =>
|
Route::SendPronounPage(name, prefs) =>
|
||||||
Route::send_pronoun_page(name, prefs, settings),
|
Route::send_pronoun_page(name, prefs, settings),
|
||||||
Route::SendThumbnail(name, prefs) =>
|
Route::SendThumbnail(name, prefs) =>
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ impl StaticAsset {
|
||||||
/// Generate the HTTP response for sending this asset to the client
|
/// Generate the HTTP response for sending this asset to the client
|
||||||
// I wrote all this code to make this a const fn, and then don't even use it in
|
// I wrote all this code to make this a const fn, and then don't even use it in
|
||||||
// compile-time :(
|
// compile-time :(
|
||||||
const fn generate_response(&self) -> Response {
|
pub const fn generate_response(&self) -> Response {
|
||||||
Response {
|
Response {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: Cow::Borrowed(StaticAsset::STATIC_HEADERS),
|
headers: Cow::Borrowed(StaticAsset::STATIC_HEADERS),
|
||||||
|
|
@ -54,7 +54,7 @@ macro_rules! static_asset {
|
||||||
pub const FONT: StaticAsset = static_asset!("font.otf");
|
pub const FONT: StaticAsset = static_asset!("font.otf");
|
||||||
|
|
||||||
/// A list of static assets which should be served by the server
|
/// A list of static assets which should be served by the server
|
||||||
pub const STATIC_ASSETS: &[StaticAsset] = &[
|
pub const STATIC_ASSETS: &[&StaticAsset] = &[
|
||||||
#[cfg(any(feature = "embed_static_assets"))]
|
#[cfg(any(feature = "embed_static_assets"))]
|
||||||
FONT,
|
&FONT,
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue