Last active
July 8, 2018 08:11
-
-
Save sorz/885561c8e10122073f6e to your computer and use it in GitHub Desktop.
Source code of paste.sorz.org
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
server { | |
server_name example.com; | |
charset utf-8; | |
location ~ "/t/([^/]+)(/[^/]+)?$" { | |
alias /srv/http/paste/data/$1; | |
gunzip on; | |
gzip_static always; | |
default_type text/plain; | |
expires 90d; | |
} | |
location / { | |
root /srv/http/paste/; | |
index index.html; | |
} | |
location /data { | |
deny all; | |
} | |
location /new { | |
include proxy_params; | |
proxy_pass http://127.0.0.1:8080; | |
client_max_body_size 8M; | |
} | |
} |
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
#!/usr/bin/env python3 | |
import os | |
from random import SystemRandom | |
import time | |
import gzip | |
from flask import Flask, redirect, request | |
app = Flask(__name__) | |
STORAGE_PATH = "/srv/http/paste/data" | |
VIEW_PATH = "/t/%s" | |
ID_LENGTH = 5 | |
ID_CHOICE = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnoprstuvwxy345678' | |
ID_RETRY = 2 | |
LIMIT_COUNT = 50 | |
LIMIT_INTERVAL = 5 * 60 | |
GZIP_LEVEL = 9 | |
GZIP_THRESHOLD = 120 | |
limit_time = time.time() | |
limit_count = 0 | |
random = SystemRandom() | |
def _get_random_id(): | |
return ''.join([random.choice(ID_CHOICE) for i in range(ID_LENGTH)]) | |
def _get_path(sid): | |
return os.path.join(STORAGE_PATH, "%s.gz" % sid) | |
@app.route("/new", methods=['POST']) | |
def new(): | |
global limit_time, limit_count | |
if time.time() - limit_time > LIMIT_INTERVAL: | |
limit_time = time.time() | |
limit_count = 0 | |
elif limit_count > LIMIT_COUNT: | |
return "Ops, please contact administrator and retry in minutes.", 503 | |
limit_count += 1 | |
sid = _get_random_id() | |
retry = 0 | |
while retry < ID_RETRY and os.path.exists(_get_path(sid)): | |
retry += 1 | |
sid = _get_random_id() | |
if retry >= ID_RETRY: | |
return "Bad luck! Please contact administrator.", 500 | |
text = request.form['text'] | |
if len(text) <= GZIP_THRESHOLD: | |
with open(_get_path(sid)[:-3], 'wt', encoding='utf-8') as out: | |
out.write(text) | |
else: | |
with gzip.open(_get_path(sid), 'wt', GZIP_LEVEL, 'utf-8') as out: | |
out.write(text) | |
return redirect(VIEW_PATH % sid) | |
if __name__ == "__main__": | |
app.run(port=8080) |
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
[Unit] | |
Description=Paste nginx backend deamon. | |
[Service] | |
Type=simple | |
User=http | |
Group=http | |
WorkingDirectory=/path/to/paste/ | |
ExecStart=/path/to/paste/env/bin/python paste.py | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment