forked from MIrrors/bin
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4eb9049a1a | ||
|
|
54f531a0a8 | ||
|
|
598baa2ace | ||
|
|
5b5c7dc2fb | ||
|
|
ebe0df5cc6 | ||
|
|
439f4bf2b4 | ||
|
|
377d4a129d | ||
|
|
9c53294b88 | ||
|
|
b095d2464c | ||
|
|
3c76d4fd1a | ||
|
|
06b90b8271 | ||
|
|
21b04b88ae | ||
|
|
53d97709fa | ||
|
|
ff3d193734 | ||
|
|
2b53d0a34c |
@@ -25,7 +25,7 @@ matrix:
|
||||
os: osx
|
||||
|
||||
# *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
|
||||
|
||||
# Windows
|
||||
@@ -33,6 +33,7 @@ matrix:
|
||||
|
||||
before_install:
|
||||
- set -e
|
||||
- command -v apt-get && sudo apt-get install -y libclang-dev
|
||||
- rustup self update
|
||||
|
||||
install:
|
||||
|
||||
1841
Cargo.lock
generated
1841
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
14
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "bin"
|
||||
version = "1.0.3"
|
||||
version = "1.0.5"
|
||||
description = "a paste bin."
|
||||
repository = "https://github.com/w4/bin"
|
||||
license = "WTFPL OR 0BSD"
|
||||
@@ -10,13 +10,15 @@ edition = "2018"
|
||||
[dependencies]
|
||||
owning_ref = "0.4"
|
||||
linked-hash-map = "0.5"
|
||||
rocket = { version = "0.4.4", default-features = false }
|
||||
askama = "0.8"
|
||||
lazy_static = "1.2"
|
||||
rand = { version = "0.6", features = ["nightly"] }
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "master" }
|
||||
askama = "0.9"
|
||||
lazy_static = "1.4"
|
||||
rand = { version = "0.7", features = ["nightly"] }
|
||||
gpw = "0.1"
|
||||
syntect = "3.0"
|
||||
syntect = "4.1"
|
||||
serde_derive = "1.0"
|
||||
tokio = { version = "0.2", features = ["sync", "macros"] }
|
||||
async-trait = "0.1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
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
|
||||
|
||||
10
README.md
10
README.md
@@ -1,9 +1,9 @@
|
||||
# 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 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.
|
||||
[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?
|
||||
|
||||
@@ -36,9 +36,9 @@ bin's only configuration value is `BIN_BUFFER_SIZE` which defaults to 2000. Chan
|
||||
##### is there curl support?
|
||||
|
||||
```bash
|
||||
$ curl -X PUT --data 'hello world' bin.doyle.la
|
||||
https://bin.doyle.la/cateettary
|
||||
$ curl https://bin.doyle.la/cateettary
|
||||
$ curl -X PUT --data 'hello world' https://bin.gy
|
||||
https://bin.gy/cateettary
|
||||
$ curl https://bin.gy/cateettary
|
||||
hello world
|
||||
```
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ main() {
|
||||
|
||||
cross rustc --bin bin --target $TARGET --release -- -C lto
|
||||
|
||||
[ "$TARGET" == "arm-unknown-linux-gnueabi" ] || [ "$TARGET" == "x86_64-pc-windows-gnu" ] || strip target/$TARGET/release/bin
|
||||
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
|
||||
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
|
||||
main() {
|
||||
cross build --target $TARGET
|
||||
cross check
|
||||
cross build --target $TARGET --release
|
||||
|
||||
if [ ! -z $DISABLE_TESTS ]; then
|
||||
@@ -12,7 +12,6 @@ main() {
|
||||
fi
|
||||
|
||||
cross test --target $TARGET
|
||||
cross test --target $TARGET --release
|
||||
}
|
||||
|
||||
# we don't run the "test phase" when doing deploys
|
||||
|
||||
@@ -2,9 +2,9 @@ with import <nixpkgs> {};
|
||||
let src = fetchFromGitHub {
|
||||
owner = "mozilla";
|
||||
repo = "nixpkgs-mozilla";
|
||||
# commited 12/2/2019
|
||||
rev = "37f7f33ae3ddd70506cd179d9718621b5686c48d";
|
||||
sha256 = "0cmvc9fnr38j3n0m4yf0k6s2x589w1rdby1qry1vh435v79gp95j";
|
||||
# commited 19/2/2020
|
||||
rev = "e912ed483e980dfb4666ae0ed17845c4220e5e7c";
|
||||
sha256 = "08fvzb8w80bkkabc1iyhzd15f4sm7ra10jn32kfch5klgl0gj3j3";
|
||||
};
|
||||
in
|
||||
with import "${src.out}/rust-overlay.nix" pkgs pkgs;
|
||||
@@ -14,6 +14,9 @@ stdenv.mkDerivation {
|
||||
buildInputs = [
|
||||
latest.rustChannels.nightly.cargo
|
||||
latest.rustChannels.nightly.rust
|
||||
stdenv.cc.libc
|
||||
] ++
|
||||
pkgs.lib.optional pkgs.stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.Security;
|
||||
|
||||
LIBCLANG_PATH="${llvmPackages.libclang}/lib";
|
||||
}
|
||||
|
||||
26
src/io.rs
26
src/io.rs
@@ -8,11 +8,14 @@ use rand::{thread_rng, Rng};
|
||||
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
|
||||
use owning_ref::RwLockReadGuardRef;
|
||||
use owning_ref::OwningRef;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::env;
|
||||
use std::sync::{PoisonError, RwLock};
|
||||
|
||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||
|
||||
type RwLockReadGuardRef<'a, T, U = T> = OwningRef<Box<RwLockReadGuard<'a, T>>, U>;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||
@@ -27,13 +30,13 @@ lazy_static! {
|
||||
/// `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.
|
||||
fn purge_old() {
|
||||
let entries_len = ENTRIES.read().unwrap_or_else(PoisonError::into_inner).len();
|
||||
async fn purge_old() {
|
||||
let entries_len = ENTRIES.read().await.len();
|
||||
|
||||
if entries_len > *BUFFER_SIZE {
|
||||
let to_remove = entries_len - *BUFFER_SIZE;
|
||||
|
||||
let mut entries = ENTRIES.write().unwrap_or_else(PoisonError::into_inner);
|
||||
let mut entries = ENTRIES.write().await;
|
||||
|
||||
for _ in 0..to_remove {
|
||||
entries.pop_front();
|
||||
@@ -44,6 +47,7 @@ fn purge_old() {
|
||||
/// Generates a 'pronounceable' random ID using gpw
|
||||
pub fn generate_id() -> String {
|
||||
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
|
||||
|
||||
KEYGEN.with(|k| k.borrow_mut().next()).unwrap_or_else(|| {
|
||||
thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
@@ -53,19 +57,21 @@ pub fn generate_id() -> String {
|
||||
}
|
||||
|
||||
/// Stores a paste under the given id
|
||||
pub fn store_paste(id: String, content: String) {
|
||||
purge_old();
|
||||
pub async fn store_paste(id: String, content: String) {
|
||||
purge_old().await;
|
||||
|
||||
ENTRIES
|
||||
.write()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.await
|
||||
.insert(id, content);
|
||||
}
|
||||
|
||||
/// Get a paste by id.
|
||||
///
|
||||
/// Returns `None` if the paste doesn't exist.
|
||||
pub fn get_paste(id: &str) -> Option<RwLockReadGuardRef<LinkedHashMap<String, String>, String>> {
|
||||
let or = RwLockReadGuardRef::new(ENTRIES.read().unwrap_or_else(PoisonError::into_inner));
|
||||
pub async fn get_paste(id: &str) -> Option<RwLockReadGuardRef<'_, LinkedHashMap<String, String>, String>> {
|
||||
// need to box the guard until owning_ref understands Pin is a stable address
|
||||
let or = RwLockReadGuardRef::new(Box::new(ENTRIES.read().await));
|
||||
|
||||
if or.contains_key(id) {
|
||||
Some(or.map(|x| x.get(id).unwrap()))
|
||||
|
||||
35
src/main.rs
35
src/main.rs
@@ -25,7 +25,8 @@ use rocket::response::Redirect;
|
||||
use rocket::Data;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::io::Read;
|
||||
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
///
|
||||
/// Homepage
|
||||
@@ -53,22 +54,24 @@ struct IndexForm {
|
||||
}
|
||||
|
||||
#[post("/", data = "<input>")]
|
||||
fn submit(input: Form<IndexForm>) -> Redirect {
|
||||
async fn submit(input: Form<IndexForm>) -> Redirect {
|
||||
let id = generate_id();
|
||||
let uri = uri!(show_paste: &id);
|
||||
store_paste(id, input.into_inner().val);
|
||||
store_paste(id, input.into_inner().val).await;
|
||||
Redirect::to(uri)
|
||||
}
|
||||
|
||||
#[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();
|
||||
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 uri = uri!(show_paste: &id);
|
||||
|
||||
store_paste(id, data);
|
||||
store_paste(id, data).await;
|
||||
|
||||
match *host {
|
||||
Some(host) => Ok(format!("https://{}{}", host, uri)),
|
||||
@@ -87,14 +90,12 @@ struct ShowPaste<'a> {
|
||||
}
|
||||
|
||||
#[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 key = splitter.next().ok_or_else(|| Status::NotFound)?;
|
||||
let ext = splitter.next();
|
||||
|
||||
// get() returns a read-only lock, we're not going to be writing to this key
|
||||
// again so we can hold this for as long as we want
|
||||
let entry = &*get_paste(key).ok_or_else(|| Status::NotFound)?;
|
||||
let entry = &*get_paste(key).await.ok_or_else(|| Status::NotFound)?;
|
||||
|
||||
if *plaintext {
|
||||
Ok(Content(ContentType::Plain, entry.to_string()))
|
||||
@@ -110,7 +111,7 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<Stri
|
||||
// Add <code> tags to enable line numbering with CSS
|
||||
let html = format!(
|
||||
"<code>{}</code>",
|
||||
code_highlighted.replace("\n", "</code><code>")
|
||||
code_highlighted.replace("\n", "\n</code><code>")
|
||||
);
|
||||
|
||||
let content = MarkupDisplay::new_safe(Cow::Borrowed(&html), AskamaHtml);
|
||||
@@ -123,8 +124,14 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<Stri
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let result = rocket::ignite()
|
||||
.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;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
/// 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,
|
||||
@@ -17,10 +19,11 @@ impl Deref for IsPlaintextRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest {
|
||||
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 format.is_plain() {
|
||||
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> {
|
||||
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")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<title>bin.</title>
|
||||
|
||||
<link rel="help" href="https://github.com/w4/bin">
|
||||
|
||||
<style>
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user