use rocket::{Rocket, Build, Orbit};
use rocket::fairing::{self, Fairing, Info, Kind};
use rocket::figment::{Source, value::magic::RelativePathBuf};
use rocket::trace::Trace;
use crate::context::{Callback, Context, ContextManager};
use crate::template::DEFAULT_TEMPLATE_DIR;
use crate::engine::Engines;
pub struct TemplateFairing {
pub callback: Callback,
}
#[rocket::async_trait]
impl Fairing for TemplateFairing {
fn info(&self) -> Info {
let kind = Kind::Ignite | Kind::Liftoff;
#[cfg(debug_assertions)] let kind = kind | Kind::Request;
Info { kind, name: "Templating" }
}
async fn on_ignite(&self, rocket: Rocket<Build>) -> fairing::Result {
let configured_dir = rocket.figment()
.extract_inner::<RelativePathBuf>("template_dir")
.map(|path| path.relative());
let path = match configured_dir {
Ok(dir) => dir,
Err(e) if e.missing() => DEFAULT_TEMPLATE_DIR.into(),
Err(e) => {
e.trace_error();
return Err(rocket);
}
};
if let Some(ctxt) = Context::initialize(&path, &self.callback) {
Ok(rocket.manage(ContextManager::new(ctxt)))
} else {
error!("Template initialization failed. Aborting launch.");
Err(rocket)
}
}
async fn on_liftoff(&self, rocket: &Rocket<Orbit>) {
let cm = rocket.state::<ContextManager>()
.expect("Template ContextManager registered in on_ignite");
span_info!("templating" => {
info!(directory = %Source::from(&*cm.context().root));
info!(engines = ?Engines::ENABLED_EXTENSIONS);
});
}
#[cfg(debug_assertions)]
async fn on_request(&self, req: &mut rocket::Request<'_>, _data: &mut rocket::Data<'_>) {
let cm = req.rocket().state::<ContextManager>()
.expect("Template ContextManager registered in on_ignite");
cm.reload_if_needed(&self.callback);
}
}