fixed pub mod imports
parent
279c236401
commit
68aa90d2e1
121
src/admin.rs
121
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<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();
|
||||
|
||||
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]
|
||||
}
|
||||
|
|
|
@ -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<Post>, 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<Post, Error> {
|
||||
use schema::posts::dsl::*;
|
||||
use crate::schema::posts::dsl::*;
|
||||
|
||||
let connection = establish_connection();
|
||||
let post = posts.find(id_number).first(&connection);
|
||||
|
|
|
@ -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<NamedFile> {
|
|||
}
|
||||
|
||||
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();
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
extern crate chrono;
|
||||
extern crate serde;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::Serialize;
|
||||
|
||||
|
|
|
@ -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 %}
|
|
@ -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 %}
|
|
@ -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 %}
|
Loading…
Reference in New Issue