From 64fe3d5f03eaf064f3fc04d6e2d9dcbf2d1aeddf Mon Sep 17 00:00:00 2001 From: kirbylife Date: Wed, 13 Nov 2019 20:10:42 -0600 Subject: [PATCH] now display the posts saved on the DB --- src/main.rs | 47 ++++++++++++++++++++++++++++++++++----- static/css/style.css | 25 +++++++++++++++++++++ templates/index.html.tera | 13 ++++++++--- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 549e28a..fa9ac0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,61 @@ #![feature(proc_macro_hygiene, decl_macro)] +pub mod models; +pub mod schema; + #[macro_use] extern crate rocket; +#[macro_use] +extern crate diesel; +extern crate dotenv; extern crate rand; extern crate rocket_contrib; +extern crate tera; +use self::diesel::prelude::*; +use self::models::*; +use diesel::pg::PgConnection; +use dotenv::dotenv; use rand::Rng; use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; -use std::collections::HashMap; +use std::env; +use std::vec::Vec; +use tera::Context; const TITLES: [&str; 4] = ["/* {} */", "# {}", "// {}", ""]; -#[get("/")] -fn index() -> Template { +fn establish_connection() -> PgConnection { + dotenv().ok(); + + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL not setted"); + PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) +} + +pub fn get_posts() -> Vec { + use schema::posts::dsl::*; + + let connection = establish_connection(); + posts + .filter(published.eq(true)) + .limit(20) + .load::(&connection) + .expect("Error loading posts") +} + +#[get("/?")] +fn index(page: Option) -> Template { + let page = page.unwrap_or_else(|| 0); + println!("{}", page); + + let mut context = Context::new(); + let mut rng = rand::thread_rng(); - let mut context = HashMap::new(); let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; let title = str::replace(title_fmt, "{}", "CódigoComentado"); - context.insert("title", title); + + context.insert("title", &title); + context.insert("posts", &get_posts()); Template::render("index", context) } diff --git a/static/css/style.css b/static/css/style.css index 3a18940..73750ec 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -3,6 +3,7 @@ body{ padding: 0; margin: 0; border: 0; + height: 100%; } @@ -100,3 +101,27 @@ article strong { font-family: monospace; font-size: 10px; } + +footer { + bottom: 0; + position: fixed; + display: grid; + width: 100%; + background-color: #444444; + color: #f0f0f0; + font-family: monospace; + font-size: 15px; + grid-template-columns: auto 35em auto; + grid-template-rows: 4em; + grid-template-areas: ". info ."; +} + +footer span { + grid-area: info; + margin-top: 1.5em; + text-align: center; +} + +footer span a { + color: #f0f0f0; +} diff --git a/templates/index.html.tera b/templates/index.html.tera index bdf1699..cdc49f6 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -16,17 +16,24 @@
+ {% for post in posts %}
{ image:
-

title: Foo bar

-

Article: Lorem Ipsum

+

title: {{ post.title }}

+

Article: {{ post.content | split(pat=" ") | slice(end=100) | join(sep=" ") }}

+ {% endfor %}
+