sept6
This commit is contained in:
parent
67ce5cd5ee
commit
5f55870d02
13 changed files with 142 additions and 20 deletions
18
Cargo.toml
18
Cargo.toml
|
@ -4,8 +4,18 @@ version = "0.1.0"
|
|||
authors = ["siva <alexandra.hosp@protonmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.5.0-rc.1"
|
||||
[dependencies.rocket]
|
||||
version = "0.5.0-rc.1"
|
||||
features = ["json"]
|
||||
|
||||
diesel = { version = "1.4.4", features = ["mysql"]}
|
||||
dotenv = "0.15.0"
|
||||
[dependencies]
|
||||
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"
|
|
@ -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
|
||||
|
|
3
lang.json
Normal file
3
lang.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"lang" : "json"
|
||||
}
|
32
src/build_rocket/config.rs
Normal file
32
src/build_rocket/config.rs
Normal 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()
|
||||
}
|
|
@ -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, NotFound<String>> {
|
||||
|
||||
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> {
|
||||
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")]
|
||||
|
@ -127,8 +133,17 @@ use rocket::response::status::NotFound;
|
|||
}
|
||||
|
||||
#[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",
|
||||
})
|
||||
}
|
||||
|
9
src/build_rocket/mod.rs
Normal file
9
src/build_rocket/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
// Routes
|
||||
pub mod get;
|
||||
pub mod get_static_files;
|
||||
|
||||
// Templates
|
||||
pub mod templates;
|
||||
|
||||
// Configuration
|
||||
pub mod config;
|
27
src/build_rocket/templates.rs
Normal file
27
src/build_rocket/templates.rs
Normal 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)
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
27
src/main.rs
27
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<Build> => configuration
|
||||
// registering routes
|
||||
// mounting routes
|
||||
// registering/ catchers
|
||||
// manage State
|
||||
// mount routes
|
||||
// register catchers
|
||||
// attach Fairings
|
||||
// manage State
|
||||
rocket::build()
|
||||
// State Rocket<Ignite> => 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<Orbit> => Running App
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
pub mod get;
|
||||
pub mod get_static_files;
|
|
@ -19,7 +19,7 @@
|
|||
<nav class="top-bar">
|
||||
<!--Add Askama Language Handling-->
|
||||
<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/francais.html">Francais </a>
|
||||
</div>
|
||||
|
@ -362,7 +362,7 @@
|
|||
</li>
|
||||
<li>
|
||||
<p>
|
||||
<a href="privacy.html">Privacy Policy</a>
|
||||
<a type="text/html" href="/privacy">Privacy Policy</a>
|
||||
</p>
|
||||
</li>
|
||||
<li>
|
||||
|
|
|
@ -10,5 +10,6 @@
|
|||
<body>
|
||||
<h1>Privacy Policy</h1>
|
||||
<p>Add info how data is handled and licence of code</p>
|
||||
<h1>{{ name }}</h1>
|
||||
</body>
|
||||
</html>
|
11
templates/test.html.tera
Normal file
11
templates/test.html.tera
Normal 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…
Reference in a new issue