2019-12-17 23:02:10 +00:00
|
|
|
use crate::controllers::posts;
|
|
|
|
use crate::misc::get_context;
|
2019-12-18 09:20:00 +00:00
|
|
|
use crate::models::NewPost;
|
2019-12-17 23:02:10 +00:00
|
|
|
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;
|
2019-12-15 23:41:56 +00:00
|
|
|
|
2019-12-17 23:02:10 +00:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct Login {
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
}
|
2019-12-15 23:41:56 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
2019-12-17 23:02:10 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
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 {
|
2019-12-19 00:32:45 +00:00
|
|
|
let mut context = get_context();
|
|
|
|
|
|
|
|
let (all_posts, n_posts) = posts::get_posts(None);
|
2019-12-17 23:02:10 +00:00
|
|
|
|
2019-12-19 00:32:45 +00:00
|
|
|
context.insert("posts", &all_posts);
|
|
|
|
context.insert("n_posts", &n_posts);
|
2019-12-17 23:02:10 +00:00
|
|
|
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))
|
2019-12-21 09:55:39 +00:00
|
|
|
} else if cookie.value() == "123" {
|
|
|
|
Err(Redirect::to("/admin"))
|
2019-12-17 23:02:10 +00:00
|
|
|
} else {
|
|
|
|
Err(Redirect::to("/admin"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Redirect::to("/admin")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/add_post", data = "<post>")]
|
2019-12-19 07:07:14 +00:00
|
|
|
fn write_add_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> Redirect {
|
2019-12-17 23:02:10 +00:00
|
|
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
|
|
|
|
|
|
|
match cookies.get_private("user") {
|
|
|
|
Some(cookie) => {
|
|
|
|
if cookie.value() == password {
|
2019-12-18 09:20:00 +00:00
|
|
|
match posts::add_post(&post) {
|
|
|
|
Ok(post) => Redirect::to(format!("/post/{}", post.id)),
|
|
|
|
Err(_) => Redirect::to("admin/add_post"),
|
|
|
|
}
|
2019-12-17 23:02:10 +00:00
|
|
|
} else {
|
2019-12-18 09:20:00 +00:00
|
|
|
Redirect::to("/admin")
|
2019-12-17 23:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 09:20:00 +00:00
|
|
|
None => Redirect::to("/admin"),
|
2019-12-17 23:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 23:41:56 +00:00
|
|
|
|
2019-12-19 07:07:14 +00:00
|
|
|
#[get("/edit_post/<id>")]
|
|
|
|
fn edit_post(mut cookies: Cookies<'_>, id: i32) -> 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 mut context = get_context();
|
|
|
|
|
|
|
|
let post = posts::get_post(id).unwrap();
|
|
|
|
|
2019-12-21 09:55:39 +00:00
|
|
|
context.insert("post", &post);
|
2019-12-19 07:07:14 +00:00
|
|
|
Ok(Template::render("admin/edit_post", context))
|
|
|
|
} else {
|
|
|
|
Err(Redirect::to("/admin"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Redirect::to("/admin")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/edit_post/<id>", data = "<post>")]
|
|
|
|
fn write_edit_post(mut cookies: Cookies<'_>, id: i32, post: Form<NewPost>) -> Redirect {
|
|
|
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
|
|
|
|
|
|
|
match cookies.get_private("user") {
|
|
|
|
Some(cookie) => {
|
|
|
|
if cookie.value() == password {
|
|
|
|
match posts::edit_post(id, &post) {
|
|
|
|
Ok(post) => Redirect::to(format!("/post/{}", post.id)),
|
2019-12-19 22:26:10 +00:00
|
|
|
Err(_) => Redirect::to("admin/edit_post"),
|
2019-12-19 07:07:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Redirect::to("/admin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Redirect::to("/admin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 23:02:10 +00:00
|
|
|
pub fn get_routes() -> Vec<Route> {
|
2019-12-19 07:07:14 +00:00
|
|
|
routes![
|
|
|
|
index,
|
|
|
|
login,
|
|
|
|
panel,
|
|
|
|
add_post,
|
|
|
|
write_add_post,
|
|
|
|
edit_post,
|
|
|
|
write_edit_post
|
|
|
|
]
|
2019-12-15 23:41:56 +00:00
|
|
|
}
|