forked from MIrrors/bin
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4eb9049a1a | ||
|
|
54f531a0a8 | ||
|
|
598baa2ace | ||
|
|
5b5c7dc2fb | ||
|
|
ebe0df5cc6 | ||
|
|
439f4bf2b4 | ||
|
|
377d4a129d | ||
|
|
9c53294b88 | ||
|
|
b095d2464c | ||
|
|
3c76d4fd1a | ||
|
|
06b90b8271 | ||
|
|
21b04b88ae | ||
|
|
53d97709fa | ||
|
|
ff3d193734 | ||
|
|
2b53d0a34c | ||
|
|
2502cc7f5c | ||
|
|
4bd924b423 | ||
|
|
be88df22f7 | ||
|
|
275ace8356 | ||
|
|
41900eaeee | ||
|
|
1115bbc937 | ||
|
|
5b7868080f | ||
|
|
c2219e15f4 | ||
|
|
26b21d7b39 | ||
|
|
fda03cfe21 | ||
|
|
e466683062 | ||
|
|
2fad96945e | ||
|
|
c3b886c196 | ||
|
|
caa8c3568d |
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
@@ -25,17 +25,15 @@ matrix:
|
|||||||
os: osx
|
os: osx
|
||||||
|
|
||||||
# *BSD
|
# *BSD
|
||||||
- env: TARGET=x86_64-unknown-freebsd DISABLE_TESTS=1
|
# - env: TARGET=x86_64-unknown-freebsd DISABLE_TESTS=1
|
||||||
- env: TARGET=x86_64-unknown-netbsd DISABLE_TESTS=1
|
- env: TARGET=x86_64-unknown-netbsd DISABLE_TESTS=1
|
||||||
|
|
||||||
# Windows
|
# Windows
|
||||||
- env: TARGET=x86_64-pc-windows-gnu
|
- env: TARGET=x86_64-pc-windows-gnu
|
||||||
|
|
||||||
allow_failures:
|
|
||||||
- env: TARGET=x86_64-unknown-linux-gnu
|
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- set -e
|
- set -e
|
||||||
|
- command -v apt-get && sudo apt-get install -y libclang-dev
|
||||||
- rustup self update
|
- rustup self update
|
||||||
|
|
||||||
install:
|
install:
|
||||||
@@ -56,7 +54,7 @@ deploy:
|
|||||||
# - Encrypt it: `travis encrypt 0123456789012345678901234567890123456789
|
# - Encrypt it: `travis encrypt 0123456789012345678901234567890123456789
|
||||||
# - Paste the output down here
|
# - Paste the output down here
|
||||||
api_key:
|
api_key:
|
||||||
secure: "VRWAaqZi3ttaVfvgaSK0hQAIYDFUvPdOhhHxQESNO5qoexjc9wioDe7qfi2PcIKEMcEnXgVlmqKEyd0K4Cur9gWKDCfVPUEDTlph+Xdx+p5ViB+9Bz1z9qFSJchZtqqPGqfaq4Fk5HjBr6V31JFV0pjDfy2dHJzjQke+CGG8Zk/1n4H0dYqxZ4EyxcmuIxAnplyUDNdOrbwkuRKEGXLg94Ayk/OmLqHwvCJGYsa3jSgS2ySDT3nRaM9CG5WxKaEQMW4uEWJLbgmqFaxCWh1sOYt+brDWXc9aIJIFsR4b8wdHdHVL0C34dW/0xjxSQPN3PK3jKZYduiu49XgHtpKF9fvsFQSeQwMsAizucJtVKaJr1LfjKk3WwKPuiRotrPVwggdm9/zjXn1POerLR1TMNZr2fxI2iqkXA2sjpRJiDL7sBDg+glY09lGZazyCRIMszxuthoxjw38ZqwYzU4iM7mCkQQHFzVObZdanDtsCH6sM0FDfb7LmJ9uAdDHnzKwTKk23w0hEmdQJHkYimwCQR8Yp7jAhRyxNxYuWtdpuWP4WXDRmLypqMszNqFkZymvVFmMAm0hcSRxwkup4eUWY4yVuHch8/sl51zKXAFk35P2/gZyfoD7+yhiBY5j6dN4s6VBuePc9knRAUP3vWxdTjVg+PoQuWFEC5pHUvsUm/yk="
|
secure: "I1pMvhljeuqXkQDtaktLAcC0VFwlHZIXiM81f+FI+m8pWBV6eAy4alD9+tBSl808g8VIyv1nTbw/UGxVlKALqhR0iT9eCCxvVrgJuWKawJYuzrKxYnKf82t3RjTO1qh6Uf3LeCuZ+ReGbFeR4wTlxC9CQLwdBg3xUA+8bNNJigrEECmNjrUigmRdnKb1R1KXCNW9AwXMlVv+4I1me50/dFLdnhlaAjpsWFXKb2vAk/xoyob1WMEZbInrD/NX1kOz0UPiIX/K8Qjsgp6SAFxjhWGu9jk7VnzvEGPUmTS7OJ4tols2Lhde1AC0y/pElt3YFjA3qfKJfk4B8bUAizomeR+GwJ5YD/CEqhopa8b34SuxeHUOkX9GxPoba08qAmqPAeWcO2hlS0aBiiIUMIOgUqjGDy3MiMqg7VdalkiNtRsr2iCXXSe9p2FPWFsYW1bjGCbIYGBrmWZZEIVYJk2laSmIng27MBvlJFdDkX5Nf3d75Y5U/ZAUwfhpA8ZxwVbddKjYs1S8x09s5yhVOjxJzK2G0aJIg94wMflYEsygrZxjpMkOI79HMZrKArY2S6N6wqgKIdFM2kCmk0ps9DM9pn2o9h91EpnAi7PUd8RIB8yOh9g/K4K9ClvScUqagLR+DvjPoMh68F7Y5XnbuZkIgQ7fKB0cBtB+JnrIE3b3icI="
|
||||||
file_glob: true
|
file_glob: true
|
||||||
file: $CRATE_NAME-$TRAVIS_TAG-$TARGET.*
|
file: $CRATE_NAME-$TRAVIS_TAG-$TARGET.*
|
||||||
on:
|
on:
|
||||||
|
|||||||
1923
Cargo.lock
generated
1923
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
33
Cargo.toml
33
Cargo.toml
@@ -1,17 +1,32 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bin"
|
name = "bin"
|
||||||
version = "1.0.1"
|
version = "1.0.5"
|
||||||
|
description = "a paste bin."
|
||||||
|
repository = "https://github.com/w4/bin"
|
||||||
|
license = "WTFPL OR 0BSD"
|
||||||
authors = ["Jordan Doyle <jordan@doyle.la>"]
|
authors = ["Jordan Doyle <jordan@doyle.la>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
owning_ref = "0.4"
|
||||||
linked-hash-map = "0.5"
|
linked-hash-map = "0.5"
|
||||||
askama = { git = "https://github.com/djc/askama", rev = "fc5addc4", features = ["with-rocket"] }
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" }
|
||||||
askama_escape = { git = "https://github.com/djc/askama", rev = "fc5addc4" }
|
askama = "0.9"
|
||||||
rocket = "0.4"
|
lazy_static = "1.4"
|
||||||
lazy_static = "1.2"
|
rand = { version = "0.7", features = ["nightly"] }
|
||||||
rand = "0.6"
|
|
||||||
gpw = "0.1"
|
gpw = "0.1"
|
||||||
syntect = "3.0"
|
syntect = "4.1"
|
||||||
chashmap = { git = "https://github.com/redox-os/tfs", rev = "b3e7cae1" }
|
serde_derive = "1.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
tokio = { version = "0.2", features = ["sync", "macros"] }
|
||||||
|
async-trait = "0.1"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
codegen-units = 1
|
||||||
|
incremental = false
|
||||||
|
|
||||||
|
[badges]
|
||||||
|
travis-ci = { repository = "w4/bin" }
|
||||||
|
is-it-maintained-issue-resolution = { repository = "w4/bin" }
|
||||||
|
is-it-maintained-open-issues = { repository = "w4/bin" }
|
||||||
|
maintenance = { status = "passively-maintained" }
|
||||||
|
|||||||
17
Dockerfile
Normal file
17
Dockerfile
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
FROM rust:1.34.2-slim-stretch AS builder
|
||||||
|
RUN rustup install nightly-x86_64-unknown-linux-gnu
|
||||||
|
|
||||||
|
RUN apt update && apt install -y libclang-dev
|
||||||
|
|
||||||
|
COPY . /sources
|
||||||
|
WORKDIR /sources
|
||||||
|
RUN cargo +nightly build --release
|
||||||
|
RUN chown nobody:nogroup /sources/target/release/bin
|
||||||
|
|
||||||
|
|
||||||
|
FROM debian:stretch-slim
|
||||||
|
COPY --from=builder /sources/target/release/bin /pastebin
|
||||||
|
|
||||||
|
USER nobody
|
||||||
|
EXPOSE 8000
|
||||||
|
ENTRYPOINT ["/pastebin"]
|
||||||
10
README.md
10
README.md
@@ -1,9 +1,9 @@
|
|||||||
# bin
|
# bin
|
||||||
a paste bin.
|
a paste bin.
|
||||||
|
|
||||||
A paste bin that's actually minimalist. No database requirement, no commenting functionality, no self-destructing or time bomb messages and no social media integration—just an application to quickly send snippits of text to people.
|
A paste bin that's actually minimalist. No database requirement, no commenting functionality, no self-destructing or time bomb messages and no social media integration—just an application to quickly send snippets of text to people.
|
||||||
|
|
||||||
[bin](https://bin.doyle.la/) is written in Rust in around 100 lines of code. It's fast, it's simple, there's code highlighting and you can ⌘+A without going to the 'plain' page. It's revolutionary in the paste bin industry, disrupting markets and pushing boundaries never seen before.
|
[bin](https://bin.gy/) is written in Rust in around 200 lines of code. It's fast, it's simple, there's code highlighting and you can ⌘+A without going to the 'plain' page. It's revolutionary in the paste bin industry, disrupting markets and pushing boundaries never seen before.
|
||||||
|
|
||||||
##### so how do you get bin?
|
##### so how do you get bin?
|
||||||
|
|
||||||
@@ -36,9 +36,9 @@ bin's only configuration value is `BIN_BUFFER_SIZE` which defaults to 2000. Chan
|
|||||||
##### is there curl support?
|
##### is there curl support?
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ curl -X PUT --data 'hello world' bin.doyle.la
|
$ curl -X PUT --data 'hello world' https://bin.gy
|
||||||
https://bin.doyle.la/cateettary
|
https://bin.gy/cateettary
|
||||||
$ curl https://bin.doyle.la/cateettary
|
$ curl https://bin.gy/cateettary
|
||||||
hello world
|
hello world
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ main() {
|
|||||||
|
|
||||||
cross rustc --bin bin --target $TARGET --release -- -C lto
|
cross rustc --bin bin --target $TARGET --release -- -C lto
|
||||||
|
|
||||||
cp target/$TARGET/release/bin $stage/
|
[ "$TARGET" = "arm-unknown-linux-gnueabi" ] || [ "$TARGET" = "x86_64-pc-windows-gnu" ] || strip target/$TARGET/release/bin
|
||||||
|
cp target/$TARGET/release/* $stage/
|
||||||
|
|
||||||
cd $stage
|
cd $stage
|
||||||
tar czf $src/$CRATE_NAME-$TRAVIS_TAG-$TARGET.tar.gz *
|
tar czf $src/$CRATE_NAME-$TRAVIS_TAG-$TARGET.tar.gz *
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ set -ex
|
|||||||
|
|
||||||
# TODO This is the "test phase", tweak it as you see fit
|
# TODO This is the "test phase", tweak it as you see fit
|
||||||
main() {
|
main() {
|
||||||
cross build --target $TARGET
|
cross check
|
||||||
cross build --target $TARGET --release
|
cross build --target $TARGET --release
|
||||||
|
|
||||||
if [ ! -z $DISABLE_TESTS ]; then
|
if [ ! -z $DISABLE_TESTS ]; then
|
||||||
@@ -12,7 +12,6 @@ main() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
cross test --target $TARGET
|
cross test --target $TARGET
|
||||||
cross test --target $TARGET --release
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# we don't run the "test phase" when doing deploys
|
# we don't run the "test phase" when doing deploys
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ with import <nixpkgs> {};
|
|||||||
let src = fetchFromGitHub {
|
let src = fetchFromGitHub {
|
||||||
owner = "mozilla";
|
owner = "mozilla";
|
||||||
repo = "nixpkgs-mozilla";
|
repo = "nixpkgs-mozilla";
|
||||||
# commited 12/2/2019
|
# commited 19/2/2020
|
||||||
rev = "37f7f33ae3ddd70506cd179d9718621b5686c48d";
|
rev = "e912ed483e980dfb4666ae0ed17845c4220e5e7c";
|
||||||
sha256 = "0cmvc9fnr38j3n0m4yf0k6s2x589w1rdby1qry1vh435v79gp95j";
|
sha256 = "08fvzb8w80bkkabc1iyhzd15f4sm7ra10jn32kfch5klgl0gj3j3";
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
with import "${src.out}/rust-overlay.nix" pkgs pkgs;
|
with import "${src.out}/rust-overlay.nix" pkgs pkgs;
|
||||||
@@ -14,6 +14,9 @@ stdenv.mkDerivation {
|
|||||||
buildInputs = [
|
buildInputs = [
|
||||||
latest.rustChannels.nightly.cargo
|
latest.rustChannels.nightly.cargo
|
||||||
latest.rustChannels.nightly.rust
|
latest.rustChannels.nightly.rust
|
||||||
|
stdenv.cc.libc
|
||||||
] ++
|
] ++
|
||||||
pkgs.lib.optional pkgs.stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.Security;
|
pkgs.lib.optional pkgs.stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.Security;
|
||||||
|
|
||||||
|
LIBCLANG_PATH="${llvmPackages.libclang}/lib";
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/io.rs
49
src/io.rs
@@ -1,5 +1,6 @@
|
|||||||
extern crate gpw;
|
extern crate gpw;
|
||||||
extern crate linked_hash_map;
|
extern crate linked_hash_map;
|
||||||
|
extern crate owning_ref;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
use rand::distributions::Alphanumeric;
|
use rand::distributions::Alphanumeric;
|
||||||
@@ -7,9 +8,14 @@ use rand::{thread_rng, Rng};
|
|||||||
|
|
||||||
use linked_hash_map::LinkedHashMap;
|
use linked_hash_map::LinkedHashMap;
|
||||||
|
|
||||||
|
use owning_ref::OwningRef;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::RwLock;
|
|
||||||
|
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||||
|
|
||||||
|
type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||||
@@ -24,13 +30,13 @@ lazy_static! {
|
|||||||
/// `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.
|
||||||
fn purge_old() {
|
async fn purge_old() {
|
||||||
let entries_len = ENTRIES.read().unwrap_or_else(|p| p.into_inner()).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().unwrap_or_else(|p| p.into_inner());
|
let mut entries = ENTRIES.write().await;
|
||||||
|
|
||||||
for _ in 0..to_remove {
|
for _ in 0..to_remove {
|
||||||
entries.pop_front();
|
entries.pop_front();
|
||||||
@@ -41,32 +47,35 @@ fn purge_old() {
|
|||||||
/// Generates a 'pronounceable' random ID using gpw
|
/// Generates a 'pronounceable' random ID using gpw
|
||||||
pub fn generate_id() -> String {
|
pub fn generate_id() -> String {
|
||||||
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
|
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
|
||||||
KEYGEN.with(|k| {
|
|
||||||
k.borrow_mut().next().unwrap_or_else(|| {
|
KEYGEN.with(|k| k.borrow_mut().next()).unwrap_or_else(|| {
|
||||||
thread_rng()
|
thread_rng()
|
||||||
.sample_iter(&Alphanumeric)
|
.sample_iter(&Alphanumeric)
|
||||||
.take(6)
|
.take(6)
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores a paste under the given id
|
/// Stores a paste under the given id
|
||||||
pub fn store_paste(id: String, content: String) {
|
pub async fn store_paste(id: String, content: String) {
|
||||||
purge_old();
|
purge_old().await;
|
||||||
|
|
||||||
ENTRIES
|
ENTRIES
|
||||||
.write()
|
.write()
|
||||||
.unwrap_or_else(|p| p.into_inner())
|
.await
|
||||||
.insert(id, content);
|
.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 fn get_paste(id: &str) -> Option<String> {
|
pub async fn get_paste(id: &str) -> Option<RwLockReadGuardRef<'_, LinkedHashMap<String, String>, String>> {
|
||||||
ENTRIES
|
// need to box the guard until owning_ref understands Pin is a stable address
|
||||||
.read()
|
let or = RwLockReadGuardRef::new(Box::new(ENTRIES.read().await));
|
||||||
.unwrap_or_else(|p| p.into_inner())
|
|
||||||
.get(id)
|
if or.contains_key(id) {
|
||||||
.cloned()
|
Some(or.map(|x| x.get(id).unwrap()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
95
src/main.rs
95
src/main.rs
@@ -1,5 +1,4 @@
|
|||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
#![feature(type_alias_enum_variants)]
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
@@ -8,7 +7,6 @@ extern crate lazy_static;
|
|||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
extern crate askama;
|
extern crate askama;
|
||||||
extern crate askama_escape;
|
|
||||||
|
|
||||||
mod highlight;
|
mod highlight;
|
||||||
mod io;
|
mod io;
|
||||||
@@ -18,16 +16,17 @@ use highlight::highlight;
|
|||||||
use io::{generate_id, get_paste, store_paste};
|
use io::{generate_id, get_paste, store_paste};
|
||||||
use params::{HostHeader, IsPlaintextRequest};
|
use params::{HostHeader, IsPlaintextRequest};
|
||||||
|
|
||||||
use askama::Template;
|
use askama::{Html as AskamaHtml, MarkupDisplay, Template};
|
||||||
use askama_escape::{Html, MarkupDisplay};
|
|
||||||
|
|
||||||
use rocket::http::{ContentType, Status};
|
use rocket::http::{ContentType, RawStr, Status};
|
||||||
use rocket::request::Form;
|
use rocket::request::Form;
|
||||||
use rocket::response::content::Content;
|
use rocket::response::content::{Content, Html};
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::Data;
|
use rocket::Data;
|
||||||
|
|
||||||
use std::io::Read;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use tokio::io::AsyncReadExt;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Homepage
|
/// Homepage
|
||||||
@@ -35,11 +34,14 @@ use std::io::Read;
|
|||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct Index {}
|
struct Index;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> Index {
|
fn index() -> Result<Html<String>, Status> {
|
||||||
Index {}
|
Index
|
||||||
|
.render()
|
||||||
|
.map(Html)
|
||||||
|
.map_err(|_| Status::InternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@@ -52,23 +54,28 @@ struct IndexForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<input>")]
|
#[post("/", data = "<input>")]
|
||||||
fn submit(input: Form<IndexForm>) -> Redirect {
|
async fn submit(input: Form<IndexForm>) -> Redirect {
|
||||||
let id = generate_id();
|
let id = generate_id();
|
||||||
store_paste(id.clone(), input.into_inner().val);
|
let uri = uri!(show_paste: &id);
|
||||||
Redirect::to(uri!(show_paste: id))
|
store_paste(id, input.into_inner().val).await;
|
||||||
|
Redirect::to(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put("/", data = "<input>")]
|
#[put("/", data = "<input>")]
|
||||||
fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
|
async fn submit_raw(input: Data, host: HostHeader<'_>) -> Result<String, Status> {
|
||||||
let mut data = String::new();
|
let mut data = String::new();
|
||||||
input.open().take(1024 * 1000).read_to_string(&mut data)?;
|
input.open().take(1024 * 1000)
|
||||||
|
.read_to_string(&mut data).await
|
||||||
|
.map_err(|_| Status::InternalServerError)?;
|
||||||
|
|
||||||
let id = generate_id();
|
let id = generate_id();
|
||||||
store_paste(id.clone(), data);
|
let uri = uri!(show_paste: &id);
|
||||||
|
|
||||||
|
store_paste(id, data).await;
|
||||||
|
|
||||||
match *host {
|
match *host {
|
||||||
Some(host) => Ok(format!("https://{}{}", host, uri!(show_paste: id))),
|
Some(host) => Ok(format!("https://{}{}", host, uri)),
|
||||||
None => Ok(id),
|
None => Ok(format!("{}", uri)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,39 +85,53 @@ fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
|
|||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "paste.html")]
|
#[template(path = "paste.html")]
|
||||||
struct ShowPaste {
|
struct ShowPaste<'a> {
|
||||||
content: MarkupDisplay<Html, String>,
|
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<key>")]
|
#[get("/<key>")]
|
||||||
fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<String>, Status> {
|
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().ok_or_else(|| Status::NotFound)?;
|
||||||
let ext = splitter.next();
|
let ext = splitter.next();
|
||||||
|
|
||||||
// get() returns a read-only lock, we're not going to be writing to this key
|
let entry = &*get_paste(key).await.ok_or_else(|| Status::NotFound)?;
|
||||||
// again so we can hold this for as long as we want
|
|
||||||
let entry = get_paste(key).ok_or_else(|| Status::NotFound)?;
|
|
||||||
|
|
||||||
if *plaintext {
|
if *plaintext {
|
||||||
Ok(Content(ContentType::Plain, entry))
|
Ok(Content(ContentType::Plain, entry.to_string()))
|
||||||
} else {
|
} else {
|
||||||
let content = match ext {
|
let code_highlighted = match ext {
|
||||||
None => MarkupDisplay::new_unsafe(entry, Html),
|
Some(extension) => match highlight(&entry, extension) {
|
||||||
Some(extension) => highlight(&entry, extension)
|
Some(html) => html,
|
||||||
.map(|h| MarkupDisplay::new_safe(h, Html))
|
None => return Err(Status::NotFound),
|
||||||
.ok_or_else(|| Status::NotFound)?,
|
},
|
||||||
|
None => String::from(RawStr::from_str(entry).html_escape()),
|
||||||
};
|
};
|
||||||
|
|
||||||
ShowPaste { content }
|
// Add <code> tags to enable line numbering with CSS
|
||||||
.render()
|
let html = format!(
|
||||||
.map(|html| Content(ContentType::HTML, html))
|
"<code>{}</code>",
|
||||||
.map_err(|_| Status::InternalServerError)
|
code_highlighted.replace("\n", "\n</code><code>")
|
||||||
|
);
|
||||||
|
|
||||||
|
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
|
||||||
|
|
||||||
|
let template = ShowPaste { content };
|
||||||
|
match template.render() {
|
||||||
|
Ok(html) => Ok(Content(ContentType::HTML, html)),
|
||||||
|
Err(_) => Err(Status::InternalServerError),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
#[tokio::main]
|
||||||
rocket::ignite()
|
async fn main() {
|
||||||
|
let result = rocket::ignite()
|
||||||
.mount("/", routes![index, submit, submit_raw, show_paste])
|
.mount("/", routes![index, submit, submit_raw, show_paste])
|
||||||
.launch();
|
.launch()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Err(e) = result {
|
||||||
|
eprintln!("Failed to launch Rocket: {:#?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ use std::ops::Deref;
|
|||||||
use rocket::request::{FromRequest, Outcome};
|
use rocket::request::{FromRequest, Outcome};
|
||||||
use rocket::Request;
|
use rocket::Request;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
/// 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.
|
||||||
///
|
///
|
||||||
/// We assume anything with the text/plain Accept or Content-Type headers want plaintext,
|
/// We assume anything with the text/plain Accept or Content-Type headers want plaintext,
|
||||||
@@ -17,10 +19,11 @@ impl Deref for IsPlaintextRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest {
|
impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn from_request(request: &'a Request<'r>) -> Outcome<IsPlaintextRequest, ()> {
|
async fn from_request(request: &'a Request<'r>) -> Outcome<IsPlaintextRequest, ()> {
|
||||||
if let Some(format) = request.format() {
|
if let Some(format) = request.format() {
|
||||||
if format.is_plain() {
|
if format.is_plain() {
|
||||||
return Outcome::Success(IsPlaintextRequest(true));
|
return Outcome::Success(IsPlaintextRequest(true));
|
||||||
@@ -54,10 +57,11 @@ impl<'a> Deref for HostHeader<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl<'a, 'r> FromRequest<'a, 'r> for HostHeader<'a> {
|
impl<'a, 'r> FromRequest<'a, 'r> for HostHeader<'a> {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn from_request(request: &'a Request<'r>) -> Outcome<HostHeader<'a>, ()> {
|
async fn from_request(request: &'a Request<'r>) -> Outcome<HostHeader<'a>, ()> {
|
||||||
Outcome::Success(HostHeader(request.headers().get_one("Host")))
|
Outcome::Success(HostHeader(request.headers().get_one("Host")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,12 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
<title>bin.</title>
|
<title>bin.</title>
|
||||||
|
|
||||||
|
<link rel="help" href="https://github.com/w4/bin">
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
* { box-sizing: border-box; }
|
* { box-sizing: border-box; }
|
||||||
|
|
||||||
@@ -25,4 +29,4 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>{% block content %}{% endblock content %}</body>
|
<body>{% block content %}{% endblock content %}</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -9,7 +9,23 @@
|
|||||||
font-family: inherit;
|
font-family: inherit;
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
line-height: inherit;
|
line-height: inherit;
|
||||||
|
counter-reset: line;
|
||||||
}
|
}
|
||||||
|
code {
|
||||||
|
counter-increment: line;
|
||||||
|
}
|
||||||
|
|
||||||
|
code::before {
|
||||||
|
content: counter(line);
|
||||||
|
display: inline-block;
|
||||||
|
width: 2em; /* Fixed width */
|
||||||
|
padding: 0 1em 0.3em 0;
|
||||||
|
margin-right: .5em;
|
||||||
|
color: #888;
|
||||||
|
-webkit-user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
{% endblock styles %}
|
{% endblock styles %}
|
||||||
|
|
||||||
{% block content %}<pre><code>{{ content|safe }}</code></pre>{% endblock content %}
|
{% block content %}<pre>{{ content|safe }}</pre>{% endblock content %}
|
||||||
Reference in New Issue
Block a user