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
142
src/main.rs
142
src/main.rs
|
@ -1,28 +1,39 @@
|
|||
#![deny(clippy::pedantic)]
|
||||
#![allow(clippy::unused_async)]
|
||||
|
||||
mod errors;
|
||||
mod highlight;
|
||||
mod io;
|
||||
mod params;
|
||||
mod errors;
|
||||
|
||||
use highlight::highlight;
|
||||
use io::{generate_id, get_paste, store_paste, PasteStore};
|
||||
use params::{HostHeader, IsPlaintextRequest};
|
||||
use errors::{NotFound, InternalServerError};
|
||||
use crate::{
|
||||
errors::{InternalServerError, NotFound},
|
||||
highlight::highlight,
|
||||
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 actix_web::{web, http::header, web::Data, App, HttpResponse, HttpServer, Responder, Error, HttpRequest};
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use log::error;
|
||||
use actix_web::web::{PayloadConfig, FormConfig};
|
||||
use log::{error, info};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
};
|
||||
use syntect::html::{css_for_theme_with_class_style, ClassStyle};
|
||||
|
||||
#[derive(argh::FromArgs, Clone)]
|
||||
/// a pastebin.
|
||||
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(
|
||||
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,
|
||||
/// 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::post().to(submit))
|
||||
.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::head().to(|| HttpResponse::MethodNotAllowed()))
|
||||
.default_service(web::to(|req: HttpRequest| -> HttpResponse {
|
||||
.route("/{paste}", web::head().to(HttpResponse::MethodNotAllowed))
|
||||
.default_service(web::to(|req: HttpRequest| async move {
|
||||
error!("Couldn't find resource {}", req.uri());
|
||||
HttpResponse::from_error(NotFound.into())
|
||||
HttpResponse::from_error(NotFound)
|
||||
}))
|
||||
}
|
||||
});
|
||||
|
||||
server.bind(args.bind_addr)?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
info!("Listening on http://{}", args.bind_addr);
|
||||
|
||||
///
|
||||
/// Homepage
|
||||
///
|
||||
server.bind(args.bind_addr)?.run().await
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct Index;
|
||||
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
render_template(&req, Index)
|
||||
}
|
||||
|
||||
///
|
||||
/// Submit Paste
|
||||
///
|
||||
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
render_template(&req, &Index)
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct IndexForm {
|
||||
val: String,
|
||||
val: Bytes,
|
||||
}
|
||||
|
||||
async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>) -> impl Responder {
|
||||
let id = generate_id();
|
||||
let uri = format!("/{}", &id);
|
||||
store_paste(&store, id, input.into_inner().val).await;
|
||||
HttpResponse::Found().header(header::LOCATION, uri).finish()
|
||||
store_paste(&store, id, input.into_inner().val);
|
||||
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 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 {
|
||||
Some(host) => Ok(format!("https://{}{}", host, uri)),
|
||||
None => Ok(format!("{}", uri)),
|
||||
}
|
||||
Ok(uri)
|
||||
}
|
||||
|
||||
///
|
||||
/// Show paste page
|
||||
///
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "paste.html")]
|
||||
struct ShowPaste<'a> {
|
||||
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 key = splitter.next().unwrap();
|
||||
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 {
|
||||
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 {
|
||||
let data = std::str::from_utf8(entry.as_ref())?;
|
||||
|
||||
let code_highlighted = match ext {
|
||||
Some(extension) => match highlight(&entry, extension) {
|
||||
Some(extension) => match highlight(data, extension) {
|
||||
Some(html) => html,
|
||||
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!(
|
||||
"<code>{}</code>",
|
||||
code_highlighted.replace("\n", "</code><code>")
|
||||
code_highlighted.replace('\n', "</code><code>")
|
||||
);
|
||||
|
||||
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
|
||||
|
||||
render_template(&req, ShowPaste { content })
|
||||
render_template(&req, &ShowPaste { content })
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Helpers
|
||||
///
|
||||
async fn highlight_css() -> HttpResponse {
|
||||
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() {
|
||||
Ok(html) => Ok(HttpResponse::Ok().body(html)),
|
||||
Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)),
|
||||
Err(e) => {
|
||||
error!("Error while rendering template for {}: {}", req.uri(), e);
|
||||
Err(InternalServerError(Box::new(e)).into())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue