fixed pub mod imports

pull/1/head
kirbylife 2019-12-17 17:02:10 -06:00
parent 279c236401
commit 68aa90d2e1
8 changed files with 166 additions and 26 deletions

View File

@ -1,15 +1,116 @@
#[path = "misc.rs"] use crate::controllers::posts;
pub mod misc; use crate::misc::get_context;
use rocket::http::{Cookie, Cookies};
extern crate rocket; use rocket::request::Form;
extern crate rocket_contrib; use rocket::response::Redirect;
use rocket::Route;
use misc::get_context;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use std::env;
use std::vec::Vec;
#[derive(FromForm)]
struct Login {
username: String,
password: String,
}
#[derive(FromForm, Debug)]
struct Post {
pub title: String,
pub content: String,
pub published: bool,
}
#[get("/")] #[get("/")]
pub fn index() -> Template { fn index(mut cookies: Cookies<'_>) -> Result<Redirect, Template> {
let password = env::var("admin_pass").expect("admin_pass not setted");
match cookies.get_private("user") {
Some(cookie) => {
if cookie.value() == password {
Ok(Redirect::to("/admin/panel"))
} else {
let context = get_context(); let context = get_context();
Template::render("base", context) Err(Template::render("admin/index", context))
}
}
None => {
let context = get_context();
Err(Template::render("admin/index", context))
}
}
}
#[post("/", data = "<login>")]
fn login(mut cookies: Cookies<'_>, login: Form<Login>) -> Redirect {
let username = env::var("admin_user").expect("admin_user not setted");
let password = env::var("admin_pass").expect("admin_pass not setted");
if login.username == username && login.password == password {
cookies.add_private(Cookie::new("user", password));
Redirect::to("/admin/panel")
} else {
Redirect::to("/admin")
}
}
#[get("/panel")]
fn panel(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
let password = env::var("admin_pass").expect("admin_pass not setted");
match cookies.get_private("user") {
Some(cookie) => {
if cookie.value() == password {
let context = get_context();
Ok(Template::render("admin/panel", context))
} else {
Err(Redirect::to("/admin"))
}
}
None => Err(Redirect::to("/admin")),
}
}
#[get("/add_post")]
fn add_post(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
let password = env::var("admin_pass").expect("admin_pass not setted");
match cookies.get_private("user") {
Some(cookie) => {
if cookie.value() == password {
let context = get_context();
Ok(Template::render("admin/add_post", context))
} else {
Err(Redirect::to("/admin"))
}
}
None => Err(Redirect::to("/admin")),
}
}
#[post("/add_post", data = "<post>")]
fn write_post(mut cookies: Cookies<'_>, post: Form<Post>) -> Result<Template, Redirect> {
let password = env::var("admin_pass").expect("admin_pass not setted");
match cookies.get_private("user") {
Some(cookie) => {
if cookie.value() == password {
let context = get_context();
println!("{:?}", post);
Ok(Template::render("admin/add_post", context))
} else {
Err(Redirect::to("/admin"))
}
}
None => Err(Redirect::to("/admin")),
}
}
pub fn get_routes() -> Vec<Route> {
routes![index, login, panel, add_post, write_post]
} }

View File

@ -1,21 +1,13 @@
#[path = "models.rs"]
pub mod models;
#[path = "schema.rs"]
pub mod schema;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::result::Error; use diesel::result::Error;
use diesel::Connection; use diesel::Connection;
use diesel::ExpressionMethods; use diesel::ExpressionMethods;
use diesel::QueryDsl; use diesel::QueryDsl;
use diesel::RunQueryDsl; use diesel::RunQueryDsl;
use dotenv::dotenv;
use std::env; use std::env;
use std::vec::Vec; use std::vec::Vec;
fn establish_connection() -> PgConnection { fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL not setted"); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL not setted");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
@ -23,12 +15,12 @@ fn establish_connection() -> PgConnection {
pub mod posts { pub mod posts {
use crate::controllers::*; use crate::controllers::*;
use models::Post; use crate::models::Post;
pub const MAX_POSTS_PER_PAGE: u64 = 20; pub const MAX_POSTS_PER_PAGE: u64 = 20;
pub fn get_posts(page: u64) -> (Vec<Post>, i64) { pub fn get_posts(page: u64) -> (Vec<Post>, i64) {
use schema::posts::dsl::*; use crate::schema::posts::dsl::*;
let connection = establish_connection(); let connection = establish_connection();
let visible_posts = posts let visible_posts = posts
@ -49,7 +41,7 @@ pub mod posts {
} }
pub fn get_post(id_number: i32) -> Result<Post, Error> { pub fn get_post(id_number: i32) -> Result<Post, Error> {
use schema::posts::dsl::*; use crate::schema::posts::dsl::*;
let connection = establish_connection(); let connection = establish_connection();
let post = posts.find(id_number).first(&connection); let post = posts.find(id_number).first(&connection);

View File

@ -4,7 +4,7 @@ pub mod admin;
pub mod controllers; pub mod controllers;
pub mod misc; pub mod misc;
pub mod models; pub mod models;
pub mod schema; mod schema;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
@ -17,6 +17,7 @@ extern crate tera;
use comrak::{markdown_to_html, ComrakOptions}; use comrak::{markdown_to_html, ComrakOptions};
use controllers::posts; use controllers::posts;
use dotenv::dotenv;
use misc::get_context; use misc::get_context;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket::Request; use rocket::Request;
@ -80,10 +81,12 @@ fn favicon() -> Option<NamedFile> {
} }
fn main() { fn main() {
dotenv().ok();
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
.mount("/", routes![index, show_post, favicon]) .mount("/", routes![index, show_post, favicon])
.mount("/admin", routes![admin::index]) .mount("/admin", admin::get_routes())
.mount("/static", StaticFiles::from("static")) .mount("/static", StaticFiles::from("static"))
.register(catchers![not_found_404]) .register(catchers![not_found_404])
.launch(); .launch();

View File

@ -1,6 +1,3 @@
extern crate chrono;
extern crate serde;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::Serialize; use serde::Serialize;

View File

@ -0,0 +1,17 @@
{% extends "base" %}
{% block content %}
<form action="" method="post">
<p>
<label for="">Título</label>
<input name="title" type="text"/>
<label for="">Publicar</label>
<input name="published" type="checkbox" value="true"/>
</p>
<p>
<label for="">Contenido</label>
<textarea cols="30" name="content" rows="10"></textarea>
</p>
<button>Publicar</button>
</form>
{% endblock content %}

View File

@ -0,0 +1,20 @@
{% extends "base" %}
{% block extracss %}
<style>
.container {
text-align: center;
}
</style>
{% endblock extracss %}
{% block content %}
<form action="" method="post">
<h2>Usuario</h2>
<input name="username" type="text" value=""/>
<h2>Contraseña</h2>
<input name="password" type="password" value=""/>
<h2></h2>
<button>Inicia sesión</button>
</form>
{% endblock content %}

View File

@ -0,0 +1,10 @@
{% extends "base" %}
{%block content %}
<h1>Panel de administración</h1>
<h3>
<a href="/admin/add_post">Agregar artículo</a>
<a href="">Editar artículo</a>
</h3>
{%endblock content %}