gardenserver/src/main.rs

357 lines
10 KiB
Rust
Raw Normal View History

2020-10-09 21:02:45 +02:00
use actix_files::NamedFile;
use actix_web::error::ErrorInternalServerError;
use actix_web::{error, get, http, middleware, web, App, Error, HttpResponse, HttpServer};
use anyhow::anyhow;
2020-10-09 21:02:45 +02:00
use chrono::{DateTime, Local};
use clap::{App as ClapApp, Arg};
2020-10-09 22:45:29 +02:00
use linkify::LinkFinder;
use log::{info, trace};
2020-10-09 21:02:45 +02:00
use percent_encoding::utf8_percent_encode;
use pulldown_cmark::{html, Options, Parser};
use regex::{Captures, Regex};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::SystemTime;
use std::{env, fs};
2020-10-09 21:02:45 +02:00
use tera::{Context, Tera};
#[derive(Clone)]
struct State {
garden_dir: PathBuf,
index_file: Option<String>,
title: Option<String>,
tera: Tera,
}
struct MutableState {
garden_cache: Mutex<GardenCache>,
}
#[derive(Clone, Debug)]
struct GardenCache {
pages: HashMap<PathBuf, ParsedPage>,
files: Vec<PathBuf>,
}
impl Default for GardenCache {
fn default() -> Self {
GardenCache {
pages: HashMap::new(),
files: vec![],
}
}
}
#[derive(Clone, Debug)]
struct ParsedPage {
timestamp: Option<SystemTime>,
title: String,
html: String,
links: Vec<String>,
}
2020-10-09 21:02:45 +02:00
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() -> anyhow::Result<()> {
let env = env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info");
env_logger::init_from_env(env);
let app = ClapApp::new("gardenserver")
.version(VERSION)
.author("Tomáš Mládek <t@mldk.cz>")
.arg(Arg::with_name("DIRECTORY").required(true).index(1))
.arg(
Arg::with_name("BIND")
.long("bind")
.default_value("127.0.0.1:8642")
.help("address and port to bind the Web interface on")
.required(true),
)
.arg(
Arg::with_name("INDEX_FILE")
.takes_value(true)
.short("i")
.long("index")
.help("File to be served at the root."),
)
.arg(
Arg::with_name("TITLE")
.takes_value(true)
.short("t")
.long("title")
.help("Title of this digital garden."),
);
let matches = app.get_matches();
let directory = Path::new(matches.value_of("DIRECTORY").unwrap());
info!(
"Starting GardenServer {} of {}...",
VERSION,
directory.display()
);
let tera = Tera::new("templates/**/*.html")?;
let sys = actix::System::new("gardenserver");
let bind: SocketAddr = matches
.value_of("BIND")
.unwrap()
.parse()
.expect("Incorrect bind format.");
info!("Starting server at: {}", &bind);
let mutable_state = web::Data::new(MutableState {
garden_cache: Mutex::new(update_garden(directory, GardenCache::default())?),
});
2020-10-09 21:02:45 +02:00
let state = State {
garden_dir: directory.to_path_buf(),
index_file: matches.value_of("INDEX_FILE").map(|s| s.to_string()),
title: matches.value_of("TITLE").map(|s| s.to_string()),
tera,
};
// Start HTTP server
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.data(state.clone())
.app_data(mutable_state.clone())
.service(actix_files::Files::new("/static", "templates"))
2020-10-09 21:02:45 +02:00
.service(render)
})
.bind(&bind)?
.run();
Ok(sys.run()?)
}
#[get("{path:.*}")]
async fn render(
request: web::HttpRequest,
data: web::Data<State>,
state: web::Data<MutableState>,
2020-10-09 21:02:45 +02:00
path: web::Path<String>,
) -> Result<HttpResponse, Error> {
let mut cache = state.garden_cache.lock().unwrap();
*cache = update_garden(&data.garden_dir, (*cache).clone())
.map_err(error::ErrorInternalServerError)?;
2020-10-09 21:02:45 +02:00
// Redirect to index if path is empty.
2020-10-09 21:02:45 +02:00
if path.is_empty() {
let location = match data.index_file.as_ref() {
2020-10-09 21:02:45 +02:00
Some(index_file) => index_file.clone(),
None => cache
.files
2020-10-20 19:13:51 +02:00
.iter()
.filter(|f| f.to_str().unwrap().ends_with(".md"))
.collect::<Vec<&PathBuf>>()
.first()
.unwrap_or(&cache.files.first().unwrap())
2020-10-20 19:13:51 +02:00
.display()
.to_string(),
2020-10-09 21:02:45 +02:00
};
return Ok(HttpResponse::Found()
.header(http::header::LOCATION, location.as_str())
.finish());
}
let full_path = data.garden_dir.join(path.as_str());
// Redirect to ".md" version if requested path matches a .md file without the extension
if !full_path.exists() && full_path.extension().is_none() {
let md_path = format!("{}.md", path.to_string());
if Path::new(&md_path).exists() {
2020-10-09 21:02:45 +02:00
return Ok(HttpResponse::Found()
.header(http::header::LOCATION, md_path)
2020-10-09 21:02:45 +02:00
.finish());
}
}
if full_path.exists() && !path.ends_with(".md") {
return Ok(NamedFile::open(full_path)?.into_response(&request)?);
}
let page = cache.pages.get(&full_path);
let mut context = Context::new();
context.insert("version", VERSION);
context.insert(
"garden_title",
data.title.as_ref().unwrap_or(&"Digital Garden".to_string()),
);
context.insert("files", &cache.files);
context.insert(
"page_title",
&match page {
Some(page) => page.title.clone(),
None => full_path
.components()
.last()
.unwrap()
.as_os_str()
.to_str()
.unwrap()
.to_string(),
},
);
context.insert(
"content",
&match page {
Some(page) => page.html.clone(),
None => data
.tera
.render("_not_found.html", &Context::new())
.map_err(ErrorInternalServerError)?,
},
);
context.insert(
"mtime",
&match page {
Some(page) => {
if let Some(timestamp) = page.timestamp {
let mtime: DateTime<Local> = timestamp.into();
Some(mtime.format("%c").to_string())
} else {
None
}
}
None => None,
},
);
Ok(HttpResponse::Ok().body(
data.tera
.render("main.html", &context)
.map_err(ErrorInternalServerError)?,
))
}
fn update_garden<P: AsRef<Path>>(
garden_path: P,
current: GardenCache,
) -> anyhow::Result<GardenCache> {
let garden_path = garden_path.as_ref().clone();
let mut files: Vec<PathBuf> = fs::read_dir(&garden_path)?
.filter_map(|entry| {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() {
let stripped_path = path.strip_prefix(&garden_path).unwrap().to_path_buf();
if !stripped_path.to_str().unwrap().starts_with(".") {
return Some(stripped_path);
}
}
}
None
})
.collect();
files.sort();
if files.is_empty() {
return Err(anyhow!("Garden is empty."));
}
let mut pages = current.pages.clone();
let markdown_paths = files
.iter()
.filter(|p| p.to_str().unwrap_or("").ends_with(".md"))
.map(|p| garden_path.join(p));
for path in markdown_paths {
trace!("Loading {} into cache...", path.display());
let mtime = path.metadata().unwrap().modified().ok();
if let Some(page) = pages.get(&path) {
match (mtime, page.timestamp) {
(Some(fs_time), Some(last_time)) => {
if fs_time == last_time {
continue;
}
}
_ => {}
}
}
let mut file = File::open(&path)?;
2020-10-09 21:02:45 +02:00
let mut file_string = String::new();
file.read_to_string(&mut file_string)?;
let markdown_source = preprocess_markdown(file_string);
2020-10-09 21:02:45 +02:00
let parser = Parser::new_ext(markdown_source.as_str(), Options::all());
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
// TODO!
let h1_regex = Regex::new(r"<h1>([^>]+)</h1>").unwrap();
let title = match h1_regex.captures(&html_output) {
2020-10-09 21:02:45 +02:00
Some(h1_match) => h1_match.get(1).unwrap().as_str(),
_ => &path
2020-10-09 21:02:45 +02:00
.components()
.last()
.unwrap()
.as_os_str()
.to_str()
.unwrap_or("???"),
};
pages.insert(
path.clone(),
ParsedPage {
timestamp: mtime,
html: html_output.clone(),
title: String::from(title),
links: vec![], // todo!,
},
2020-10-09 21:02:45 +02:00
);
}
let result = GardenCache { files, pages };
trace!("{:#?}", result);
Ok(result)
2020-10-09 21:02:45 +02:00
}
fn preprocess_markdown(string: String) -> String {
2020-10-09 21:02:45 +02:00
let double_brackets = Regex::new(r"\[\[(?P<inner>[\w .]+)\]\]").unwrap();
2020-10-09 22:45:29 +02:00
let finder = LinkFinder::new();
2020-10-09 21:02:45 +02:00
2020-10-09 22:45:29 +02:00
let result = double_brackets
2020-10-09 21:02:45 +02:00
.replace_all(string.as_str(), |caps: &Captures| {
format!(
"[{}]({})",
&caps[1],
utf8_percent_encode(&caps[1], percent_encoding::NON_ALPHANUMERIC)
)
})
2020-10-09 22:45:29 +02:00
.to_string();
let result_vec = Vec::from(result.as_str());
let start_delims = vec![b'(', b'<'];
let end_delims = vec![b')', b'>'];
2020-10-12 21:46:28 +02:00
// link.end() is the first char AFTER the link!
2020-10-09 22:45:29 +02:00
let links = finder.links(result.as_str()).filter(|link| {
2020-10-12 21:46:28 +02:00
link.start() == 0
|| link.end() == result.len()
|| !start_delims.contains(&result_vec[link.start() - 1])
|| !end_delims.contains(&result_vec[link.end()])
2020-10-09 22:45:29 +02:00
});
let mut offset = 0;
let mut result_string = result.to_string();
for link in links {
let orig = link.as_str();
let new = format!("<{}>", orig);
result_string.replace_range((link.start() + offset)..(link.end() + offset), new.as_str());
offset += new.len() - orig.len();
}
result_string
2020-10-09 21:02:45 +02:00
}