2010-12-07 02:34:59 +00:00
|
|
|
"""
|
|
|
|
|
This implements resources for twisted webservers using the wsgi
|
|
|
|
|
interface of django. This alleviates the need of running e.g. an
|
|
|
|
|
apache server to serve Evennia's web presence (although you could do
|
|
|
|
|
that too if desired).
|
|
|
|
|
|
|
|
|
|
The actual servers are started inside server.py as part of the Evennia
|
|
|
|
|
application.
|
|
|
|
|
|
|
|
|
|
(Lots of thanks to http://githup.com/clemensha/twisted-wsgi-django for
|
|
|
|
|
a great example/aid on how to do this.)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
from twisted.web import resource
|
|
|
|
|
from twisted.python import threadpool
|
|
|
|
|
from twisted.internet import reactor
|
2011-02-23 21:08:02 +00:00
|
|
|
from twisted.application import service, internet
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
from twisted.web.wsgi import WSGIResource
|
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Website server resource
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
class DjangoWebRoot(resource.Resource):
|
|
|
|
|
"""
|
|
|
|
|
This creates a web root (/) that Django
|
|
|
|
|
understands by tweaking the way the
|
|
|
|
|
child instancee are recognized.
|
|
|
|
|
"""
|
2011-02-23 21:08:02 +00:00
|
|
|
def __init__(self, pool):
|
2010-12-07 02:34:59 +00:00
|
|
|
"""
|
|
|
|
|
Setup the django+twisted resource
|
|
|
|
|
"""
|
2011-02-23 21:08:02 +00:00
|
|
|
resource.Resource.__init__(self)
|
|
|
|
|
self.wsgi_resource = WSGIResource(reactor, pool , WSGIHandler())
|
2010-12-07 02:34:59 +00:00
|
|
|
|
|
|
|
|
def getChild(self, path, request):
|
|
|
|
|
"""
|
|
|
|
|
To make things work we nudge the
|
|
|
|
|
url tree to make this the root.
|
|
|
|
|
"""
|
|
|
|
|
path0 = request.prepath.pop(0)
|
|
|
|
|
request.postpath.insert(0, path0)
|
|
|
|
|
return self.wsgi_resource
|
2011-02-23 21:08:02 +00:00
|
|
|
|
|
|
|
|
class WSGIWebServer(internet.TCPServer):
|
|
|
|
|
"""
|
|
|
|
|
This is a WSGI webserver. It makes sure to start
|
|
|
|
|
the threadpool after the service itself started,
|
|
|
|
|
so as to register correctly with the twisted daemon.
|
|
|
|
|
|
|
|
|
|
call with WSGIWebServer(threadpool, port, wsgi_resource)
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, pool, *args, **kwargs ):
|
|
|
|
|
"This just stores the threadpool"
|
|
|
|
|
self.pool = pool
|
|
|
|
|
internet.TCPServer.__init__(self, *args, **kwargs)
|
|
|
|
|
def startService(self):
|
|
|
|
|
"Start the pool after the service"
|
|
|
|
|
internet.TCPServer.startService(self)
|
|
|
|
|
self.pool.start()
|
|
|
|
|
def stopService(self):
|
|
|
|
|
"Safely stop the pool after service stop."
|
|
|
|
|
internet.TCPServer.stopService(self)
|
|
|
|
|
self.pool.stop()
|