|
@@ -1,3 +1,4 @@
|
|
|
+mod articles_repository;
|
|
|
mod config;
|
|
|
mod vcs;
|
|
|
|
|
@@ -7,28 +8,16 @@ use actix_web::middleware::Logger;
|
|
|
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
|
|
use fern;
|
|
|
use log::error;
|
|
|
-use std::{env, io};
|
|
|
+use std::{env, fs, io};
|
|
|
|
|
|
+use articles_repository::{ArticlesRepository, FsRepository};
|
|
|
use config::Configuration;
|
|
|
-
|
|
|
-fn get_directory_listing(path: &str) -> Result<Vec<String>, io::Error>
|
|
|
-{
|
|
|
- match std::fs::read_dir(path) {
|
|
|
- Ok(files) => {
|
|
|
- Ok(files
|
|
|
- .into_iter()
|
|
|
- .filter(|dir| dir.as_ref().unwrap().path().starts_with("./."))
|
|
|
- .map(|dir| dir.as_ref().unwrap().path().to_str().unwrap().to_owned())
|
|
|
- .collect::<Vec<String>>())
|
|
|
- },
|
|
|
- Err(error) => {
|
|
|
- Err(error)
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+use pulldown_cmark::{html, Options, Parser};
|
|
|
|
|
|
#[get("")]
|
|
|
async fn show_articles_list() -> impl Responder {
|
|
|
+ let repo: FsRepository = FsRepository {};
|
|
|
+
|
|
|
const CONTENT_FOLDER: &str = "./content";
|
|
|
let estimated_path: String = env::current_dir()
|
|
|
.unwrap_or_default()
|
|
@@ -38,20 +27,32 @@ async fn show_articles_list() -> impl Responder {
|
|
|
|
|
|
log::trace!("Estimated content repository: {estimated_path}/{CONTENT_FOLDER}");
|
|
|
|
|
|
- let paths = match get_directory_listing(&CONTENT_FOLDER) {
|
|
|
- Ok(list) => list.join("\n"),
|
|
|
- Err(error) => {
|
|
|
- error!("Couldn't list articles from path. Error: {error}");
|
|
|
- String::new()
|
|
|
- }
|
|
|
- };
|
|
|
+ let paths = repo.get_directory_listing(&CONTENT_FOLDER).join("\n");
|
|
|
|
|
|
HttpResponse::Ok().body(format!("List of articles:\r\n{paths}"))
|
|
|
}
|
|
|
|
|
|
#[get("/{path:.+}")]
|
|
|
async fn show_article(path: web::Path<(String,)>) -> impl Responder {
|
|
|
- HttpResponse::Ok().body(format!("Specific article: {}", path.into_inner().0))
|
|
|
+ let p = &path.0;
|
|
|
+ let file = fs::read_to_string(p.as_str());
|
|
|
+
|
|
|
+ match file {
|
|
|
+ Ok(content) => {
|
|
|
+ log::trace!("Opening article located {}", p.as_str());
|
|
|
+ let mut options = Options::empty();
|
|
|
+ options.insert(Options::ENABLE_STRIKETHROUGH);
|
|
|
+ let parser = Parser::new_ext(content.as_str(), options);
|
|
|
+ let mut html_output = String::new();
|
|
|
+ html::push_html(&mut html_output, parser);
|
|
|
+
|
|
|
+ HttpResponse::Ok().body(html_output)
|
|
|
+ }
|
|
|
+ Err(error) => {
|
|
|
+ log::error!("Article located at path {} was not found.", p.as_str());
|
|
|
+ HttpResponse::NotFound().finish()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
#[actix_web::main]
|