Embed css themes to simplify adding and testing new themes (#489)

This commit is contained in:
mikupls 2022-05-21 03:41:31 +02:00 committed by GitHub
parent 1c8bcf33c1
commit 60c7b6b23f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 274 additions and 121 deletions

View file

@ -19,7 +19,7 @@ use hyper::{header::HeaderValue, Body, Request, Response};
mod client;
use client::proxy;
use server::RequestExt;
use utils::{error, redirect};
use utils::{error, redirect, ThemeAssets};
mod server;
@ -85,6 +85,23 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result<Respons
Ok(res)
}
async fn style() -> Result<Response<Body>, String> {
let mut res = include_str!("../static/style.css").to_string();
for file in ThemeAssets::iter() {
res.push_str("\n");
let theme = ThemeAssets::get(file.as_ref()).unwrap();
res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap());
}
Ok(
Response::builder()
.status(200)
.header("content-type", "text/css")
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body(res.to_string().into())
.unwrap_or_default(),
)
}
#[tokio::main]
async fn main() {
let matches = Command::new("Libreddit")
@ -152,7 +169,7 @@ async fn main() {
}
// Read static files
app.at("/style.css").get(|_| resource(include_str!("../static/style.css"), "text/css", false).boxed());
app.at("/style.css").get(|_| style().boxed());
app
.at("/manifest.json")
.get(|_| resource(include_str!("../static/manifest.json"), "application/json", false).boxed());