From 5f55870d020744140da2ca94dfe4e42de61770ca Mon Sep 17 00:00:00 2001 From: Xsivax Date: Mon, 6 Sep 2021 19:33:38 +0200 Subject: [PATCH] sept6 --- Cargo.toml | 16 ++++++++-- README.md | 3 ++ lang.json | 3 ++ src/build_rocket/config.rs | 32 +++++++++++++++++++ src/{routes => build_rocket}/get.rs | 25 ++++++++++++--- .../get_static_files.rs | 0 src/build_rocket/mod.rs | 9 ++++++ src/build_rocket/templates.rs | 27 ++++++++++++++++ src/main.rs | 27 ++++++++++++---- src/routes/mod.rs | 2 -- templates/index.html | 4 +-- templates/privacy-policy.html | 1 + templates/test.html.tera | 11 +++++++ 13 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 lang.json create mode 100644 src/build_rocket/config.rs rename src/{routes => build_rocket}/get.rs (83%) rename src/{routes => build_rocket}/get_static_files.rs (100%) create mode 100644 src/build_rocket/mod.rs create mode 100644 src/build_rocket/templates.rs delete mode 100644 src/routes/mod.rs create mode 100644 templates/test.html.tera diff --git a/Cargo.toml b/Cargo.toml index a61e841..791f03d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,18 @@ version = "0.1.0" authors = ["siva "] edition = "2018" +[dependencies.rocket] +version = "0.5.0-rc.1" +features = ["json"] + [dependencies] -rocket = "0.5.0-rc.1" +serde = "1.0.130" +serde_json = "1.0.67" +lazy_static = "1.4.0" + +[dependencies.rocket_dyn_templates] +version = "0.1.0-rc.1" +features = ["tera"] -diesel = { version = "1.4.4", features = ["mysql"]} -dotenv = "0.15.0" \ No newline at end of file +#diesel = { version = "1.4.4", features = ["mysql"]} +#dotenv = "0.15.0" \ No newline at end of file diff --git a/README.md b/README.md index 53102be..1538a76 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ start docker container: docker-compose up [To do for me ] ! add dynamic language handling with templates +--> extract JSON data into Rust type => use in Template => use conditionally(Accept-Language-Header) ! add form handling ! wasm for Threejs canvas ! login, payment, cart @@ -13,6 +14,8 @@ start docker container: docker-compose up Frontend: ! Fix css queries +! Fix css footer +! Fix css home posts-section ! Fix js open menu ! add css Animations ! cleaner look diff --git a/lang.json b/lang.json new file mode 100644 index 0000000..a415af5 --- /dev/null +++ b/lang.json @@ -0,0 +1,3 @@ +{ + "lang" : "json" +} \ No newline at end of file diff --git a/src/build_rocket/config.rs b/src/build_rocket/config.rs new file mode 100644 index 0000000..01dfdba --- /dev/null +++ b/src/build_rocket/config.rs @@ -0,0 +1,32 @@ +use std::fs::{ File }; +use std::io; +use std::io::prelude::*; + +use rocket_dyn_templates::tera::Value; + +// Define File for language handling +pub const LANG_FILE : &str ="./lang.json"; + +// Store JSON Value in 'static variable +lazy_static! { + #[derive(Debug)] + pub static ref LANG: bool = init_lang(); +} + + +// fn file_to_string() -> io::Result { +// let mut f = File::open(LANG_FILE)?; +// let mut buffer = String::new(); +// f.read_to_string(&mut buffer)?; +// Ok(buffer) +// } + +// pub fn init_lang() -> Value { +// serde_json::from_str(&file_to_string().unwrap()).expect("unable to convert to JSON") +// } + +pub fn init_lang() -> bool { + let f = File::open(LANG_FILE).expect("unable to open file"); + let json : Value = serde_json::from_reader(f).expect("file shoulde be JSON"); + json.is_object() +} diff --git a/src/routes/get.rs b/src/build_rocket/get.rs similarity index 83% rename from src/routes/get.rs rename to src/build_rocket/get.rs index a7442ab..d39da9f 100644 --- a/src/routes/get.rs +++ b/src/build_rocket/get.rs @@ -4,21 +4,27 @@ use rocket::fs::NamedFile; // Error type 404 use rocket::response::status::NotFound; +// Responder type Template +use rocket_dyn_templates::{ Template }; + +use crate::build_rocket::{ templates, config }; + // Configure requests // generate routes, set attributes // set http method // set uri - #[route(GET, uri = "/")] + #[get("/")] // if success call handler pub async fn index() -> Result> { + NamedFile::open("templates/index.html").await.map_err(|e|NotFound(e.to_string())) } // serve other pages (html GET) // create routes for pages #[get("/experiments")] - pub async fn experiments() -> Option { - NamedFile::open("templates/experiments.html").await.ok() + pub async fn experiments() -> Result> { + NamedFile::open("templates/experiments.html").await.map_err(|e|NotFound(e.to_string())) } #[get("/permaculture")] @@ -127,8 +133,17 @@ use rocket::response::status::NotFound; } #[get("/privacy")] - pub async fn privacy() -> Option { - NamedFile::open("templates/privacy-policy.html").await.ok() + pub async fn privacy() -> Result> { + NamedFile::open("templates/privacy-policy.html").await.map_err(|e|NotFound(e.to_string())) } + #[get("/test")] + // Render Template Responder + pub fn test() -> Template { + println!("{}", &config::init_lang()); + Template::render("test", templates::MyStruct{ + title: "test", + lang: "en", + }) + } \ No newline at end of file diff --git a/src/routes/get_static_files.rs b/src/build_rocket/get_static_files.rs similarity index 100% rename from src/routes/get_static_files.rs rename to src/build_rocket/get_static_files.rs diff --git a/src/build_rocket/mod.rs b/src/build_rocket/mod.rs new file mode 100644 index 0000000..deebed8 --- /dev/null +++ b/src/build_rocket/mod.rs @@ -0,0 +1,9 @@ +// Routes +pub mod get; +pub mod get_static_files; + +// Templates +pub mod templates; + +// Configuration +pub mod config; \ No newline at end of file diff --git a/src/build_rocket/templates.rs b/src/build_rocket/templates.rs new file mode 100644 index 0000000..eb76283 --- /dev/null +++ b/src/build_rocket/templates.rs @@ -0,0 +1,27 @@ +// Serde Data Model conversion +use rocket::serde::{ Serialize }; +use serde_json::Value; + +use crate ::build_rocket::{ config::LANG }; + + +// Implement serde Serialize to create context +#[derive(Serialize)] +pub struct MyStruct<'a> { + pub title : &'a str, + pub lang: &'a str, +} + +// pub fn get_value<'a>( key: &'a str, lang: &'a str ) -> Option<&'a Value> { +// let translation = LANG.get(key).unwrap(); +// Some(translation) +// } + + + + + + + + + diff --git a/src/main.rs b/src/main.rs index 2cd191e..302d0a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,26 @@ -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; -mod routes; -use crate::routes::{ get, get_static_files }; +#[macro_use] +extern crate lazy_static; +mod build_rocket; +use crate::build_rocket::{ get, get_static_files }; + +// support for template as Responder +use rocket_dyn_templates::Template; // Equivalent to main() #[rocket::main] // enable async main() +// Return Rocket Instance async fn main() -> Result<(), rocket::Error> { // State Rocket => configuration // registering routes - // mounting routes - // registering/ catchers - // manage State + // mount routes + // register catchers // attach Fairings + // manage State rocket::build() // State Rocket => Finalized App ready to launch // check before launching @@ -31,10 +38,16 @@ async fn main() -> Result<(), rocket::Error> { .mount("/spider", routes! [ get::spiderpi, get::join ]) .mount("/learn", - routes! [ get::about, get::partners, get::meet, get::blog, get::code ]) + routes! [ get::about, get::partners, get::meet, get::blog, get::code ]) .mount("/policies", routes! [ get::policies_info, get::privacy ]) + // test route template + .mount("/", + routes! [ get::test] ) + // initialize and maintain templating state + .attach(Template::fairing()) + .ignite().await? // State Rocket => Running App diff --git a/src/routes/mod.rs b/src/routes/mod.rs deleted file mode 100644 index 5711f6e..0000000 --- a/src/routes/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod get; -pub mod get_static_files; \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 33cabee..b1d5453 100644 --- a/templates/index.html +++ b/templates/index.html @@ -19,7 +19,7 @@