Commits

clee  committed f3c7eec

first effort to serve all static files via wsgiserver using WSGI dispatcher

  • Participants
  • Parent commits 3b379e0

Comments (0)

Files changed (2)

File django_wsgiserver/management/commands/runwsgiserver.py

 import logging, sys, os, signal, time, errno
 from socket import gethostname
 from django.core.management.base import BaseCommand
-
+from django_wsgiserver import mediahandler
 
 CPWSGI_HELP = r"""
   Run this project in CherryPy's production quality http webserver.
         #ensure the that the daemon runs as specified user
         change_uid_gid(options['server_user'], options['server_group'])
     
-    from django_wsgiserver.wsgiserver import CherryPyWSGIServer as Server
-    #from cherrypy.wsgiserver import CherryPyWSGIServer as Server
+    from django_wsgiserver.wsgiserver import CherryPyWSGIServer 
+    #from cherrypy.wsgiserver import CherryPyWSGIServer
     from django.core.handlers.wsgi import WSGIHandler
     app = WSGIHandler()
     if options['adminserve']: # serve the admin media too
         import django.core.servers.basehttp
         app = django.core.servers.basehttp.AdminMediaHandler(app)
         
-    server = Server(
+    server = CherryPyWSGIServer(
         (options['host'], int(options['port'])),
         app,
         int(options['threads']), 
         server.stop()
 
 
+
+
+def start_server_servestatic(options):
+    """
+    Start CherryPy server AND serve default static files
+
+    Want SSL support?
+    a. The new (3.1 or 3.2) way: Just set server.ssl_adapter to an SSLAdapter instance.
+    b. The old way (deprecated way) is to set these attributes:
+
+       server.ssl_certificate = <filename>
+       server.ssl_private_key = <filename>
+
+       But this is the only way from the management command line
+       in the future I may need to adapt this to use a server.ssl_adapter
+
+    """
+    
+    if options['daemonize'] and options['server_user'] and options['server_group']:
+        #ensure the that the daemon runs as specified user
+        change_uid_gid(options['server_user'], options['server_group'])
+    
+    from django_wsgiserver.wsgiserver import CherryPyWSGIServer, WSGIPathInfoDispatcher
+    #from cherrypy.wsgiserver import CherryPyWSGIServer, WSGIPathInfoDispatcher
+    from django.core.handlers.wsgi import WSGIHandler
+    from django.conf import settings
+    app = WSGIHandler()
+    if options['adminserve']: # serve the admin media too
+        # AdminMediaHandler is middleware for local use
+        import django.core.servers.basehttp
+        app = django.core.servers.basehttp.AdminMediaHandler(app)
+    # another way to serve the admin media three application
+    path = { '/': app,
+             settings.MEDIA_URL: mediahandler.MediaHandler(settings.MEDIA_ROOT),
+             settings.ADMIN_MEDIA_PREFIX: mediahandler.MediaHandler(
+                 os.path.join( django.contrib.admin.__path__[0],
+                               'media' )
+                 )
+             }
+    dispatcher =  WSGIPathInfoDispatcher( path )
+        
+    server = CherryPyWSGIServer(
+        (options['host'], int(options['port'])),
+        dispatcher,
+        int(options['threads']), 
+        options['server_name']
+    )
+    if options['ssl_certificate'] and options['ssl_private_key']:
+        server.ssl_certificate = options['ssl_certificate']
+        server.ssl_private_key = options['ssl_private_key']  
+    try:
+        server.start()
+    except KeyboardInterrupt:
+        server.stop()
+
 def runwsgiserver(argset=[], **kwargs):
     # Get the options
     options = CPWSGI_OPTIONS.copy()
     
     # Start the webserver
     print 'starting server with options %s' % options
-    start_server(options)
+    start_server_servestatic(options)
 
 
 if __name__ == '__main__':
 # modified version no cherrypy reliance
 setup(
     name="django-wsgiserver",
-    version="0.6.5",
+    version="0.7.0",
     description="""django-wsgiserver installs a web server for Django using CherryPy's WSGI server.""",
     author="""Peter Baumgartner Chris Lee-Messer the authors of CherryPy""",
     author_email="pete@lincolnloop.com, chris@lee-messer.net",