Move to actix-web

This commit is contained in:
jordan@doyle.la 2020-04-13 11:33:52 +01:00
parent 296cd50b7b
commit ccef0d9370
No known key found for this signature in database
GPG key ID: 1EA6BAE6F66DC49A
9 changed files with 1176 additions and 348 deletions

1173
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,17 +8,26 @@ authors = ["Jordan Doyle <jordan@doyle.la>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
argh = "0.1"
log = "0.4"
pretty_env_logger = "0.4"
owning_ref = "0.4" owning_ref = "0.4"
linked-hash-map = "0.5" linked-hash-map = "0.5"
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" }
askama = "0.9"
lazy_static = "1.4" lazy_static = "1.4"
serde = { version = "1.0", features = ["derive"] }
rand = { version = "0.7", features = ["nightly"] } rand = { version = "0.7", features = ["nightly"] }
gpw = "0.1" gpw = "0.1"
actix = "0.10.0-alpha.2"
actix-web = "3.0.0-alpha.1"
actix-rt = "1.0"
htmlescape = "0.3"
askama = "0.9"
syntect = "4.1" syntect = "4.1"
serde_derive = "1.0"
tokio = { version = "0.2", features = ["sync"] } tokio = { version = "0.2", features = ["sync"] }
async-trait = "0.1" futures = "0.3"
[profile.release] [profile.release]
lto = true lto = true

54
src/errors.rs Normal file
View file

@ -0,0 +1,54 @@
use actix_web::{web, http::header, body::Body, HttpResponse, ResponseError, http::StatusCode};
use std::fmt::{Write, Formatter};
macro_rules! impl_response_error_for_http_resp {
($tt:tt) => {
impl ResponseError for $tt {
fn error_response(&self) -> HttpResponse {
HtmlResponseError::error_response(self)
}
}
}
}
#[derive(Debug)]
pub struct NotFound;
impl std::fmt::Display for NotFound {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
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)]
pub struct InternalServerError(pub Box<dyn std::error::Error>);
impl std::fmt::Display for InternalServerError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", include_str!("../templates/500.html"))
}
}
impl HtmlResponseError for InternalServerError {}
impl_response_error_for_http_resp!(InternalServerError);
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(Body::from(buf))
}
}

View file

@ -1,10 +1,10 @@
extern crate syntect;
use syntect::easy::HighlightLines; use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet; use syntect::highlighting::ThemeSet;
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground}; use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
use syntect::parsing::SyntaxSet; use syntect::parsing::SyntaxSet;
use lazy_static::lazy_static;
/// 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.
/// ///

View file

