now display the posts saved on the DB

pull/1/head
kirbylife 2019-11-13 20:10:42 -06:00
parent 7f8d700a64
commit 64fe3d5f03
3 changed files with 77 additions and 8 deletions

View File

@ -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<Post> {
use schema::posts::dsl::*;
let connection = establish_connection();
posts
.filter(published.eq(true))
.limit(20)
.load::<Post>(&connection)
.expect("Error loading posts")
}
#[get("/?<page>")]
fn index(page: Option<i8>) -> 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)
}

View File

@ -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;
}

View File

@ -16,17 +16,24 @@
</ul>
</nav>
<section>
{% for post in posts %}
<article>
<span><strong>{ image: </strong></span>
<figure>
<img src="https://files.realpython.com/media/MProc.7cf3be371bbc.png">
</figure>
<p class="article-title"><strong>title: </strong>Foo bar</p>
<p class="article-body"><strong>Article: </strong>Lorem Ipsum</p>
<p class="article-title"><strong>title: </strong>{{ post.title }}</p>
<p class="article-body"><strong>Article: </strong>{{ post.content | split(pat=" ") | slice(end=100) | join(sep=" ") }}</p>
<div class="article-info">
<span><strong>Author: </strong>kirbylife <strong>Date: </strong>12/11/2019<strong> }</strong></span>
<span><strong>Author: </strong>kirbylife <strong>Date: </strong>{{ post.created_at | date(format="%Y-%m-%d") }}<strong> }</strong></span>
</div>
</article>
{% endfor %}
</section>
<footer>
<span>
Hecho con Rust, Rocket y Emacs por <a href="https://kirbylife.gitlab.io">@kirbylife</a>
</span>
</footer>
</body>
</html>