Skip to content

moar dockerized #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
22 changes: 8 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
FROM python:2.7-alpine
FROM jfloff/alpine-python:2.7-slim

RUN pip install tornado pygments

ARG domain=dev.paste.se
ARG deflang=text
ARG configurable_index=True
ARG altdomains=[]

ADD server.py favicon.ico /paste/
ADD templates/*.html /paste/templates/

RUN \
echo "DB_FILE = '/data/paste.db'" >/paste/pasteconfig.py; \
echo "REDIRECT_SCHEME = 'http'" >>/paste/pasteconfig.py; \
echo "BASE_DOMAIN = '${domain}'" >>/paste/pasteconfig.py; \
echo "ALT_DOMAINS = ${altdomains}" >>/paste/pasteconfig.py; \
echo "DEFAULT_LANG = '${deflang}'" >>/paste/pasteconfig.py; \
echo "CONFIGURABLE_INDEX = ${configurable_index}" >>/paste/pasteconfig.py; \
echo "TORNADOARGS=dict(debug=True)" >>/paste/pasteconfig.py;
ENV DB_FILE 'paste.se'
ENV BASE_DOMAIN 'dev.paste.se'
ENV ALT_DOMAINS "[]"
ENV CONFIGURABLE_INDEX True
ENV REDIRECT_SCHEME http
ENV DEFAULT_LANG "text"
ENV TORNADOARGS dict(debug=True)

WORKDIR /paste

Expand Down
72 changes: 72 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
CONTAINER := paste.se
HUB_USER := ${USER}
TAG := v0.2
IMAGE_NAME := ${CONTAINER}:${TAG}
PORT := 8800
VOLUME := data
BASE_DOMAIN := localhost

build:
docker \
build \
--tag=${CONTAINER} \
.

run:
docker \
run \
--detach \
--hostname=${CONTAINER} \
--name=${CONTAINER} \
-e BASE_DOMAIN=${BASE_DOMAIN} \
-p ${PORT}:8800 \
-v ${VOLUME}:/data \
${CONTAINER}

shell:
docker \
run \
--rm \
--interactive \
--tty \
--hostname=${CONTAINER} \
--name=${CONTAINER} \
-e BASE_DOMAIN=${BASE_DOMAIN} \
-v ${VOLUME}:/data \
${CONTAINER} \
/bin/bash

exec:
docker exec \
--interactive \
--tty \
${CONTAINER} \
/bin/sh

stop:
docker \
kill ${CONTAINER}

rm:
docker \
rm ${CONTAINER}

history:
docker \
history ${CONTAINER}

clean:
-docker \
rm ${CONTAINER}
-docker \
rmi ${CONTAINER}

commit:
docker commit -m "Built version ${TAG}" -a "${USER}" ${CONTAINER} ${HUB_USER}/${CONTAINER}:${TAG}

push:
docker tag ${CONTAINER} ${HUB_USER}/${CONTAINER}:${TAG}
docker tag ${CONTAINER} ${HUB_USER}/${CONTAINER}:latest
docker push ${HUB_USER}/${CONTAINER}:latest

restart: stop clean run
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# paste.se

Self-hosted pastebin-like services, written in Python/Tornado/SQLite.

You will need a wildcard DNS record to the paste server since it uses a [bfe0c9274d1deea6.paste.se](https://bfe0c9274d1deea6.paste.se/) hash format to generate a link to the paste.

To make paste data persistent, just map /data to some persistent dir on your server.

The default port is 8800, change that in the `-p <myport>:8800` part of the command..

Parameters can be overwritten by setting them directly in the docker Environment, Here they are:
```
ENV DB_FILE 'paste.se'
ENV BASE_DOMAIN 'dev.paste.se'
ENV ALT_DOMAINS "[]"
ENV CONFIGURABLE_INDEX True
ENV REDIRECT_SCHEME http
ENV DEFAULT_LANG "text"
ENV TORNADOARGS dict(debug=True)
```

Start the docker like so:
```docker
docker \
run \
--detach \
--name=paste.se \
-e BASE_DOMAIN=localhost \
-p 8800:8800 \
-v /srv/someplace:/data \
paste.se
```

## nginx

If you run paste.se behind a nginx proxy, you can use this config as a template. Replace $BASE_DOMAIN and localhost in the `proxy_pass` section to your setup.

```nginx

server {
listen 80;
server_name .<BASE_DOMAIN>

location / {
proxy_pass http://localhost:8800;
proxy_set_header Host $host;
proxy_read_timeout 7200;
proxy_send_timeout 7200;
}


} #end server stansa
```

12 changes: 0 additions & 12 deletions pasteconfig.py

This file was deleted.

34 changes: 20 additions & 14 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
import pygments.lexers
from pygments.formatters import HtmlFormatter, ImageFormatter, TerminalFormatter

import pasteconfig

import os
DB_FILE = os.getenv('DB_FILE', 'paste.se')
BASE_DOMAIN = os.getenv('BASE_DOMAIN', 'dev.paste.se')
ALT_DOMAINS = os.getenv('ALT_DOMAINS', [])
CONFIGURABLE_INDEX = os.getenv('CONFIGURABLE_INDEX', True)
REDIRECT_SCHEME = os.getenv('REDIRECT_SCHEME', 'http')
DEFAULT_LANG = os.getenv('DEFAULT_LANG', "text")
TORNADOARGS = os.getenv('TORNADOARGS', dict(debug=True))

OK_LANGS = [x[2][0] for x in pygments.lexers.LEXERS.values()]
OK_LANGS.sort(key=lambda x: x.lower())
Expand Down Expand Up @@ -83,7 +89,7 @@ class PasteBaseHandler(tornado.web.RequestHandler):
def _get_paste(self, fields, key=None):
if key is None:
key = self.request.host.split(".")[0]
db = sqlite3.connect(pasteconfig.DB_FILE)
db = sqlite3.connect(DB_FILE)
c = db.cursor()
try:
c.execute("SELECT "+(",".join(fields))+" FROM paste WHERE hash=?", (str(key),))
Expand Down Expand Up @@ -116,17 +122,17 @@ def get(self):

class MainHandler(PasteBaseHandler):
def get(self):
if (self.request.host.split(":")[0] == pasteconfig.BASE_DOMAIN or
self.request.host.split(":")[0] in pasteconfig.ALT_DOMAINS or
if (self.request.host.split(":")[0] == BASE_DOMAIN or
self.request.host.split(":")[0] in ALT_DOMAINS or
self.request.host.split(".")[0] == "new"):
try:
uname = tornado.escape.url_unescape(self.get_cookie("username", ""))
except Exception:
uname = ""
self.render("templates/main.html",
username=uname,
default_lang=pasteconfig.DEFAULT_LANG,
configurable_index=pasteconfig.CONFIGURABLE_INDEX,
default_lang=DEFAULT_LANG,
configurable_index=CONFIGURABLE_INDEX,
langs=OK_LANGS)
return

Expand All @@ -141,15 +147,15 @@ def get(self):
self.render("templates/404.html",
key=self.request.host.split(".")[0],
host=self.request.host,
base=pasteconfig.BASE_DOMAIN)
base=BASE_DOMAIN)
return

lexer = pygments.lexers.get_lexer_by_name(lang)
formatter = HtmlFormatter(linenos=True, cssclass="source")
paste = stripctlchars(highlight(paste, lexer, formatter))
css = formatter.get_style_defs(arg='')

self.render("templates/paste.html", css=css, user=user, desc=desc, paste=paste, basedomain=pasteconfig.BASE_DOMAIN)
self.render("templates/paste.html", css=css, user=user, desc=desc, paste=paste, basedomain=BASE_DOMAIN)


class TermHandler(PasteBaseHandler):
Expand Down Expand Up @@ -219,15 +225,15 @@ def post(self):
desc.encode("utf-8") +
paste.encode("utf-8")).hexdigest()[:16]

db = sqlite3.connect(pasteconfig.DB_FILE)
db = sqlite3.connect(DB_FILE)
c = db.cursor()
c.execute("REPLACE into paste (hash, user, description, lang, paste, may_index) VALUES (?, ?, ?, ?, ?, ?)",
(key, user, desc, lang, paste, int(index)))
db.commit()
db.close()

self.set_cookie("username", tornado.escape.url_escape(user),
domain=pasteconfig.BASE_DOMAIN,
domain=BASE_DOMAIN,
path='/',
expires_days=30)

Expand All @@ -236,7 +242,7 @@ def post(self):
else:
base_host = self.request.host

self.redirect("{}://{}.{}/".format(pasteconfig.REDIRECT_SCHEME, key, base_host))
self.redirect("{}://{}.{}/".format(REDIRECT_SCHEME, key, base_host))

routes = [
(r"/robots.txt", RobotsTxtHandler),
Expand All @@ -250,7 +256,7 @@ def post(self):


def create_db_if_not_exists():
db = sqlite3.connect(pasteconfig.DB_FILE)
db = sqlite3.connect(DB_FILE)
c = db.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS paste (
hash PRIMARY KEY,
Expand All @@ -264,7 +270,7 @@ def create_db_if_not_exists():


application = tornado.web.Application(routes,
**pasteconfig.TORNADOARGS)
TORNADOARGS)

if __name__ == "__main__":
create_db_if_not_exists()
Expand Down