Add bat for its extended syntax sets
This commit is contained in:
parent
e8c3d1a5fc
commit
e1639c032a
9 changed files with 1055 additions and 1153 deletions
1845
Cargo.lock
generated
1845
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
15
Cargo.toml
15
Cargo.toml
|
@ -11,21 +11,20 @@ edition = "2018"
|
||||||
argh = "0.1"
|
argh = "0.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
owning_ref = "0.4"
|
|
||||||
linked-hash-map = "0.5"
|
linked-hash-map = "0.5"
|
||||||
lazy_static = "1.4"
|
once_cell = "1.10"
|
||||||
|
parking_lot = "0.12"
|
||||||
|
bytes = { version = "1.1", features = ["serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
rand = { version = "0.8" }
|
rand = { version = "0.8" }
|
||||||
gpw = "0.1"
|
gpw = "0.1"
|
||||||
|
actix = "0.13"
|
||||||
actix = "0.12"
|
actix-web = "4.0"
|
||||||
actix-web = "3.3"
|
|
||||||
|
|
||||||
htmlescape = "0.3"
|
htmlescape = "0.3"
|
||||||
askama = "0.11"
|
askama = "0.11"
|
||||||
|
bat = "0.20"
|
||||||
syntect = "4.6"
|
syntect = "4.6"
|
||||||
|
tokio = { version = "1.17", features = ["sync"] }
|
||||||
tokio = { version = "1.15", features = ["sync"] }
|
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -1,40 +1,42 @@
|
||||||
use actix_web::{web, http::header, body::Body, HttpResponse, ResponseError, http::StatusCode};
|
use actix_web::{body::BoxBody, http::header, http::StatusCode, web, HttpResponse, ResponseError};
|
||||||
|
|
||||||
use std::fmt::{Write, Formatter};
|
use std::fmt::{Formatter, Write};
|
||||||
|
|
||||||
macro_rules! impl_response_error_for_http_resp {
|
macro_rules! impl_response_error_for_http_resp {
|
||||||
($tt:tt) => {
|
($ty:ty, $path:expr, $status:expr) => {
|
||||||
impl ResponseError for $tt {
|
impl ResponseError for $ty {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
HtmlResponseError::error_response(self)
|
HtmlResponseError::error_response(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for $ty {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", include_str!($path))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HtmlResponseError for $ty {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
$status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NotFound;
|
pub struct NotFound;
|
||||||
impl std::fmt::Display for NotFound {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
impl_response_error_for_http_resp!(NotFound, "../templates/404.html", StatusCode::NOT_FOUND);
|
||||||
write!(f, "{}", include_str!("../templates/404.html"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl HtmlResponseError for NotFound {
|
|
||||||
fn status_code(&self) -> StatusCode {
|
|
||||||
StatusCode::NOT_FOUND
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl_response_error_for_http_resp!(NotFound);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InternalServerError(pub Box<dyn std::error::Error>);
|
pub struct InternalServerError(pub Box<dyn std::error::Error>);
|
||||||
impl std::fmt::Display for InternalServerError {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
impl_response_error_for_http_resp!(
|
||||||
write!(f, "{}", include_str!("../templates/500.html"))
|
InternalServerError,
|
||||||
}
|
"../templates/500.html",
|
||||||
}
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
impl HtmlResponseError for InternalServerError {}
|
);
|
||||||
impl_response_error_for_http_resp!(InternalServerError);
|
|
||||||
|
|
||||||
pub trait HtmlResponseError: ResponseError {
|
pub trait HtmlResponseError: ResponseError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
|
@ -49,6 +51,6 @@ pub trait HtmlResponseError: ResponseError {
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("text/html; charset=utf-8"),
|
header::HeaderValue::from_static("text/html; charset=utf-8"),
|
||||||
);
|
);
|
||||||
resp.set_body(Body::from(buf))
|
resp.set_body(BoxBody::new(buf))
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,26 +1,45 @@
|
||||||
use syntect::easy::HighlightLines;
|
use bat::assets::HighlightingAssets;
|
||||||
use syntect::highlighting::ThemeSet;
|
use once_cell::sync::Lazy;
|
||||||
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
|
use syntect::{
|
||||||
use syntect::parsing::SyntaxSet;
|
html::{ClassStyle, ClassedHTMLGenerator},
|
||||||
|
parsing::SyntaxSet,
|
||||||
|
};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
thread_local!(pub static BAT_ASSETS: HighlightingAssets = HighlightingAssets::from_binary());
|
||||||
|
|
||||||
/// Takes the content of a paste and the extension passed in by the viewer and will return the content
|
/// Takes the content of a paste and the extension passed in by the viewer and will return the content
|
||||||
/// highlighted in the appropriate format in HTML.
|
/// highlighted in the appropriate format in HTML.
|
||||||
///
|
///
|
||||||
/// Returns `None` if the extension isn't supported.
|
/// Returns `None` if the extension isn't supported.
|
||||||
pub fn highlight(content: &str, ext: &str) -> Option<String> {
|
pub fn highlight(content: &str, ext: &str) -> Option<String> {
|
||||||
lazy_static! {
|
static SS: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines);
|
||||||
static ref SS: SyntaxSet = SyntaxSet::load_defaults_newlines();
|
|
||||||
static ref TS: ThemeSet = ThemeSet::load_defaults();
|
BAT_ASSETS.with(|f| {
|
||||||
|
let ss = f.get_syntax_set().ok().unwrap_or(&SS);
|
||||||
|
let syntax = ss.find_syntax_by_extension(ext)?;
|
||||||
|
let mut html_generator =
|
||||||
|
ClassedHTMLGenerator::new_with_class_style(syntax, ss, ClassStyle::Spaced);
|
||||||
|
for line in LinesWithEndings(content.trim()) {
|
||||||
|
html_generator.parse_html_for_line_which_includes_newline(line);
|
||||||
|
}
|
||||||
|
Some(html_generator.finalize())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LinesWithEndings<'a>(&'a str);
|
||||||
|
|
||||||
|
impl<'a> Iterator for LinesWithEndings<'a> {
|
||||||
|
type Item = &'a str;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.0.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let split = self.0.find('\n').map_or(self.0.len(), |i| i + 1);
|
||||||
|
let (line, rest) = self.0.split_at(split);
|
||||||
|
self.0 = rest;
|
||||||
|
Some(line)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let syntax = SS.find_syntax_by_extension(ext)?;
|
|
||||||
let mut h = HighlightLines::new(syntax, &TS.themes["base16-ocean.dark"]);
|
|
||||||
let regions = h.highlight(content, &SS);
|
|
||||||
|
|
||||||
Some(styled_line_to_highlighted_html(
|
|
||||||
®ions[..],
|
|
||||||
IncludeBackground::No,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
48
src/io.rs
48
src/io.rs
|
@ -1,31 +1,21 @@
|
||||||
use rand::{thread_rng, Rng, distributions::Alphanumeric};
|
use actix_web::web::Bytes;
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use linked_hash_map::LinkedHashMap;
|
use linked_hash_map::LinkedHashMap;
|
||||||
use owning_ref::OwningRef;
|
use once_cell::sync::Lazy;
|
||||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
use parking_lot::RwLock;
|
||||||
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>;
|
pub type PasteStore = RwLock<LinkedHashMap<String, Bytes>>;
|
||||||
|
|
||||||
pub type PasteStore = RwLock<LinkedHashMap<String, String>>;
|
static BUFFER_SIZE: Lazy<usize> = Lazy::new(|| argh::from_env::<crate::BinArgs>().buffer_size);
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref BUFFER_SIZE: usize = argh::from_env::<crate::BinArgs>().buffer_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ensures `ENTRIES` is less than the size of `BIN_BUFFER_SIZE`. If it isn't then
|
/// Ensures `ENTRIES` is less than the size of `BIN_BUFFER_SIZE`. If it isn't then
|
||||||
/// `ENTRIES.len() - BIN_BUFFER_SIZE` elements will be popped off the front of the map.
|
/// `ENTRIES.len() - BIN_BUFFER_SIZE` elements will be popped off the front of the map.
|
||||||
///
|
///
|
||||||
/// During the purge, `ENTRIES` is locked and the current thread will block.
|
/// During the purge, `ENTRIES` is locked and the current thread will block.
|
||||||
async fn purge_old(entries: &PasteStore) {
|
fn purge_old(entries: &mut LinkedHashMap<String, Bytes>) {
|
||||||
let entries_len = entries.read().await.len();
|
if entries.len() > *BUFFER_SIZE {
|
||||||
|
let to_remove = entries.len() - *BUFFER_SIZE;
|
||||||
if entries_len > *BUFFER_SIZE {
|
|
||||||
let to_remove = entries_len - *BUFFER_SIZE;
|
|
||||||
|
|
||||||
let mut entries = entries.write().await;
|
|
||||||
|
|
||||||
for _ in 0..to_remove {
|
for _ in 0..to_remove {
|
||||||
entries.pop_front();
|
entries.pop_front();
|
||||||
|
@ -47,24 +37,18 @@ pub fn generate_id() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores a paste under the given id
|
/// Stores a paste under the given id
|
||||||
pub async fn store_paste(entries: &PasteStore, id: String, content: String) {
|
pub fn store_paste(entries: &PasteStore, id: String, content: Bytes) {
|
||||||
purge_old(&entries).await;
|
let mut entries = entries.write();
|
||||||
|
|
||||||
entries.write()
|
purge_old(&mut entries);
|
||||||
.await
|
|
||||||
.insert(id, content);
|
entries.insert(id, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a paste by id.
|
/// Get a paste by id.
|
||||||
///
|
///
|
||||||
/// Returns `None` if the paste doesn't exist.
|
/// Returns `None` if the paste doesn't exist.
|
||||||
pub async fn get_paste<'a>(entries: &'a PasteStore, id: &str) -> Option<RwLockReadGuardRef<'a, LinkedHashMap<String, String>, String>> {
|
pub fn get_paste(entries: &PasteStore, id: &str) -> Option<Bytes> {
|
||||||
// need to box the guard until owning_ref understands Pin is a stable address
|
// need to box the guard until owning_ref understands Pin is a stable address
|
||||||
let or = RwLockReadGuardRef::new(Box::new(entries.read().await));
|
entries.read().get(id).map(Bytes::clone)
|
||||||
|
|
||||||
if or.contains_key(id) {
|
|
||||||
Some(or.map(|x| x.get(id).unwrap()))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
140
src/main.rs
140
src/main.rs
|
@ -1,28 +1,39 @@
|
||||||
|
#![deny(clippy::pedantic)]
|
||||||
|
#![allow(clippy::unused_async)]
|
||||||
|
|
||||||
|
mod errors;
|
||||||
mod highlight;
|
mod highlight;
|
||||||
mod io;
|
mod io;
|
||||||
mod params;
|
mod params;
|
||||||
mod errors;
|
|
||||||
|
|
||||||
use highlight::highlight;
|
use crate::{
|
||||||
use io::{generate_id, get_paste, store_paste, PasteStore};
|
errors::{InternalServerError, NotFound},
|
||||||
use params::{HostHeader, IsPlaintextRequest};
|
highlight::highlight,
|
||||||
use errors::{NotFound, InternalServerError};
|
io::{generate_id, get_paste, store_paste, PasteStore},
|
||||||
|
params::{HostHeader, IsPlaintextRequest},
|
||||||
|
};
|
||||||
|
|
||||||
|
use actix_web::{
|
||||||
|
http::header,
|
||||||
|
web::{self, Bytes, Data, FormConfig, PayloadConfig},
|
||||||
|
App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||||
|
};
|
||||||
use askama::{Html as AskamaHtml, MarkupDisplay, Template};
|
use askama::{Html as AskamaHtml, MarkupDisplay, Template};
|
||||||
use actix_web::{web, http::header, web::Data, App, HttpResponse, HttpServer, Responder, Error, HttpRequest};
|
use log::{error, info};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use std::borrow::Cow;
|
use std::{
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
borrow::Cow,
|
||||||
use log::error;
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
use actix_web::web::{PayloadConfig, FormConfig};
|
};
|
||||||
|
use syntect::html::{css_for_theme_with_class_style, ClassStyle};
|
||||||
|
|
||||||
#[derive(argh::FromArgs, Clone)]
|
#[derive(argh::FromArgs, Clone)]
|
||||||
/// a pastebin.
|
/// a pastebin.
|
||||||
pub struct BinArgs {
|
pub struct BinArgs {
|
||||||
/// socket address to bind to (default: 127.0.0.1:8080)
|
/// socket address to bind to (default: 127.0.0.1:8820)
|
||||||
#[argh(
|
#[argh(
|
||||||
positional,
|
positional,
|
||||||
default = "SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)"
|
default = "SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8820)"
|
||||||
)]
|
)]
|
||||||
bind_addr: SocketAddr,
|
bind_addr: SocketAddr,
|
||||||
/// maximum amount of pastes to store before rotating (default: 1000)
|
/// maximum amount of pastes to store before rotating (default: 1000)
|
||||||
|
@ -56,107 +67,124 @@ async fn main() -> std::io::Result<()> {
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
.route("/", web::post().to(submit))
|
.route("/", web::post().to(submit))
|
||||||
.route("/", web::put().to(submit_raw))
|
.route("/", web::put().to(submit_raw))
|
||||||
.route("/", web::head().to(|| HttpResponse::MethodNotAllowed()))
|
.route("/", web::head().to(HttpResponse::MethodNotAllowed))
|
||||||
|
.route("/highlight.css", web::get().to(highlight_css))
|
||||||
.route("/{paste}", web::get().to(show_paste))
|
.route("/{paste}", web::get().to(show_paste))
|
||||||
.route("/{paste}", web::head().to(|| HttpResponse::MethodNotAllowed()))
|
.route("/{paste}", web::head().to(HttpResponse::MethodNotAllowed))
|
||||||
.default_service(web::to(|req: HttpRequest| -> HttpResponse {
|
.default_service(web::to(|req: HttpRequest| async move {
|
||||||
error!("Couldn't find resource {}", req.uri());
|
error!("Couldn't find resource {}", req.uri());
|
||||||
HttpResponse::from_error(NotFound.into())
|
HttpResponse::from_error(NotFound)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
server.bind(args.bind_addr)?
|
info!("Listening on http://{}", args.bind_addr);
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
server.bind(args.bind_addr)?.run().await
|
||||||
/// Homepage
|
}
|
||||||
///
|
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct Index;
|
struct Index;
|
||||||
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
|
||||||
render_template(&req, Index)
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
/// Submit Paste
|
render_template(&req, &Index)
|
||||||
///
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct IndexForm {
|
struct IndexForm {
|
||||||
val: String,
|
val: Bytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>) -> impl Responder {
|
async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>) -> impl Responder {
|
||||||
let id = generate_id();
|
let id = generate_id();
|
||||||
let uri = format!("/{}", &id);
|
let uri = format!("/{}", &id);
|
||||||
store_paste(&store, id, input.into_inner().val).await;
|
store_paste(&store, id, input.into_inner().val);
|
||||||
HttpResponse::Found().header(header::LOCATION, uri).finish()
|
HttpResponse::Found()
|
||||||
|
.append_header((header::LOCATION, uri))
|
||||||
|
.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn submit_raw(data: String, host: HostHeader, store: Data<PasteStore>) -> Result<String, Error> {
|
async fn submit_raw(
|
||||||
|
data: Bytes,
|
||||||
|
host: HostHeader,
|
||||||
|
store: Data<PasteStore>,
|
||||||
|
) -> Result<String, Error> {
|
||||||
let id = generate_id();
|
let id = generate_id();
|
||||||
let uri = format!("/{}", &id);
|
let uri = if let Some(Ok(host)) = host.0.as_ref().map(|v| std::str::from_utf8(v.as_bytes())) {
|
||||||
|
format!("https://{}/{}", host, id)
|
||||||
|
} else {
|
||||||
|
format!("/{}", id)
|
||||||
|
};
|
||||||
|
|
||||||
store_paste(&store, id, data).await;
|
store_paste(&store, id, data);
|
||||||
|
|
||||||
match &*host {
|
Ok(uri)
|
||||||
Some(host) => Ok(format!("https://{}{}", host, uri)),
|
|
||||||
None => Ok(format!("{}", uri)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
|
||||||
/// Show paste page
|
|
||||||
///
|
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "paste.html")]
|
#[template(path = "paste.html")]
|
||||||
struct ShowPaste<'a> {
|
struct ShowPaste<'a> {
|
||||||
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
|
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn show_paste(req: HttpRequest, key: actix_web::web::Path<String>, plaintext: IsPlaintextRequest, store: Data<PasteStore>) -> Result<HttpResponse, Error> {
|
async fn show_paste(
|
||||||
|
req: HttpRequest,
|
||||||
|
key: actix_web::web::Path<String>,
|
||||||
|
plaintext: IsPlaintextRequest,
|
||||||
|
store: Data<PasteStore>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
let mut splitter = key.splitn(2, '.');
|
let mut splitter = key.splitn(2, '.');
|
||||||
let key = splitter.next().unwrap();
|
let key = splitter.next().unwrap();
|
||||||
let ext = splitter.next();
|
let ext = splitter.next();
|
||||||
|
|
||||||
let entry = &*get_paste(&store, key).await.ok_or_else(|| NotFound)?;
|
let entry = get_paste(&store, key).ok_or(NotFound)?;
|
||||||
|
|
||||||
if *plaintext {
|
if *plaintext {
|
||||||
Ok(HttpResponse::Ok().content_type("text/plain; charset=utf-8").body(entry))
|
Ok(HttpResponse::Ok()
|
||||||
|
.content_type("text/plain; charset=utf-8")
|
||||||
|
.body(entry))
|
||||||
} else {
|
} else {
|
||||||
|
let data = std::str::from_utf8(entry.as_ref())?;
|
||||||
|
|
||||||
let code_highlighted = match ext {
|
let code_highlighted = match ext {
|
||||||
Some(extension) => match highlight(&entry, extension) {
|
Some(extension) => match highlight(data, extension) {
|
||||||
Some(html) => html,
|
Some(html) => html,
|
||||||
None => return Err(NotFound.into()),
|
None => return Err(NotFound.into()),
|
||||||
},
|
},
|
||||||
None => htmlescape::encode_minimal(entry),
|
None => htmlescape::encode_minimal(data),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add <code> tags to enable line numbering with CSS
|
// Add <code> tags to enable line numbering with CSS
|
||||||
let html = format!(
|
let html = format!(
|
||||||
"<code>{}</code>",
|
"<code>{}</code>",
|
||||||
code_highlighted.replace("\n", "</code><code>")
|
code_highlighted.replace('\n', "</code><code>")
|
||||||
);
|
);
|
||||||
|
|
||||||
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
|
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
|
||||||
|
|
||||||
render_template(&req, ShowPaste { content })
|
render_template(&req, &ShowPaste { content })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
async fn highlight_css() -> HttpResponse {
|
||||||
/// Helpers
|
static CSS: Lazy<Bytes> = Lazy::new(|| {
|
||||||
///
|
highlight::BAT_ASSETS.with(|s| {
|
||||||
|
Bytes::from(css_for_theme_with_class_style(
|
||||||
|
s.get_theme("OneHalfDark"),
|
||||||
|
ClassStyle::Spaced,
|
||||||
|
))
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
fn render_template<T: Template>(req: &HttpRequest, template: T) -> Result<HttpResponse, Error> {
|
HttpResponse::Ok()
|
||||||
|
.content_type("text/css")
|
||||||
|
.body(CSS.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_template<T: Template>(req: &HttpRequest, template: &T) -> Result<HttpResponse, Error> {
|
||||||
match template.render() {
|
match template.render() {
|
||||||
Ok(html) => Ok(HttpResponse::Ok().body(html)),
|
Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Error while rendering template for {}: {}", req.uri(), e);
|
error!("Error while rendering template for {}: {}", req.uri(), e);
|
||||||
Err(InternalServerError(Box::new(e)).into())
|
Err(InternalServerError(Box::new(e)).into())
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use actix_web::{FromRequest, HttpRequest, HttpMessage};
|
use actix_web::{
|
||||||
use actix_web::dev::Payload;
|
dev::Payload,
|
||||||
use actix_web::http::header;
|
http::header::{self, HeaderValue},
|
||||||
|
FromRequest, HttpMessage, HttpRequest,
|
||||||
|
};
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
|
|
||||||
/// Holds a value that determines whether or not this request wanted a plaintext response.
|
/// Holds a value that determines whether or not this request wanted a plaintext response.
|
||||||
|
@ -23,7 +24,6 @@ impl Deref for IsPlaintextRequest {
|
||||||
impl FromRequest for IsPlaintextRequest {
|
impl FromRequest for IsPlaintextRequest {
|
||||||
type Error = actix_web::Error;
|
type Error = actix_web::Error;
|
||||||
type Future = futures::future::Ready<Result<Self, Self::Error>>;
|
type Future = futures::future::Ready<Result<Self, Self::Error>>;
|
||||||
type Config = ();
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||||||
if req.content_type() == "text/plain" {
|
if req.content_type() == "text/plain" {
|
||||||
|
@ -33,11 +33,9 @@ impl FromRequest for IsPlaintextRequest {
|
||||||
match req
|
match req
|
||||||
.headers()
|
.headers()
|
||||||
.get(header::USER_AGENT)
|
.get(header::USER_AGENT)
|
||||||
.and_then(|u| u.to_str().unwrap().splitn(2, '/').next())
|
.and_then(|u| u.to_str().unwrap().split('/').next())
|
||||||
{
|
{
|
||||||
None | Some("Wget") | Some("curl") | Some("HTTPie") => {
|
None | Some("Wget" | "curl" | "HTTPie") => ok(IsPlaintextRequest(true)),
|
||||||
ok(IsPlaintextRequest(true))
|
|
||||||
}
|
|
||||||
_ => ok(IsPlaintextRequest(false)),
|
_ => ok(IsPlaintextRequest(false)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,25 +45,13 @@ impl FromRequest for IsPlaintextRequest {
|
||||||
///
|
///
|
||||||
/// The inner value of this `HostHeader` will be `None` if there was no Host header
|
/// The inner value of this `HostHeader` will be `None` if there was no Host header
|
||||||
/// on the request.
|
/// on the request.
|
||||||
pub struct HostHeader(pub Option<String>);
|
pub struct HostHeader(pub Option<HeaderValue>);
|
||||||
|
|
||||||
impl Deref for HostHeader {
|
|
||||||
type Target = Option<String>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromRequest for HostHeader {
|
impl FromRequest for HostHeader {
|
||||||
type Error = actix_web::Error;
|
type Error = actix_web::Error;
|
||||||
type Future = futures::future::Ready<Result<Self, Self::Error>>;
|
type Future = futures::future::Ready<Result<Self, Self::Error>>;
|
||||||
type Config = ();
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||||||
match req.headers().get(header::HOST) {
|
ok(Self(req.headers().get(header::HOST).cloned()))
|
||||||
None => ok(Self(None)),
|
|
||||||
Some(h) => ok(Self(h.to_str().ok().map(|f| f.to_string())))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>bin.</title>
|
<title>bin.</title>
|
||||||
|
|
||||||
<link rel="help" href="https://github.com/w4/bin">
|
<link rel="help" href="https://github.com/w4/bin">
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
* { box-sizing: border-box; }
|
* { box-sizing: border-box; }
|
||||||
|
|
||||||
html, body { margin: 0; }
|
html, body { margin: 0; }
|
||||||
|
|
||||||
body {
|
body {
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
padding: 2rem;
|
padding: 2rem;
|
||||||
|
@ -18,13 +14,11 @@
|
||||||
color: #B0BEC5;
|
color: #B0BEC5;
|
||||||
font-family: 'Courier New', Courier, monospace;
|
font-family: 'Courier New', Courier, monospace;
|
||||||
line-height: 1.1;
|
line-height: 1.1;
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
{% block styles %}{% endblock styles %}
|
||||||
{% block styles %}
|
|
||||||
{% endblock styles %}
|
|
||||||
</style>
|
</style>
|
||||||
|
{% block head %}{% endblock head %}
|
||||||
</head>
|
</head>
|
||||||
<body>{% block content %}{% endblock content %}</body>
|
<body>{% block content %}{% endblock content %}</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
code {
|
code {
|
||||||
counter-increment: line;
|
counter-increment: line;
|
||||||
}
|
}
|
||||||
|
|
||||||
code::before {
|
code::before {
|
||||||
content: counter(line);
|
content: counter(line);
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
|
@ -24,8 +23,10 @@
|
||||||
color: #888;
|
color: #888;
|
||||||
-webkit-user-select: none;
|
-webkit-user-select: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
{% endblock styles %}
|
{% endblock styles %}
|
||||||
|
|
||||||
|
{% block head %}
|
||||||
|
<link rel="stylesheet" type="text/css" href="highlight.css" />
|
||||||
|
{% endblock head %}
|
||||||
|
|
||||||
{% block content %}<pre>{{ content|safe }}</pre>{% endblock content %}
|
{% block content %}<pre>{{ content|safe }}</pre>{% endblock content %}
|
Loading…
Add table
Add a link
Reference in a new issue