This commit is contained in:
Xsivax 2021-09-06 19:33:38 +02:00
parent 67ce5cd5ee
commit 5f55870d02
13 changed files with 142 additions and 20 deletions

View file

@ -4,8 +4,18 @@ version = "0.1.0"
authors = ["siva <alexandra.hosp@protonmail.com>"] authors = ["siva <alexandra.hosp@protonmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies.rocket]
rocket = "0.5.0-rc.1" version = "0.5.0-rc.1"
features = ["json"]
diesel = { version = "1.4.4", features = ["mysql"]} [dependencies]
dotenv = "0.15.0" 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"

View file

@ -4,6 +4,7 @@ start docker container: docker-compose up
[To do for me ] [To do for me ]
! add dynamic language handling with templates ! add dynamic language handling with templates
--> extract JSON data into Rust type => use in Template => use conditionally(Accept-Language-Header)
! add form handling ! add form handling
! wasm for Threejs canvas ! wasm for Threejs canvas
! login, payment, cart ! login, payment, cart
@ -13,6 +14,8 @@ start docker container: docker-compose up
Frontend: Frontend:
! Fix css queries ! Fix css queries
! Fix css footer
! Fix css home posts-section
! Fix js open menu ! Fix js open menu
! add css Animations ! add css Animations
! cleaner look ! cleaner look

3
lang.json Normal file
View file

@ -0,0 +1,3 @@
{
"lang" : "json"
}

View file

@ -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<String> {
// 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()
}

View file

@ -4,21 +4,27 @@ use rocket::fs::NamedFile;
// Error type 404 // Error type 404
use rocket::response::status::NotFound; use rocket::response::status::NotFound;
// Responder type Template
use rocket_dyn_templates::{ Template };
use crate::build_rocket::{ templates, config };
// Configure requests // Configure requests
// generate routes, set attributes // generate routes, set attributes
// set http method // set http method
// set uri // set uri
#[route(GET, uri = "/")] #[get("/")]
// if success call handler // if success call handler
pub async fn index() -> Result<NamedFile, NotFound<String>> { pub async fn index() -> Result<NamedFile, NotFound<String>> {
NamedFile::open("templates/index.html").await.map_err(|e|NotFound(e.to_string())) NamedFile::open("templates/index.html").await.map_err(|e|NotFound(e.to_string()))
} }
// serve other pages (html GET) // serve other pages (html GET)
// create routes for pages // create routes for pages
#[get("/experiments")] #[get("/experiments")]
pub async fn experiments() -> Option<NamedFile> { pub async fn experiments() -> Result<NamedFile, NotFound<String>> {
NamedFile::open("templates/experiments.html").await.ok() NamedFile::open("templates/experiments.html").await.map_err(|e|NotFound(e.to_string()))
} }
#[get("/permaculture")] #[get("/permaculture")]
@ -127,8 +133,17 @@ use rocket::response::status::NotFound;
} }
#[get("/privacy")] #[get("/privacy")]
pub async fn privacy() -> Option<NamedFile> { pub async fn privacy() -> Result<NamedFile, NotFound<String>> {
NamedFile::open("templates/privacy-policy.html").await.ok() 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",
})
}

9
src/build_rocket/mod.rs Normal file
View file

@ -0,0 +1,9 @@
// Routes
pub mod get;
pub mod get_static_files;
// Templates
pub mod templates;
// Configuration
pub mod config;

View file

@ -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)
// }

View file

@ -1,19 +1,26 @@
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
mod routes; #[macro_use]
use crate::routes::{ get, get_static_files }; 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() // Equivalent to main()
#[rocket::main] // enable async main() #[rocket::main] // enable async main()
// Return Rocket Instance
async fn main() -> Result<(), rocket::Error> { async fn main() -> Result<(), rocket::Error> {
// State Rocket<Build> => configuration // State Rocket<Build> => configuration
// registering routes // registering routes
// mounting routes // mount routes
// registering/ catchers // register catchers
// manage State
// attach Fairings // attach Fairings
// manage State
rocket::build() rocket::build()
// State Rocket<Ignite> => Finalized App ready to launch // State Rocket<Ignite> => Finalized App ready to launch
// check before launching // check before launching
@ -31,10 +38,16 @@ async fn main() -> Result<(), rocket::Error> {
.mount("/spider", .mount("/spider",
routes! [ get::spiderpi, get::join ]) routes! [ get::spiderpi, get::join ])
.mount("/learn", .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", .mount("/policies",
routes! [ get::policies_info, get::privacy ]) routes! [ get::policies_info, get::privacy ])
// test route template
.mount("/",
routes! [ get::test] )
// initialize and maintain templating state
.attach(Template::fairing())
.ignite().await? .ignite().await?
// State Rocket<Orbit> => Running App // State Rocket<Orbit> => Running App

View file

@ -1,2 +0,0 @@
pub mod get;
pub mod get_static_files;

View file

@ -19,7 +19,7 @@
<nav class="top-bar"> <nav class="top-bar">
<!--Add Askama Language Handling--> <!--Add Askama Language Handling-->
<div class="languages"> <div class="languages">
<a href="static/languages/deutsch.html">Deutsch |</a> <a href="static/languages/deutsch.html">{{ name }}</a>
<a href="static/languages/italiano.html">Italiano |</a> <a href="static/languages/italiano.html">Italiano |</a>
<a href="static/languages/francais.html">Francais </a> <a href="static/languages/francais.html">Francais </a>
</div> </div>
@ -362,7 +362,7 @@
</li> </li>
<li> <li>
<p> <p>
<a href="privacy.html">Privacy Policy</a> <a type="text/html" href="/privacy">Privacy Policy</a>
</p> </p>
</li> </li>
<li> <li>

View file

@ -10,5 +10,6 @@
<body> <body>
<h1>Privacy Policy</h1> <h1>Privacy Policy</h1>
<p>Add info how data is handled and licence of code</p> <p>Add info how data is handled and licence of code</p>
<h1>{{ name }}</h1>
</body> </body>
</html> </html>

11
templates/test.html.tera Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
<p>{{ lang }}</p>
</body>
</html>