diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/Dockerfile b/Dockerfile index 0e1716d..621073c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e7b913f --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..4a8b9ea --- /dev/null +++ b/README.md @@ -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 :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 . + + location / { + proxy_pass http://localhost:8800; + proxy_set_header Host $host; + proxy_read_timeout 7200; + proxy_send_timeout 7200; + } + + +} #end server stansa +``` + diff --git a/pasteconfig.py b/pasteconfig.py deleted file mode 100644 index 62614ab..0000000 --- a/pasteconfig.py +++ /dev/null @@ -1,12 +0,0 @@ - -DB_FILE = 'paste.db' -BASE_DOMAIN = 'dev.paste.se' -ALT_DOMAINS = [] - -CONFIGURABLE_INDEX = True - -REDIRECT_SCHEME = 'http' - -DEFAULT_LANG="text" - -TORNADOARGS=dict(debug=True) diff --git a/server.py b/server.py index ee52ca3..9bdd63b 100755 --- a/server.py +++ b/server.py @@ -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()) @@ -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),)) @@ -116,8 +122,8 @@ 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", "")) @@ -125,8 +131,8 @@ def get(self): 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 @@ -141,7 +147,7 @@ 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) @@ -149,7 +155,7 @@ def get(self): 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): @@ -219,7 +225,7 @@ 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))) @@ -227,7 +233,7 @@ def post(self): db.close() self.set_cookie("username", tornado.escape.url_escape(user), - domain=pasteconfig.BASE_DOMAIN, + domain=BASE_DOMAIN, path='/', expires_days=30) @@ -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), @@ -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, @@ -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()