From 6bdb9f01a6fed8f479506512b444f4de3895239a Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 20 Dec 2018 10:48:43 -0700 Subject: [PATCH] Add basic setup for tild.es link-shortener Links aren't displayed/used anywhere yet, but this should be the basic setup needed for a simple link-shortener on the tild.es domain. Currently, it will support two uses: * https://tild.es/asdf - redirect to topic with id "asdf" * https://tild.es/~asdf - redirect to group "~asdf" --- salt/salt/nginx/shortener-config.sls | 16 +++++++++++ salt/salt/nginx/tildes-shortener.conf.jinja2 | 23 ++++++++++++++++ salt/salt/top.sls | 1 + tildes/tildes/routes.py | 6 +++++ tildes/tildes/views/shortener.py | 28 ++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 salt/salt/nginx/shortener-config.sls create mode 100644 salt/salt/nginx/tildes-shortener.conf.jinja2 create mode 100644 tildes/tildes/views/shortener.py diff --git a/salt/salt/nginx/shortener-config.sls b/salt/salt/nginx/shortener-config.sls new file mode 100644 index 0000000..03cc034 --- /dev/null +++ b/salt/salt/nginx/shortener-config.sls @@ -0,0 +1,16 @@ +/etc/nginx/sites-available/tildes-shortener.conf: + file.managed: + - source: salt://nginx/tildes-shortener.conf.jinja2 + - template: jinja + - user: root + - group: root + - mode: 644 + - makedirs: True + +/etc/nginx/sites-enabled/tildes-shortener.conf: + file.symlink: + - target: /etc/nginx/sites-available/tildes-shortener.conf + - makedirs: True + - user: root + - group: root + - mode: 644 diff --git a/salt/salt/nginx/tildes-shortener.conf.jinja2 b/salt/salt/nginx/tildes-shortener.conf.jinja2 new file mode 100644 index 0000000..922edfa --- /dev/null +++ b/salt/salt/nginx/tildes-shortener.conf.jinja2 @@ -0,0 +1,23 @@ +server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + + add_header Strict-Transport-Security "max-age={{ pillar['hsts_max_age'] }}; includeSubDomains; preload" always; + + add_header X-Content-Type-Options "nosniff" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Xss-Protection "1; mode=block" always; + add_header Referrer-Policy "same-origin" always; + + server_name tild.es; + + keepalive_timeout 5; + + location / { + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_redirect off; + proxy_pass http://app_server/shortener/; + } +} diff --git a/salt/salt/top.sls b/salt/salt/top.sls index 4d5434f..3e189c4 100644 --- a/salt/salt/top.sls +++ b/salt/salt/top.sls @@ -28,6 +28,7 @@ base: - development - prometheus 'prod': + - nginx.shortener-config - nginx.static-sites-config - raven - prod-config diff --git a/tildes/tildes/routes.py b/tildes/tildes/routes.py index 4d0b1dd..1b0d88d 100644 --- a/tildes/tildes/routes.py +++ b/tildes/tildes/routes.py @@ -90,6 +90,12 @@ def includeme(config: Configurator) -> None: with config.route_prefix_context("/api/web"): add_intercooler_routes(config) + # Add routes for the link-shortener under the /shortener path + config.add_route("shortener", "/shortener") + with config.route_prefix_context("/shortener"): + config.add_route("shortener_group", "/~{group_path}", factory=group_by_path) + config.add_route("shortener_topic", "/{topic_id36}", factory=topic_by_id36) + def add_intercooler_routes(config: Configurator) -> None: """Set up all routes for the (internal-use) Intercooler API endpoints.""" diff --git a/tildes/tildes/views/shortener.py b/tildes/tildes/views/shortener.py new file mode 100644 index 0000000..a3de4cf --- /dev/null +++ b/tildes/tildes/views/shortener.py @@ -0,0 +1,28 @@ +# Copyright (c) 2018 Tildes contributors +# SPDX-License-Identifier: AGPL-3.0-or-later + +"""Views related to the link shortener.""" + +from pyramid.httpexceptions import HTTPFound +from pyramid.request import Request +from pyramid.response import Response +from pyramid.view import view_config + + +@view_config(route_name="shortener") +def get_shortener(request: Request) -> Response: + """Display a message if someone just visits the base shortener domain.""" + # pylint: disable=unused-argument + return Response("Link-shortener for tildes.net") + + +@view_config(route_name="shortener_group") +def get_shortener_group(request: Request) -> HTTPFound: + """Redirect to the base path of a group.""" + raise HTTPFound(location=f"/~{request.context.path}") + + +@view_config(route_name="shortener_topic") +def get_shortener_topic(request: Request) -> HTTPFound: + """Redirect to the full permalink for a topic.""" + raise HTTPFound(location=request.context.permalink)