@ -1,43 +1,31 @@
extern crate gpw; use rand::{thread_rng, Rng, distributions::Alphanumeric};
extern crate linked_hash_map;
extern crate owning_ref;
extern crate rand;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use lazy_static::lazy_static;
use linked_hash_map::LinkedHashMap; use linked_hash_map::LinkedHashMap;
use owning_ref::OwningRef; use owning_ref::OwningRef;
use tokio::sync::{RwLock, RwLockReadGuard};
use std::cell::RefCell; use std::cell::RefCell;
use std::env;
use std::sync::{PoisonError};
use tokio::sync::{RwLock, RwLockReadGuard};
type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>; type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>;
pub type PasteStore = RwLock<LinkedHashMap<String, String>>;
lazy_static! { lazy_static! {
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new()); static ref BUFFER_SIZE: usize = argh::from_env::<crate::BinArgs>().buffer_size;
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE")
.map(|f| f
.parse::<usize>()
.expect("Failed to parse value of BIN_BUFFER_SIZE"))
.unwrap_or(1000usize);
} }
/// 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() { async fn purge_old(entries: &PasteStore) {
let entries_len = ENTRIES.read().await.len(); let entries_len = entries.read().await.len();
if entries_len > *BUFFER_SIZE { if entries_len > *BUFFER_SIZE {
let to_remove = entries_len - *BUFFER_SIZE; let to_remove = entries_len - *BUFFER_SIZE;
let mut entries = ENTRIES.write().await; let mut entries = entries.write().await;
for _ in 0..to_remove { for _ in 0..to_remove {
entries.pop_front(); entries.pop_front();
@ -58,11 +46,10 @@ pub fn generate_id() -> String {
} }
/// Stores a paste under the given id /// Stores a paste under the given id
pub async fn store_paste(id: String, content: String) { pub async fn store_paste(entries: &PasteStore, id: String, content: String) {
purge_old().await; purge_old(&entries).await;
ENTRIES entries.write()
.write()
.await .await
.insert(id, content); .insert(id, content);
} }
@ -70,9 +57,9 @@ pub async fn store_paste(id: String, content: String) {
/// 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(id: &str) -> Option<RwLockReadGuardRef<'_, LinkedHashMap<String, String>, String>> { pub async fn get_paste<'a>(entries: &'a PasteStore, id: &str) -> Option<RwLockReadGuardRef<'a, LinkedHashMap<String, String>, String>> {
// 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)); let or = RwLockReadGuardRef::new(Box::new(entries.read().await));
if or.contains_key(id) { if or.contains_key(id) {
Some(or.map(|x| x.get(id).unwrap())) Some(or.map(|x| x.get(id).unwrap()))

View file

@ -1,33 +1,75 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate rocket;
extern crate askama;
mod highlight; mod highlight;
mod io; mod io;
mod params; mod params;
mod errors;
use highlight::highlight; use highlight::highlight;
use io::{generate_id, get_paste, store_paste}; use io::{generate_id, get_paste, store_paste, PasteStore};
use params::{HostHeader, IsPlaintextRequest}; use params::{HostHeader, IsPlaintextRequest};
use errors::{NotFound, InternalServerError};
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 rocket::http::{ContentType, RawStr, Status};
use rocket::request::Form;
use rocket::response::content::{Content, Html};
use rocket::response::Redirect;
use rocket::Data;
use std::borrow::Cow; use std::borrow::Cow;
use std::io::Read; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use log::error;
use actix_web::web::{PayloadConfig, FormConfig};
use tokio::io::AsyncReadExt; #[derive(argh::FromArgs, Clone)]
/// a pastebin.
pub struct BinArgs {
/// socket address to bind to (default: 127.0.0.1:8080)
#[argh(
positional,
default = "SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)"
)]
bind_addr: SocketAddr,
/// maximum amount of pastes to store before rotating (default: 1000)
#[argh(option, default = "1000")]
buffer_size: usize,
/// maximum paste size in bytes (default. 32kB)
#[argh(option, default = "32 * 1024")]
max_paste_size: usize,
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "INFO");
}
pretty_env_logger::init();
let args: BinArgs = argh::from_env();
let store = Data::new(PasteStore::default());
let server = HttpServer::new({
let args = args.clone();
move || {
App::new()
.app_data(store.clone())
.app_data(PayloadConfig::default().limit(args.max_paste_size))
.app_data(FormConfig::default().limit(args.max_paste_size))
.wrap(actix_web::middleware::Compress::default())
.route("/", web::get().to(index))
.route("/", web::post().to(submit))
.route("/", web::put().to(submit_raw))
.route("/", web::head().to(|| HttpResponse::MethodNotAllowed()))
.route("/{paste}", web::get().to(show_paste))
.route("/{paste}", web::head().to(|| HttpResponse::MethodNotAllowed()))
.default_service(web::to(|req: HttpRequest| -> HttpResponse {
error!("Couldn't find resource {}", req.uri());
HttpResponse::from_error(NotFound.into())
}))
}
});
server.bind(args.bind_addr)?
.run()
.await
}
/// ///
/// Homepage /// Homepage
@ -36,45 +78,33 @@ use tokio::io::AsyncReadExt;
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
struct Index; struct Index;
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
#[get("/")] render_template(&req, Index)
fn index() -> Result<Html<String>, Status> {
Index
.render()
.map(Html)
.map_err(|_| Status::InternalServerError)
} }
/// ///
/// Submit Paste /// Submit Paste
/// ///
#[derive(FromForm)] #[derive(serde::Deserialize)]
struct IndexForm { struct IndexForm {
val: String, val: String,
} }
#[post("/", data = "<input>")] async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>) -> impl Responder {
async fn submit(input: Form<IndexForm>) -> Redirect {
let id = generate_id(); let id = generate_id();
let uri = uri!(show_paste: &id); let uri = format!("/{}", &id);
store_paste(id, input.into_inner().val).await; store_paste(&store, id, input.into_inner().val).await;
Redirect::to(uri) HttpResponse::Found().header(header::LOCATION, uri).finish()
} }
#[put("/", data = "<input>")] async fn submit_raw(data: String, host: HostHeader, store: Data<PasteStore>) -> Result<String, Error> {
async fn submit_raw(input: Data, host: HostHeader<'_>) -> Result<String, Status> {
let mut data = String::new();
input.open().take(1024 * 1000)
.read_to_string(&mut data).await
.map_err(|_| Status::InternalServerError)?;
let id = generate_id(); let id = generate_id();
let uri = uri!(show_paste: &id); let uri = format!("/{}", &id);
store_paste(id, data).await; store_paste(&store, id, data).await;
match *host { match &*host {
Some(host) => Ok(format!("https://{}{}", host, uri)), Some(host) => Ok(format!("https://{}{}", host, uri)),
None => Ok(format!("{}", uri)), None => Ok(format!("{}", uri)),
} }
@ -90,23 +120,22 @@ struct ShowPaste<'a> {
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>, content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
} }
#[get("/<key>")] async fn show_paste(req: HttpRequest, key: actix_web::web::Path<String>, plaintext: IsPlaintextRequest, store: Data<PasteStore>) -> Result<HttpResponse, Error> {
async fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<String>, Status> {
let mut splitter = key.splitn(2, '.'); let mut splitter = key.splitn(2, '.');
let key = splitter.next().ok_or_else(|| Status::NotFound)?; let key = splitter.next().unwrap();
let ext = splitter.next(); let ext = splitter.next();
let entry = &*get_paste(key).await.ok_or_else(|| Status::NotFound)?; let entry = &*get_paste(&store, key).await.ok_or_else(|| NotFound)?;
if *plaintext { if *plaintext {
Ok(Content(ContentType::Plain, entry.to_string())) Ok(HttpResponse::Ok().content_type("text/plain; charset=utf-8").body(entry))
} else { } else {
let code_highlighted = match ext { let code_highlighted = match ext {
Some(extension) => match highlight(&entry, extension) { Some(extension) => match highlight(&entry, extension) {
Some(html) => html, Some(html) => html,
None => return Err(Status::NotFound), None => return Err(NotFound.into()),
}, },
None => String::from(RawStr::from_str(entry).html_escape()), None => htmlescape::encode_minimal(entry),
}; };
// Add <code> tags to enable line numbering with CSS // Add <code> tags to enable line numbering with CSS
@ -117,16 +146,20 @@ async fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Conten
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml); let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
let template = ShowPaste { content }; render_template(&req, ShowPaste { content })
match template.render() {
Ok(html) => Ok(Content(ContentType::HTML, html)),
Err(_) => Err(Status::InternalServerError),
}
} }
} }
fn main() { ///
rocket::ignite() /// Helpers
.mount("/", routes![index, submit, submit_raw, show_paste]) ///
.launch();
fn render_template<T: Template>(req: &HttpRequest, template: T) -> Result<HttpResponse, Error> {
match template.render() {
Ok(html) => Ok(HttpResponse::Ok().body(html)),
Err(e) => {
error!("Error while rendering template for {}: {}", req.uri(), e);
Err(InternalServerError(Box::new(e)).into())
}
}
} }

