2019-11-13 07:17:38 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
|
2019-12-08 23:07:49 +00:00
|
|
|
pub mod admin;
|
2019-12-15 23:12:07 +00:00
|
|
|
pub mod controllers;
|
2019-11-25 20:59:42 +00:00
|
|
|
pub mod misc;
|
2019-11-14 02:10:42 +00:00
|
|
|
pub mod models;
|
2019-12-17 23:02:10 +00:00
|
|
|
mod schema;
|
2019-11-14 02:10:42 +00:00
|
|
|
|
2019-11-13 07:17:38 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
2019-11-14 02:10:42 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2019-11-18 08:14:29 +00:00
|
|
|
extern crate comrak;
|
2019-11-14 02:10:42 +00:00
|
|
|
extern crate dotenv;
|
2019-11-13 07:17:38 +00:00
|
|
|
extern crate rocket_contrib;
|
2019-11-14 02:10:42 +00:00
|
|
|
extern crate tera;
|
2019-11-13 07:17:38 +00:00
|
|
|
|
2019-11-18 08:14:29 +00:00
|
|
|
use comrak::{markdown_to_html, ComrakOptions};
|
2019-12-16 00:34:27 +00:00
|
|
|
use controllers::posts;
|
2019-12-17 23:02:10 +00:00
|
|
|
use dotenv::dotenv;
|
2019-11-25 20:59:42 +00:00
|
|
|
use misc::get_context;
|
2023-12-11 04:53:43 +00:00
|
|
|
use rocket::response::content;
|
2019-12-15 23:24:19 +00:00
|
|
|
use rocket::response::NamedFile;
|
2019-11-23 19:05:41 +00:00
|
|
|
use rocket::Request;
|
2019-11-13 07:17:38 +00:00
|
|
|
use rocket_contrib::serve::StaticFiles;
|
|
|
|
use rocket_contrib::templates::Template;
|
2019-12-15 23:24:19 +00:00
|
|
|
use std::path::Path;
|
2019-11-14 02:10:42 +00:00
|
|
|
use std::vec::Vec;
|
2019-11-25 20:59:42 +00:00
|
|
|
|
2019-11-14 02:10:42 +00:00
|
|
|
#[get("/?<page>")]
|
2019-12-16 00:34:27 +00:00
|
|
|
fn index(page: Option<u64>) -> Template {
|
|
|
|
let page: u64 = page.unwrap_or(1);
|
2019-11-14 02:10:42 +00:00
|
|
|
|
2019-11-25 20:59:42 +00:00
|
|
|
let mut context = get_context();
|
2019-11-14 02:10:42 +00:00
|
|
|
|
2019-12-19 00:32:45 +00:00
|
|
|
let (posts, n_posts) = posts::get_posts(Some(page));
|
2019-11-17 05:10:06 +00:00
|
|
|
|
2019-12-16 00:34:27 +00:00
|
|
|
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;
|
2019-11-17 05:10:06 +00:00
|
|
|
|
|
|
|
context.insert("posts", &posts);
|
|
|
|
context.insert("total_pages", &total_pages);
|
|
|
|
context.insert("actual_page", &page);
|
2019-11-13 07:17:38 +00:00
|
|
|
Template::render("index", context)
|
|
|
|
}
|
|
|
|
|
2023-12-11 04:53:43 +00:00
|
|
|
#[get("/feed.xml")]
|
|
|
|
fn rss_feed() -> content::Xml<Template> {
|
|
|
|
let mut context = get_context();
|
|
|
|
let (mut posts, _) = posts::get_posts(None);
|
|
|
|
|
|
|
|
let comrak_options = ComrakOptions {
|
|
|
|
ext_table: true,
|
|
|
|
ext_autolink: true,
|
|
|
|
ext_tasklist: true,
|
|
|
|
unsafe_: true,
|
|
|
|
..ComrakOptions::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
posts.iter_mut().for_each(|post| {
|
|
|
|
let content = markdown_to_html(&post.content, &comrak_options);
|
|
|
|
post.content = content;
|
|
|
|
});
|
|
|
|
|
|
|
|
context.insert("posts", &posts);
|
|
|
|
|
|
|
|
content::Xml(Template::render("rss", context))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/atom.xml")]
|
|
|
|
fn atom_feed() -> content::Xml<Template> {
|
|
|
|
let mut context = get_context();
|
|
|
|
let (mut posts, _) = posts::get_posts(None);
|
|
|
|
|
|
|
|
let comrak_options = ComrakOptions {
|
|
|
|
ext_table: true,
|
|
|
|
ext_autolink: true,
|
|
|
|
ext_tasklist: true,
|
|
|
|
unsafe_: true,
|
|
|
|
..ComrakOptions::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
posts.iter_mut().for_each(|post| {
|
|
|
|
let content = markdown_to_html(&post.content, &comrak_options);
|
|
|
|
post.content = content;
|
|
|
|
});
|
|
|
|
|
|
|
|
context.insert("posts", &posts);
|
|
|
|
|
|
|
|
content::Xml(Template::render("atom", context))
|
|
|
|
}
|
|
|
|
|
2019-11-18 08:16:16 +00:00
|
|
|
#[get("/post/<title>")]
|
2019-11-17 22:09:36 +00:00
|
|
|
fn show_post(title: String) -> Template {
|
2019-11-25 20:59:42 +00:00
|
|
|
let mut context = get_context();
|
2019-11-17 22:09:36 +00:00
|
|
|
|
|
|
|
let title_splited: Vec<&str> = title.split('-').collect();
|
2019-12-15 23:12:07 +00:00
|
|
|
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
2019-11-17 22:09:36 +00:00
|
|
|
|
2019-12-16 00:34:27 +00:00
|
|
|
match posts::get_post(id) {
|
2019-11-25 23:42:57 +00:00
|
|
|
Ok(mut post) => {
|
2019-12-21 09:55:21 +00:00
|
|
|
let comrak_options = ComrakOptions {
|
|
|
|
ext_table: true,
|
|
|
|
ext_autolink: true,
|
|
|
|
ext_tasklist: true,
|
2023-01-27 06:42:30 +00:00
|
|
|
unsafe_: true,
|
2019-12-21 09:55:21 +00:00
|
|
|
..ComrakOptions::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let content = markdown_to_html(&post.content, &comrak_options);
|
2019-11-25 23:42:57 +00:00
|
|
|
post.content = content;
|
2019-11-17 22:09:36 +00:00
|
|
|
|
2019-11-25 23:42:57 +00:00
|
|
|
context.insert("post", &post);
|
2019-11-24 04:44:11 +00:00
|
|
|
Template::render("post", context)
|
|
|
|
}
|
2019-12-15 23:12:07 +00:00
|
|
|
Err(_) => {
|
2019-11-24 04:44:11 +00:00
|
|
|
let uri = format!("/post/{}", title_splited.join("-"));
|
|
|
|
context.insert("url", &uri);
|
|
|
|
Template::render("404", context)
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:05:41 +00:00
|
|
|
#[catch(404)]
|
|
|
|
fn not_found_404(req: &Request) -> Template {
|
2019-11-25 20:59:42 +00:00
|
|
|
let mut context = get_context();
|
2019-11-23 19:05:41 +00:00
|
|
|
|
|
|
|
let uri = format!("{}", req.uri());
|
|
|
|
|
|
|
|
context.insert("url", &uri);
|
|
|
|
Template::render("404", context)
|
|
|
|
}
|
|
|
|
|
2019-12-15 23:24:19 +00:00
|
|
|
#[get("/favicon.ico")]
|
|
|
|
fn favicon() -> Option<NamedFile> {
|
|
|
|
NamedFile::open(Path::new("static/favicon.ico")).ok()
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:57:32 +00:00
|
|
|
fn main() {
|
2019-12-17 23:02:10 +00:00
|
|
|
dotenv().ok();
|
|
|
|
|
2019-11-13 07:17:38 +00:00
|
|
|
rocket::ignite()
|
|
|
|
.attach(Template::fairing())
|
2023-12-11 04:53:43 +00:00
|
|
|
.mount("/", routes![index, show_post, favicon, rss_feed, atom_feed])
|
2019-12-17 23:02:10 +00:00
|
|
|
.mount("/admin", admin::get_routes())
|
2019-11-13 07:17:38 +00:00
|
|
|
.mount("/static", StaticFiles::from("static"))
|
2019-11-23 19:05:41 +00:00
|
|
|
.register(catchers![not_found_404])
|
2019-11-13 07:17:38 +00:00
|
|
|
.launch();
|
2019-11-13 01:57:32 +00:00
|
|
|
}
|