use rocket config infrastructure to provide assets + blog feed, add justfile

This commit is contained in:
Philippe Loctaux 2025-07-20 21:38:08 +02:00
parent 0f7165d24b
commit abd425c628
4 changed files with 43 additions and 8 deletions

11
crates/plcom/justfile Normal file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env just --justfile
# list recipes
default:
just --list
run:
PLCOM_ASSETS=../../public PLCOM_BLOG_FEED=/Users/phil/x/blog/target/blog/atom.xml cargo run
tailwind:
nix-shell -p tailwindcss_4 --run 'tailwindcss --input ./css/main.css --cwd {{invocation_directory()}}' > ../../public/style.css

View file

@ -14,18 +14,42 @@ mod pages;
mod views;
use pages::*;
use rocket::fairing::AdHoc;
#[derive(rocket::serde::Deserialize, Debug)]
#[serde(crate = "rocket::serde")]
struct Config {
assets: String,
blog_feed: String,
}
pub fn config() -> rocket::figment::Figment {
use rocket::figment::providers::*;
use rocket::figment::Figment;
use rocket::Config;
// rocket defaults
Figment::from(Config::default())
// from env variables directly
.merge(Env::prefixed("PLCOM_").ignore(&["PROFILE"]).global())
}
#[rocket::launch]
fn rocket() -> _ {
let assets = std::env::var("PLCOM_ASSETS_PATH").unwrap_or("../../public".into());
let rocket = rocket::custom(config());
let figment = rocket.figment();
let server = rocket::build()
.mount("/", rocket::fs::FileServer::from(assets))
let config: Config = figment.extract().expect("server configuration");
println!("Using configuration {config:#?}");
let server = rocket
.mount("/", rocket::fs::FileServer::from(config.assets))
.mount(
"/",
rocket::routes![root_route, email_route, wallpapers_route],
)
.register("/", rocket::catchers![not_found]);
.register("/", rocket::catchers![not_found])
.attach(AdHoc::config::<Config>());
if cfg!(debug_assertions) {
server

View file

@ -4,7 +4,7 @@ mod wallpapers;
use crate::common::wallpapers::Wallpaper;
use crate::prelude::*;
use rocket::get;
use rocket::{get, State};
#[derive(rocket::Responder)]
#[response(content_type = "text/html")]
@ -28,12 +28,12 @@ pub fn not_found() -> LeptosResponder {
}
#[get("/?<wallpaper>")]
pub async fn root_route(wallpaper: Option<&str>) -> LeptosResponder {
pub async fn root_route(wallpaper: Option<&str>, config: &State<crate::Config>) -> LeptosResponder {
let wallpaper = wallpaper
.and_then(Wallpaper::find)
.or_else(Wallpaper::random);
let mut blog = root::Blog::new("https://philippeloctaux.com/blog/atom.xml");
let mut blog = root::Blog::new(&config.blog_feed);
if let Err(e) = blog.fetch_feed().await {
println!("Failed to get Atom feed: {e}");
}