Browse Source

sept6

master
Xsivax 3 years ago
parent
commit
5f55870d02
13 changed files with 141 additions and 19 deletions
  1. +13
    -3
      Cargo.toml
  2. +3
    -0
      README.md
  3. +3
    -0
      lang.json
  4. +32
    -0
      src/build_rocket/config.rs
  5. +20
    -5
      src/build_rocket/get.rs
  6. +0
    -0
      src/build_rocket/get_static_files.rs
  7. +9
    -0
      src/build_rocket/mod.rs
  8. +27
    -0
      src/build_rocket/templates.rs
  9. +20
    -7
      src/main.rs
  10. +0
    -2
      src/routes/mod.rs
  11. +2
    -2
      templates/index.html
  12. +1
    -0
      templates/privacy-policy.html
  13. +11
    -0
      templates/test.html.tera

+ 13
- 3
Cargo.toml 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.rocket]
version = "0.5.0-rc.1"
features = ["json"]
[dependencies] [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"
#diesel = { version = "1.4.4", features = ["mysql"]}
#dotenv = "0.15.0"

+ 3
- 0
README.md 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
- 0
lang.json View File

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

+ 32
- 0
src/build_rocket/config.rs 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()
}

src/routes/get.rs → src/build_rocket/get.rs 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> {
NamedFile::open("templates/experiments.html").await.ok()
pub async fn experiments() -> Result<NamedFile, NotFound<String>> {
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> {
NamedFile::open("templates/privacy-policy.html").await.ok()
pub async fn privacy() -> Result<NamedFile, NotFound<String>> {
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",
})
}

src/routes/get_static_files.rs → src/build_rocket/get_static_files.rs View File


+ 9
- 0
src/build_rocket/mod.rs View File

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

+ 27
- 0
src/build_rocket/templates.rs 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)
// }

+ 20
- 7
src/main.rs View File

@ -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() // 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
// registering/ catchers
// manage State
// mount routes
// register catchers
// 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

+ 0
- 2
src/routes/mod.rs View File

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

+ 2
- 2
templates/index.html 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>

+ 1
- 0
templates/privacy-policy.html 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
- 0
templates/test.html.tera 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>

Loading…
Cancel
Save