|
| 1 | +""" |
| 2 | +Yarrharr production server via Twisted Web |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import signal |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from twisted.python import log |
| 10 | +from twisted.web.wsgi import WSGIResource |
| 11 | +from twisted.web.server import Site |
| 12 | +from twisted.web.static import File |
| 13 | +from twisted.web.resource import Resource |
| 14 | +from twisted.python.logfile import LogFile |
| 15 | +from twisted.internet.endpoints import serverFromString |
| 16 | + |
| 17 | +from . import __version__ |
| 18 | +from .wsgi import application |
| 19 | + |
| 20 | + |
| 21 | +class FallbackResource(Resource): |
| 22 | + """ |
| 23 | + Resource which falls back to an alternative resource tree if it doesn't |
| 24 | + have a matching child resource. |
| 25 | + """ |
| 26 | + def __init__(self, fallback): |
| 27 | + Resource.__init__(self) |
| 28 | + self.fallback = fallback |
| 29 | + |
| 30 | + def render(self, request): |
| 31 | + """ |
| 32 | + Render this path with the fallback resource. |
| 33 | + """ |
| 34 | + return self.fallback.render(request) |
| 35 | + |
| 36 | + def getChild(self, path, request): |
| 37 | + """ |
| 38 | + Dispatch unhandled requests to the fallback resource. |
| 39 | + """ |
| 40 | + # Mutate the request path such that it's like FallbackResource didn't handle |
| 41 | + # the request at all. This is a bit of a nasty hack, since we're |
| 42 | + # relying on the t.w.server implementation's behavior to not break when |
| 43 | + # we do this. A better way would be to create a wrapper for the request object |
| 44 | + request.postpath.insert(0, request.prepath.pop()) |
| 45 | + return self.fallback |
| 46 | + |
| 47 | + |
| 48 | +class Root(FallbackResource): |
| 49 | + def __init__(self, reactor): |
| 50 | + wsgi = WSGIResource(reactor, reactor.getThreadPool(), application) |
| 51 | + |
| 52 | + FallbackResource.__init__(self, wsgi) |
| 53 | + |
| 54 | + # Install our static file handlers. |
| 55 | + self.putChild('static', File(settings.STATIC_ROOT)) |
| 56 | + |
| 57 | + |
| 58 | +def run(sigstop=False, logPath=None): |
| 59 | + from twisted.internet import reactor |
| 60 | + if settings.LOG_SERVER is not None: |
| 61 | + # TODO: Reopen on SIGHUP so logrotate can handle compression. |
| 62 | + logfile = LogFile.fromFullPath(settings.LOG_SERVER) |
| 63 | + log.startLogging(logfile) |
| 64 | + |
| 65 | + log.msg("Yarrharr {} starting".format(__version__)) |
| 66 | + |
| 67 | + factory = Site(Root(reactor), logPath=settings.LOG_ACCESS) |
| 68 | + endpoint = serverFromString(reactor, settings.SERVER_ENDPOINT) |
| 69 | + |
| 70 | + reactor.addSystemEventTrigger('before', 'startup', endpoint.listen, factory) |
| 71 | + |
| 72 | + if sigstop: |
| 73 | + def send_sigstop(): |
| 74 | + """ |
| 75 | + Tell Upstart we've successfully started. |
| 76 | + """ |
| 77 | + os.kill(os.getpid(), signal.SIGSTOP) |
| 78 | + |
| 79 | + reactor.addSystemEventTrigger('after', 'startup', sigstop) |
| 80 | + |
| 81 | + reactor.run() |
0 commit comments