From 68aa90d2e177a0ca09d758d1c1b066f072d55204 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 17 Dec 2019 17:02:10 -0600 Subject: [PATCH] fixed pub mod imports --- src/admin.rs | 121 ++++++++++++++++++++++++++--- src/controllers.rs | 14 +--- src/main.rs | 7 +- src/models.rs | 3 - templates/admin/add_post.html.tera | 17 ++++ templates/admin/index.html.tera | 20 +++++ templates/admin/index.tera.html | 0 templates/admin/panel.html.tera | 10 +++ 8 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 templates/admin/add_post.html.tera create mode 100644 templates/admin/index.html.tera delete mode 100644 templates/admin/index.tera.html create mode 100644 templates/admin/panel.html.tera diff --git a/src/admin.rs b/src/admin.rs index 5e35c1b..8723c1c 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -1,15 +1,116 @@ -#[path = "misc.rs"] -pub mod misc; - -extern crate rocket; -extern crate rocket_contrib; - -use misc::get_context; +use crate::controllers::posts; +use crate::misc::get_context; +use rocket::http::{Cookie, Cookies}; +use rocket::request::Form; +use rocket::response::Redirect; +use rocket::Route; 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("/")] -pub fn index() -> Template { - let context = get_context(); +fn index(mut cookies: Cookies<'_>) -> Result { + 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(); - Template::render("base", context) + Err(Template::render("admin/index", context)) + } + } + None => { + let context = get_context(); + + Err(Template::render("admin/index", context)) + } + } +} + +#[post("/", data = "")] +fn login(mut cookies: Cookies<'_>, login: Form) -> 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 { + 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 { + 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 = "")] +fn write_post(mut cookies: Cookies<'_>, post: Form) -> Result { + 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 { + routes![index, login, panel, add_post, write_post] } diff --git a/src/controllers.rs b/src/controllers.rs index f8cc464..a75837f 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -1,21 +1,13 @@ -#[path = "models.rs"] -pub mod models; -#[path = "schema.rs"] -pub mod schema; - use diesel::pg::PgConnection; use diesel::result::Error; use diesel::Connection; use diesel::ExpressionMethods; use diesel::QueryDsl; use diesel::RunQueryDsl; -use dotenv::dotenv; use std::env; use std::vec::Vec; 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)) @@ -23,12 +15,12 @@ fn establish_connection() -> PgConnection { pub mod posts { use crate::controllers::*; - use models::Post; + use crate::models::Post; pub const MAX_POSTS_PER_PAGE: u64 = 20; pub fn get_posts(page: u64) -> (Vec, i64) { - use schema::posts::dsl::*; + use crate::schema::posts::dsl::*; let connection = establish_connection(); let visible_posts = posts @@ -49,7 +41,7 @@ pub mod posts { } pub fn get_post(id_number: i32) -> Result { - use schema::posts::dsl::*; + use crate::schema::posts::dsl::*; let connection = establish_connection(); let post = posts.find(id_number).first(&connection); diff --git a/src/main.rs b/src/main.rs index 545897a..c24ed68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ pub mod admin; pub mod controllers; pub mod misc; pub mod models; -pub mod schema; +mod schema; #[macro_use] extern crate rocket; @@ -17,6 +17,7 @@ extern crate tera; use comrak::{markdown_to_html, ComrakOptions}; use controllers::posts; +use dotenv::dotenv; use misc::get_context; use rocket::response::NamedFile; use rocket::Request; @@ -80,10 +81,12 @@ fn favicon() -> Option { } fn main() { + dotenv().ok(); + rocket::ignite() .attach(Template::fairing()) .mount("/", routes![index, show_post, favicon]) - .mount("/admin", routes![admin::index]) + .mount("/admin", admin::get_routes()) .mount("/static", StaticFiles::from("static")) .register(catchers![not_found_404]) .launch(); diff --git a/src/models.rs b/src/models.rs index 3948505..7016c8b 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,6 +1,3 @@ -extern crate chrono; -extern crate serde; - use chrono::NaiveDateTime; use serde::Serialize; diff --git a/templates/admin/add_post.html.tera b/templates/admin/add_post.html.tera new file mode 100644 index 0000000..3d7def1 --- /dev/null +++ b/templates/admin/add_post.html.tera @@ -0,0 +1,17 @@ +{% extends "base" %} + +{% block content %} +
+

+ + + + +

+

+ + +

+ +
+{% endblock content %} diff --git a/templates/admin/index.html.tera b/templates/admin/index.html.tera new file mode 100644 index 0000000..e89c806 --- /dev/null +++ b/templates/admin/index.html.tera @@ -0,0 +1,20 @@ +{% extends "base" %} + +{% block extracss %} + +{% endblock extracss %} + +{% block content %} +
+

Usuario

+ +

Contraseña

+ +

+ +
+{% endblock content %} diff --git a/templates/admin/index.tera.html b/templates/admin/index.tera.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/admin/panel.html.tera b/templates/admin/panel.html.tera new file mode 100644 index 0000000..83858b0 --- /dev/null +++ b/templates/admin/panel.html.tera @@ -0,0 +1,10 @@ +{% extends "base" %} + +{%block content %} +

Panel de administración

+ +

+ Agregar artículo + Editar artículo +

+{%endblock content %}