Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: ba9f26d3ba
Fetching contributors…

Cannot retrieve contributors at this time

executable file 79 lines (62 sloc) 2.07 KB
#!/usr/bin/env python
"""PostgreSQL Planet Aggregator
This is a FastCGI implementation of the planet URL redirector.
Accepts URLs like /p/<shorturl> and sends a redirect to the proper
URL for the post requested.
Copyright (C) 2011 PostgreSQL Global Development Group
import ConfigParser
import psycopg2
from flup.server.fcgi import WSGIServer
# Simple map used to shorten id values to URLs
_urlvalmap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_']
connstr = ""
def iddecode(idstr):
idval = 0
for c in idstr:
idval *= 64
idval += _urlvalmap.index(c)
return idval
def redirapp(environ, start_response):
# Start by getting the id from the request
id = iddecode(environ['PATH_INFO'].split('/')[-1])
# Let's figure out where this URL should be
# Since we cache heavily with varnish in front of this, we don't
# bother with any connection pooling.
conn = psycopg2.connect(connstr)
c = conn.cursor()
c.execute("SELECT link FROM planet.posts WHERE id=%(id)s", {
'id': id
r = c.fetchall()
if len(r) != 1:
start_response('404 Not Found', [
('Content-type', 'text/plain'),
return ["Link not found\n"]
# We have a link, return a redirect to it
start_response('301 Moved Permanently', [
('Content-type', 'text/html'),
('Location', r[0][0]),
('X-Planet', str(id))
return [
"<a href=\"%s\">moved here</a>\n" % r[0][0],
except Exception, ex:
start_response('500 Internal Server Error', [
('Content-type', 'text/plain')
return [
"An internal server error occured\n",
if __name__ == '__main__':
c = ConfigParser.ConfigParser()'redir.ini')
connstr = c.get('planet', 'db')
Jump to Line
Something went wrong with that request. Please try again.