#![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; #[macro_use] extern crate serde; use serde_json::json; //use rocket::request::FromSegments; //use rocket::http::uri::Segments; use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; mod store; use crate::store::Store; use rocket::State; use parking_lot::RwLock; use rocket::response::Redirect; use rocket::http::Status; use rocket::request::Form; use std::collections::HashMap; use std::env; use crate::store::form::RenderedField; #[derive(Serialize)] struct FormContext<'a> { pub fields : Vec>, } #[get("/")] fn index(store : State>) -> Template { let rg = store.read(); let tags = json!(["foo", "bar"]); let indexes = &rg.index; let context = FormContext { fields: rg.model.fields.iter().map(|(key, field)| { let value = if key == "tags" { Some(&tags) } else { None }; RenderedField::from_template_field(key, field, value, indexes) }).collect() }; Template::render("index", context) } //#[post("/add", data="")] //fn add_part(store : State>, record : Form) -> Redirect { // store.write().add(record.into_inner()); // Redirect::to(uri!(index)) //} fn main() { let cwd = env::current_dir().unwrap(); let data_dir = cwd.join("data"); let store = Store::new(data_dir); rocket::ignite() .attach(Template::fairing()) .manage(RwLock::new(store)) .mount("/", StaticFiles::from(cwd.join("templates/static/"))) .mount("/", routes![index]).launch(); }