View file

@ -1,9 +1,10 @@
use std::ops::Deref; use std::ops::Deref;
use rocket::request::{FromRequest, Outcome}; use actix_web::{FromRequest, HttpRequest, HttpMessage};
use rocket::Request; use actix_web::dev::Payload;
use actix_web::http::header;
use async_trait::async_trait; 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.
/// ///
@ -19,26 +20,25 @@ impl Deref for IsPlaintextRequest {
} }
} }
#[async_trait] impl FromRequest for IsPlaintextRequest {
impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest { type Error = actix_web::Error;
type Error = (); type Future = futures::future::Ready<Result<Self, Self::Error>>;
type Config = ();
async fn from_request(request: &'a Request<'r>) -> Outcome<IsPlaintextRequest, ()> { fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
if let Some(format) = request.format() { if req.content_type() == "text/plain" {
if format.is_plain() { return ok(IsPlaintextRequest(true));
return Outcome::Success(IsPlaintextRequest(true));
}
} }
match request match req
.headers() .headers()
.get_one("User-Agent") .get(header::USER_AGENT)
.and_then(|u| u.splitn(2, '/').next()) .and_then(|u| u.to_str().unwrap().splitn(2, '/').next())
{ {
None | Some("Wget") | Some("curl") | Some("HTTPie") => { None | Some("Wget") | Some("curl") | Some("HTTPie") => {
Outcome::Success(IsPlaintextRequest(true)) ok(IsPlaintextRequest(true))
} }
_ => Outcome::Success(IsPlaintextRequest(false)), _ => ok(IsPlaintextRequest(false)),
} }
} }
} }
@ -47,21 +47,25 @@ impl<'a, 'r> FromRequest<'a, 'r> 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<'a>(pub Option<&'a str>); pub struct HostHeader(pub Option<String>);
impl<'a> Deref for HostHeader<'a> { impl Deref for HostHeader {
type Target = Option<&'a str>; type Target = Option<String>;
fn deref(&self) -> &Option<&'a str> { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
#[async_trait] impl FromRequest for HostHeader {
impl<'a, 'r> FromRequest<'a, 'r> for HostHeader<'a> { type Error = actix_web::Error;
type Error = (); type Future = futures::future::Ready<Result<Self, Self::Error>>;
type Config = ();
async fn from_request(request: &'a Request<'r>) -> Outcome<HostHeader<'a>, ()> { fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
Outcome::Success(HostHeader(request.headers().get_one("Host"))) match req.headers().get(header::HOST) {
None => ok(Self(None)),
Some(h) => ok(Self(h.to_str().ok().map(|f| f.to_string())))
}
} }
} }

15
templates/404.html Normal file
View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>404 Not Found</title>
</head>
<body align="center">
<div align="center">
<h1>404: Not Found</h1>
<p>The requested resource could not be found.</p>
<hr />
<small>bin.</small>
</div>
</body>
</html>

15
templates/500.html Normal file
View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>500 Internal Server Error</title>
</head>
<body align="center">
<div align="center">
<h1>500: Internal Server Error</h1>
<p>An error occurred while fetching the requested resource.</p>
<hr />
<small>bin.</small>
</div>
</body>
</html>