-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,788 additions
and
1,179 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
[package] | ||
name = "bin" | ||
version = "1.0.5" | ||
version = "2.0.0" | ||
description = "a paste bin." | ||
repository = "https://github.com/w4/bin" | ||
license = "WTFPL OR 0BSD" | ||
authors = ["Jordan Doyle <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
owning_ref = "0.4" | ||
argh = "0.1" | ||
log = "0.4" | ||
pretty_env_logger = "0.4" | ||
linked-hash-map = "0.5" | ||
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" } | ||
askama = "0.9" | ||
lazy_static = "1.4" | ||
rand = { version = "0.7", features = ["nightly"] } | ||
once_cell = "1.10" | ||
parking_lot = "0.12" | ||
bytes = { version = "1.1", features = ["serde"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
rand = { version = "0.8" } | ||
gpw = "0.1" | ||
syntect = "4.1" | ||
serde_derive = "1.0" | ||
tokio = { version = "0.2", features = ["sync", "macros"] } | ||
async-trait = "0.1" | ||
actix = "0.13" | ||
actix-web = "4.0" | ||
htmlescape = "0.3" | ||
askama = "0.11" | ||
bat = "0.20" | ||
syntect = "4.6" | ||
tokio = { version = "1.17", features = ["sync"] } | ||
futures = "0.3" | ||
|
||
[profile.release] | ||
lto = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
FROM rust:1.34.2-slim-stretch AS builder | ||
RUN rustup install nightly-x86_64-unknown-linux-gnu | ||
FROM rust:1-slim AS builder | ||
|
||
RUN apt update && apt install -y libclang-dev | ||
|
||
COPY . /sources | ||
WORKDIR /sources | ||
RUN cargo +nightly build --release | ||
RUN cargo build --release | ||
RUN chown nobody:nogroup /sources/target/release/bin | ||
|
||
|
||
FROM debian:stretch-slim | ||
FROM debian:bullseye-slim | ||
COPY --from=builder /sources/target/release/bin /pastebin | ||
|
||
USER nobody | ||
EXPOSE 8000 | ||
ENTRYPOINT ["/pastebin"] | ||
ENTRYPOINT ["/pastebin", "0.0.0.0:8000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use actix_web::{body::BoxBody, http::header, http::StatusCode, web, HttpResponse, ResponseError}; | ||
|
||
use std::fmt::{Formatter, Write}; | ||
|
||
macro_rules! impl_response_error_for_http_resp { | ||
($ty:ty, $path:expr, $status:expr) => { | ||
impl ResponseError for $ty { | ||
fn error_response(&self) -> HttpResponse { | ||
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)] | ||
pub struct NotFound; | ||
|
||
impl_response_error_for_http_resp!(NotFound, "../templates/404.html", StatusCode::NOT_FOUND); | ||
|
||
#[derive(Debug)] | ||
pub struct InternalServerError(pub Box<dyn std::error::Error>); | ||
|
||
impl_response_error_for_http_resp!( | ||
InternalServerError, | ||
"../templates/500.html", | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
); | ||
|
||
pub trait HtmlResponseError: ResponseError { | ||
fn status_code(&self) -> StatusCode { | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
} | ||
|
||
fn error_response(&self) -> HttpResponse { | ||
let mut resp = HttpResponse::new(HtmlResponseError::status_code(self)); | ||
let mut buf = web::BytesMut::new(); | ||
let _ = write!(&mut buf, "{}", self); | ||
resp.headers_mut().insert( | ||
header::CONTENT_TYPE, | ||
header::HeaderValue::from_static("text/html; charset=utf-8"), | ||
); | ||
resp.set_body(BoxBody::new(buf)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
extern crate syntect; | ||
use bat::assets::HighlightingAssets; | ||
use once_cell::sync::Lazy; | ||
use syntect::{ | ||
html::{ClassStyle, ClassedHTMLGenerator}, | ||
parsing::SyntaxSet, | ||
}; | ||
|
||
use syntect::easy::HighlightLines; | ||
use syntect::highlighting::ThemeSet; | ||
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground}; | ||
use syntect::parsing::SyntaxSet; | ||
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 | ||
/// highlighted in the appropriate format in HTML. | ||
/// | ||
/// Returns `None` if the extension isn't supported. | ||
pub fn highlight(content: &str, ext: &str) -> Option<String> { | ||
lazy_static! { | ||
static ref SS: SyntaxSet = SyntaxSet::load_defaults_newlines(); | ||
static ref TS: ThemeSet = ThemeSet::load_defaults(); | ||
} | ||
static SS: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines); | ||
|
||
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()) | ||
}) | ||
} | ||
|
||
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); | ||
pub struct LinesWithEndings<'a>(&'a str); | ||
|
||
Some(styled_line_to_highlighted_html( | ||
®ions[..], | ||
IncludeBackground::No, | ||
)) | ||
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.