Browse Source

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"
merge-requests/53/head
Deimos 6 years ago
parent
commit
6bdb9f01a6
  1. 16
      salt/salt/nginx/shortener-config.sls
  2. 23
      salt/salt/nginx/tildes-shortener.conf.jinja2
  3. 1
      salt/salt/top.sls
  4. 6
      tildes/tildes/routes.py
  5. 28
      tildes/tildes/views/shortener.py

16
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

23
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/;
}
}

1
salt/salt/top.sls

@ -28,6 +28,7 @@ base:
- development - development
- prometheus - prometheus
'prod': 'prod':
- nginx.shortener-config
- nginx.static-sites-config - nginx.static-sites-config
- raven - raven
- prod-config - prod-config

6
tildes/tildes/routes.py

@ -90,6 +90,12 @@ def includeme(config: Configurator) -> None:
with config.route_prefix_context("/api/web"): with config.route_prefix_context("/api/web"):
add_intercooler_routes(config) 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: def add_intercooler_routes(config: Configurator) -> None:
"""Set up all routes for the (internal-use) Intercooler API endpoints.""" """Set up all routes for the (internal-use) Intercooler API endpoints."""

28
tildes/tildes/views/shortener.py

@ -0,0 +1,28 @@
# Copyright (c) 2018 Tildes contributors <code@tildes.net>
# 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)
Loading…
Cancel
Save