Migrate to Rocket 0.5.0
parent
065eade541
commit
dedaed2281
12
Cargo.toml
12
Cargo.toml
|
@ -2,19 +2,15 @@
|
||||||
name = "codigocomentado"
|
name = "codigocomentado"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["kirbylife <gabriel13m@gmail.com>"]
|
authors = ["kirbylife <gabriel13m@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = "0.4.11"
|
rocket = { version = "0.5.0", features = ["secrets"] }
|
||||||
|
rocket_dyn_templates = { version = "0.1.0", features = ["tera"] }
|
||||||
diesel = { version = "1.0.0", features = ["postgres", "chrono"] }
|
diesel = { version = "1.0.0", features = ["postgres", "chrono"] }
|
||||||
dotenv = "0.9.0"
|
dotenv = "0.9.0"
|
||||||
rand = "0.6.0"
|
rand = "0.6.0"
|
||||||
chrono = { version = "0.4.9", features = ["serde"] }
|
chrono = { version = "0.4.9", features = ["serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
tera = "0.11"
|
|
||||||
comrak = "0.6"
|
comrak = "0.6"
|
||||||
|
tera = "1.19.1"
|
||||||
[dependencies.rocket_contrib]
|
|
||||||
version = "0.4.2"
|
|
||||||
default-features = false
|
|
||||||
features = ["json", "serve", "handlebars_templates", "tera_templates"]
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
[default]
|
||||||
|
address = "0.0.0.0"
|
||||||
|
limits = { form = "100 MiB", json = "100 MiB" }
|
||||||
|
port = 8000
|
||||||
|
|
||||||
|
[release]
|
||||||
|
secret_key = "openssl rand -base64 32"
|
|
@ -0,0 +1 @@
|
||||||
|
edition = "2021"
|
41
src/admin.rs
41
src/admin.rs
|
@ -1,11 +1,11 @@
|
||||||
|
use crate::contexts;
|
||||||
use crate::controllers::posts;
|
use crate::controllers::posts;
|
||||||
use crate::misc::get_context;
|
|
||||||
use crate::models::NewPost;
|
use crate::models::NewPost;
|
||||||
use rocket::http::{Cookie, Cookies};
|
use rocket::form::Form;
|
||||||
use rocket::request::Form;
|
use rocket::http::{Cookie, CookieJar};
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::Route;
|
use rocket::Route;
|
||||||
use rocket_contrib::templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
@ -16,20 +16,20 @@ struct Login {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(mut cookies: Cookies<'_>) -> Result<Redirect, Template> {
|
fn index(cookies: &CookieJar<'_>) -> Result<Redirect, Template> {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
if cookie.value() == password {
|
if cookie.value() == password {
|
||||||
Ok(Redirect::to("/admin/panel"))
|
Ok(Redirect::to("/admin/panel"))
|
||||||
} else {
|
} else {
|
||||||
let context = get_context();
|
let context = contexts::Empty::new();
|
||||||
|
|
||||||
Err(Template::render("admin/index", context))
|
Err(Template::render("admin/index", context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let context = get_context();
|
let context = contexts::Empty::new();
|
||||||
|
|
||||||
Err(Template::render("admin/index", context))
|
Err(Template::render("admin/index", context))
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ fn index(mut cookies: Cookies<'_>) -> Result<Redirect, Template> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<login>")]
|
#[post("/", data = "<login>")]
|
||||||
fn login(mut cookies: Cookies<'_>, login: Form<Login>) -> Redirect {
|
fn login(cookies: &CookieJar<'_>, login: Form<Login>) -> Redirect {
|
||||||
let username = env::var("admin_user").expect("admin_user not setted");
|
let username = env::var("admin_user").expect("admin_user not setted");
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
|
@ -50,18 +50,16 @@ fn login(mut cookies: Cookies<'_>, login: Form<Login>) -> Redirect {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/panel")]
|
#[get("/panel")]
|
||||||
fn panel(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
fn panel(cookies: &CookieJar<'_>) -> Result<Template, Redirect> {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
if cookie.value() == password {
|
if cookie.value() == password {
|
||||||
let mut context = get_context();
|
|
||||||
|
|
||||||
let (all_posts, n_posts) = posts::get_posts(None);
|
let (all_posts, n_posts) = posts::get_posts(None);
|
||||||
|
|
||||||
context.insert("posts", &all_posts);
|
let context = contexts::AllPosts::new(all_posts, n_posts);
|
||||||
context.insert("n_posts", &n_posts);
|
|
||||||
Ok(Template::render("admin/panel", context))
|
Ok(Template::render("admin/panel", context))
|
||||||
} else {
|
} else {
|
||||||
Err(Redirect::to("/admin"))
|
Err(Redirect::to("/admin"))
|
||||||
|
@ -72,17 +70,15 @@ fn panel(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/add_post")]
|
#[get("/add_post")]
|
||||||
fn add_post(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
fn add_post(cookies: &CookieJar<'_>) -> Result<Template, Redirect> {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
if cookie.value() == password {
|
if cookie.value() == password {
|
||||||
let context = get_context();
|
let context = contexts::Empty::new();
|
||||||
|
|
||||||
Ok(Template::render("admin/add_post", context))
|
Ok(Template::render("admin/add_post", context))
|
||||||
} else if cookie.value() == "123" {
|
|
||||||
Err(Redirect::to("/admin"))
|
|
||||||
} else {
|
} else {
|
||||||
Err(Redirect::to("/admin"))
|
Err(Redirect::to("/admin"))
|
||||||
}
|
}
|
||||||
|
@ -92,7 +88,7 @@ fn add_post(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/add_post", data = "<post>")]
|
#[post("/add_post", data = "<post>")]
|
||||||
fn write_add_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> Redirect {
|
fn write_add_post(cookies: &CookieJar<'_>, post: Form<NewPost>) -> Redirect {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
|
@ -111,17 +107,16 @@ fn write_add_post(mut cookies: Cookies<'_>, post: Form<NewPost>) -> Redirect {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/edit_post/<id>")]
|
#[get("/edit_post/<id>")]
|
||||||
fn edit_post(mut cookies: Cookies<'_>, id: i32) -> Result<Template, Redirect> {
|
fn edit_post(cookies: &CookieJar<'_>, id: i32) -> Result<Template, Redirect> {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
if cookie.value() == password {
|
if cookie.value() == password {
|
||||||
let mut context = get_context();
|
|
||||||
|
|
||||||
let post = posts::get_post(id).unwrap();
|
let post = posts::get_post(id).unwrap();
|
||||||
|
|
||||||
context.insert("post", &post);
|
let context = contexts::SinglePost::new(post);
|
||||||
|
|
||||||
Ok(Template::render("admin/edit_post", context))
|
Ok(Template::render("admin/edit_post", context))
|
||||||
} else {
|
} else {
|
||||||
Err(Redirect::to("/admin"))
|
Err(Redirect::to("/admin"))
|
||||||
|
@ -132,7 +127,7 @@ fn edit_post(mut cookies: Cookies<'_>, id: i32) -> Result<Template, Redirect> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/edit_post/<id>", data = "<post>")]
|
#[post("/edit_post/<id>", data = "<post>")]
|
||||||
fn write_edit_post(mut cookies: Cookies<'_>, id: i32, post: Form<NewPost>) -> Redirect {
|
fn write_edit_post(cookies: &CookieJar<'_>, id: i32, post: Form<NewPost>) -> Redirect {
|
||||||
let password = env::var("admin_pass").expect("admin_pass not setted");
|
let password = env::var("admin_pass").expect("admin_pass not setted");
|
||||||
|
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
use crate::controllers::posts;
|
||||||
|
use crate::misc::gen_title;
|
||||||
|
use crate::models::Post;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct PageOfPosts {
|
||||||
|
title: String,
|
||||||
|
posts: Vec<Post>,
|
||||||
|
total_pages: i64,
|
||||||
|
actual_page: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PageOfPosts {
|
||||||
|
pub fn new(posts: Vec<Post>, actual_page: u64, n_posts: i64) -> Self {
|
||||||
|
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;
|
||||||
|
|
||||||
|
PageOfPosts {
|
||||||
|
title: gen_title(),
|
||||||
|
posts,
|
||||||
|
total_pages,
|
||||||
|
actual_page,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct XmlFeed {
|
||||||
|
posts: Vec<Post>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl XmlFeed {
|
||||||
|
pub fn new(posts: Vec<Post>) -> Self {
|
||||||
|
XmlFeed { posts }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct SinglePost {
|
||||||
|
title: String,
|
||||||
|
post: Post,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SinglePost {
|
||||||
|
pub fn new(post: Post) -> Self {
|
||||||
|
SinglePost {
|
||||||
|
title: gen_title(),
|
||||||
|
post,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct NotFound {
|
||||||
|
title: String,
|
||||||
|
uri: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NotFound {
|
||||||
|
pub fn new(uri: String) -> Self {
|
||||||
|
NotFound {
|
||||||
|
title: gen_title(),
|
||||||
|
uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct AllPosts {
|
||||||
|
title: String,
|
||||||
|
posts: Vec<Post>,
|
||||||
|
n_posts: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AllPosts {
|
||||||
|
pub fn new(posts: Vec<Post>, n_posts: i64) -> Self {
|
||||||
|
AllPosts {
|
||||||
|
title: gen_title(),
|
||||||
|
posts,
|
||||||
|
n_posts,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct Empty {
|
||||||
|
title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Empty {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Empty { title: gen_title() }
|
||||||
|
}
|
||||||
|
}
|
64
src/main.rs
64
src/main.rs
|
@ -1,6 +1,5 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
|
||||||
|
|
||||||
pub mod admin;
|
pub mod admin;
|
||||||
|
pub mod contexts;
|
||||||
pub mod controllers;
|
pub mod controllers;
|
||||||
pub mod misc;
|
pub mod misc;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
@ -10,20 +9,14 @@ mod schema;
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
extern crate comrak;
|
|
||||||
extern crate dotenv;
|
|
||||||
extern crate rocket_contrib;
|
|
||||||
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 dotenv::dotenv;
|
||||||
use misc::get_context;
|
use rocket::fs::{FileServer, NamedFile};
|
||||||
use rocket::response::content;
|
use rocket::response::content::RawXml;
|
||||||
use rocket::response::NamedFile;
|
|
||||||
use rocket::Request;
|
use rocket::Request;
|
||||||
use rocket_contrib::serve::StaticFiles;
|
use rocket_dyn_templates::Template;
|
||||||
use rocket_contrib::templates::Template;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
@ -31,21 +24,15 @@ use std::vec::Vec;
|
||||||
fn index(page: Option<u64>) -> Template {
|
fn index(page: Option<u64>) -> Template {
|
||||||
let page: u64 = page.unwrap_or(1);
|
let page: u64 = page.unwrap_or(1);
|
||||||
|
|
||||||
let mut context = get_context();
|
|
||||||
|
|
||||||
let (posts, n_posts) = posts::get_posts(Some(page));
|
let (posts, n_posts) = posts::get_posts(Some(page));
|
||||||
|
|
||||||
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;
|
let context = contexts::PageOfPosts::new(posts, page, n_posts);
|
||||||
|
|
||||||
context.insert("posts", &posts);
|
|
||||||
context.insert("total_pages", &total_pages);
|
|
||||||
context.insert("actual_page", &page);
|
|
||||||
Template::render("index", context)
|
Template::render("index", context)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/feed.xml")]
|
#[get("/feed.xml")]
|
||||||
fn rss_feed() -> content::Xml<Template> {
|
fn rss_feed() -> RawXml<Template> {
|
||||||
let mut context = get_context();
|
|
||||||
let (mut posts, _) = posts::get_posts(None);
|
let (mut posts, _) = posts::get_posts(None);
|
||||||
|
|
||||||
let comrak_options = ComrakOptions {
|
let comrak_options = ComrakOptions {
|
||||||
|
@ -61,14 +48,13 @@ fn rss_feed() -> content::Xml<Template> {
|
||||||
post.content = content;
|
post.content = content;
|
||||||
});
|
});
|
||||||
|
|
||||||
context.insert("posts", &posts);
|
let context = contexts::XmlFeed::new(posts);
|
||||||
|
|
||||||
content::Xml(Template::render("rss", context))
|
RawXml(Template::render("rss", context))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/atom.xml")]
|
#[get("/atom.xml")]
|
||||||
fn atom_feed() -> content::Xml<Template> {
|
fn atom_feed() -> RawXml<Template> {
|
||||||
let mut context = get_context();
|
|
||||||
let (mut posts, _) = posts::get_posts(None);
|
let (mut posts, _) = posts::get_posts(None);
|
||||||
|
|
||||||
let comrak_options = ComrakOptions {
|
let comrak_options = ComrakOptions {
|
||||||
|
@ -84,15 +70,13 @@ fn atom_feed() -> content::Xml<Template> {
|
||||||
post.content = content;
|
post.content = content;
|
||||||
});
|
});
|
||||||
|
|
||||||
context.insert("posts", &posts);
|
let context = contexts::XmlFeed::new(posts);
|
||||||
|
|
||||||
content::Xml(Template::render("atom", context))
|
RawXml(Template::render("atom", context))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/post/<title>")]
|
#[get("/post/<title>")]
|
||||||
fn show_post(title: String) -> Template {
|
fn show_post(title: String) -> Template {
|
||||||
let mut context = get_context();
|
|
||||||
|
|
||||||
let title_splited: Vec<&str> = title.split('-').collect();
|
let title_splited: Vec<&str> = title.split('-').collect();
|
||||||
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
||||||
|
|
||||||
|
@ -109,12 +93,15 @@ fn show_post(title: String) -> Template {
|
||||||
let content = markdown_to_html(&post.content, &comrak_options);
|
let content = markdown_to_html(&post.content, &comrak_options);
|
||||||
post.content = content;
|
post.content = content;
|
||||||
|
|
||||||
context.insert("post", &post);
|
let context = contexts::SinglePost::new(post);
|
||||||
|
|
||||||
Template::render("post", context)
|
Template::render("post", context)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let uri = format!("/post/{}", title_splited.join("-"));
|
let uri = format!("/post/{}", title_splited.join("-"));
|
||||||
context.insert("url", &uri);
|
|
||||||
|
let context = contexts::NotFound::new(uri);
|
||||||
|
|
||||||
Template::render("404", context)
|
Template::render("404", context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,27 +109,26 @@ fn show_post(title: String) -> Template {
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found_404(req: &Request) -> Template {
|
fn not_found_404(req: &Request) -> Template {
|
||||||
let mut context = get_context();
|
|
||||||
|
|
||||||
let uri = format!("{}", req.uri());
|
let uri = format!("{}", req.uri());
|
||||||
|
|
||||||
context.insert("url", &uri);
|
let context = contexts::NotFound::new(uri);
|
||||||
|
|
||||||
Template::render("404", context)
|
Template::render("404", context)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/favicon.ico")]
|
#[get("/favicon.ico")]
|
||||||
fn favicon() -> Option<NamedFile> {
|
async fn favicon() -> Option<NamedFile> {
|
||||||
NamedFile::open(Path::new("static/favicon.ico")).ok()
|
NamedFile::open(Path::new("static/favicon.ico")).await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
#[launch]
|
||||||
|
fn rocket() -> _ {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
rocket::ignite()
|
rocket::build()
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![index, show_post, favicon, rss_feed, atom_feed])
|
.mount("/", routes![index, show_post, favicon, rss_feed, atom_feed])
|
||||||
.mount("/admin", admin::get_routes())
|
.mount("/admin", admin::get_routes())
|
||||||
.mount("/static", StaticFiles::from("static"))
|
.mount("/static", FileServer::from("static"))
|
||||||
.register(catchers![not_found_404])
|
.register("/", catchers![not_found_404])
|
||||||
.launch();
|
|
||||||
}
|
}
|
||||||
|
|
11
src/misc.rs
11
src/misc.rs
|
@ -1,7 +1,4 @@
|
||||||
extern crate tera;
|
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tera::Context;
|
|
||||||
|
|
||||||
const TITLES: [&str; 11] = [
|
const TITLES: [&str; 11] = [
|
||||||
"/* {} */",
|
"/* {} */",
|
||||||
|
@ -17,13 +14,9 @@ const TITLES: [&str; 11] = [
|
||||||
"(* {} *)",
|
"(* {} *)",
|
||||||
];
|
];
|
||||||
|
|
||||||
pub fn get_context() -> Context {
|
pub fn gen_title() -> String {
|
||||||
let mut context = Context::new();
|
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
|
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
|
||||||
let title = str::replace(title_fmt, "{}", "CódigoComentado");
|
let title = str::replace(title_fmt, "{}", "CódigoComentado");
|
||||||
|
title.to_string()
|
||||||
context.insert("title", &title);
|
|
||||||
context
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue