wip
This commit is contained in:
38
src/lib.rs
38
src/lib.rs
@@ -1,12 +1,40 @@
|
||||
|
||||
use rocket::*;
|
||||
use rocket_dyn_templates::Template;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::{*, fairing::AdHoc};
|
||||
use rocket_dyn_templates::{Template, tera::Value};
|
||||
use ::serde::Deserialize;
|
||||
|
||||
pub mod routes;
|
||||
|
||||
pub fn rocket_builder() -> Rocket<Build> {
|
||||
rocket::build().attach(
|
||||
Template::fairing()
|
||||
#[derive(Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct PresentationConfig {
|
||||
static_route: String,
|
||||
}
|
||||
|
||||
|
||||
pub fn static_filter(
|
||||
value: &Value,
|
||||
args: &HashMap<String, rocket_dyn_templates::tera::Value>
|
||||
) -> Result<Value, rocket_dyn_templates::tera::Error> {
|
||||
let host = args.get("static_host");
|
||||
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}/{}", host.unwrap().as_str().unwrap(), value.as_str().unwrap()))); // TODO: fix-me here!
|
||||
}
|
||||
|
||||
pub fn rocket_builder() -> Rocket<Build> {
|
||||
let rocket = rocket::build();
|
||||
//let figment = rocket.figment();
|
||||
// extract the entire config any `Deserialize` value
|
||||
//let config: PresentationConfig = figment.extract().expect("config");
|
||||
|
||||
rocket.attach(
|
||||
Template::try_custom(|engines| {
|
||||
engines.tera.register_filter("static", static_filter);
|
||||
Ok(())
|
||||
})
|
||||
).attach(
|
||||
AdHoc::config::<PresentationConfig>()
|
||||
).mount("/", routes![
|
||||
routes::root::index
|
||||
])
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
use rocket::get;
|
||||
use rocket::{get, State, config};
|
||||
use rocket_dyn_templates::Template;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct Test {
|
||||
use crate::PresentationConfig;
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct RootPage {
|
||||
static_host: String,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub fn index() -> Template {
|
||||
let context = Test {};
|
||||
pub fn index(config: &State<PresentationConfig>) -> Template {
|
||||
let context = RootPage {
|
||||
static_host: config.static_route.clone(),
|
||||
};
|
||||
Template::render("default", &context)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user