now the context og the templates is unificated in a single file

pull/1/head
kirbylife 2019-11-25 14:59:42 -06:00
parent 0203b88900
commit 11e183504d
2 changed files with 33 additions and 33 deletions

View File

@ -1,5 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)]
pub mod misc;
pub mod models;
pub mod schema;
@ -9,7 +10,6 @@ extern crate rocket;
extern crate diesel;
extern crate comrak;
extern crate dotenv;
extern crate rand;
extern crate rocket_contrib;
extern crate tera;
@ -19,25 +19,13 @@ use comrak::{markdown_to_html, ComrakOptions};
use diesel::pg::PgConnection;
use diesel::result::Error;
use dotenv::dotenv;
use rand::Rng;
use misc::get_context;
use rocket::Request;
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use std::env;
use std::vec::Vec;
use tera::Context;
const TITLES: [&str; 9] = [
"/* {} */",
"# {}",
"// {}",
"<!-- {} -->",
"{# {} #}",
"-- {}",
"--[[ {} --]]",
"; {}",
"% {}",
];
const MAX_POSTS_PER_PAGE: i64 = 20;
fn establish_connection() -> PgConnection {
@ -71,17 +59,12 @@ fn get_posts(page: i64) -> (Vec<Post>, i64) {
fn index(page: Option<i8>) -> Template {
let page = page.unwrap_or_else(|| 1);
let mut context = Context::new();
let mut rng = rand::thread_rng();
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado");
let mut context = get_context();
let (posts, n_posts) = get_posts(page as i64);
let total_pages = (n_posts as f64 / MAX_POSTS_PER_PAGE as f64).ceil() as i64;
context.insert("title", &title);
context.insert("posts", &posts);
context.insert("total_pages", &total_pages);
context.insert("actual_page", &page);
@ -99,19 +82,13 @@ fn get_post(id_number: i32) -> Result<Post, Error> {
#[get("/post/<title>")]
fn show_post(title: String) -> Template {
let mut context = Context::new();
let mut context = get_context();
let title_splited: Vec<&str> = title.split('-').collect();
let id = title_splited.last().unwrap().parse().unwrap();
let mut rng = rand::thread_rng();
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado");
let post = get_post(id);
context.insert("title", &title);
match post {
Ok(mut p) => {
let content = markdown_to_html(&p.content, &ComrakOptions::default());
@ -130,15 +107,10 @@ fn show_post(title: String) -> Template {
#[catch(404)]
fn not_found_404(req: &Request) -> Template {
let mut context = Context::new();
let mut rng = rand::thread_rng();
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado");
let mut context = get_context();
let uri = format!("{}", req.uri());
context.insert("title", &title);
context.insert("url", &uri);
Template::render("404", context)
}

28
src/misc.rs 100644
View File

@ -0,0 +1,28 @@
extern crate rand;
extern crate tera;
use rand::Rng;
use tera::Context;
const TITLES: [&str; 9] = [
"/* {} */",
"# {}",
"// {}",
"<!-- {} -->",
"{# {} #}",
"-- {}",
"--[[ {} --]]",
"; {}",
"% {}",
];
pub fn get_context() -> Context {
let mut context = Context::new();
let mut rng = rand::thread_rng();
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado");
context.insert("title", &title);
context
}