etwist/server.py
changeset 2654 6512522860aa
parent 2476 1294a6bdf3bf
child 2657 de974465d381
equal deleted inserted replaced
2653:51bf32bbe78d 2654:6512522860aa
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 import sys
    10 import sys
       
    11 import os
    11 import select
    12 import select
    12 from time import mktime
    13 from time import mktime
    13 from datetime import date, timedelta
    14 from datetime import date, timedelta
    14 from urlparse import urlsplit, urlunsplit
    15 from urlparse import urlsplit, urlunsplit
       
    16 import hotshot
    15 
    17 
    16 from twisted.application import service, strports
    18 from twisted.application import service, strports
       
    19 from twisted.scripts._twistd_unix import daemonize
    17 from twisted.internet import reactor, task, threads
    20 from twisted.internet import reactor, task, threads
    18 from twisted.internet.defer import maybeDeferred
    21 from twisted.internet.defer import maybeDeferred
    19 from twisted.web2 import channel, http, server, iweb
    22 from twisted.web2 import channel, http, server, iweb
    20 from twisted.web2 import static, resource, responsecode
    23 from twisted.web2 import static, resource, responsecode
    21 
    24 
   266             content = self.appli.loggedout_content(req)
   269             content = self.appli.loggedout_content(req)
   267         else:
   270         else:
   268             content = self.appli.need_login_content(req)
   271             content = self.appli.need_login_content(req)
   269         return http.Response(code, req.headers_out, content)
   272         return http.Response(code, req.headers_out, content)
   270 
   273 
   271 
       
   272 # This part gets run when you run this file via: "twistd -noy demo.py"
       
   273 def main(appid, cfgname):
       
   274     """Starts an cubicweb  twisted server for an instance
       
   275 
       
   276     appid: instance's identifier
       
   277     cfgname: name of the configuration to use (twisted or all-in-one)
       
   278     """
       
   279     from cubicweb.cwconfig import CubicWebConfiguration
       
   280     from cubicweb.etwist import twconfig # trigger configuration registration
       
   281     config = CubicWebConfiguration.config_for(appid, cfgname)
       
   282     # XXX why calling init_available_cubes here ?
       
   283     config.init_available_cubes()
       
   284     # create the site and application objects
       
   285     if '-n' in sys.argv: # debug mode
       
   286         cubicweb = CubicWebRootResource(config, debug=True)
       
   287     else:
       
   288         cubicweb = CubicWebRootResource(config)
       
   289     #toplevel = vhost.VHostURIRewrite(base_url, cubicweb)
       
   290     toplevel = cubicweb
       
   291     website = server.Site(toplevel)
       
   292     application = service.Application("cubicweb")
       
   293     # serve it via standard HTTP on port set in the configuration
       
   294     s = strports.service('tcp:%04d' % (config['port'] or 8080),
       
   295                          channel.HTTPFactory(website))
       
   296     s.setServiceParent(application)
       
   297     return application
       
   298 
       
   299 
       
   300 from twisted.python import failure
   274 from twisted.python import failure
   301 from twisted.internet import defer
   275 from twisted.internet import defer
   302 from twisted.web2 import fileupload
   276 from twisted.web2 import fileupload
   303 
   277 
   304 # XXX set max file size to 100Mo: put max upload size in the configuration
   278 # XXX set max file size to 100Mo: put max upload size in the configuration
   376     print 'IN MEM REQUESTS', count
   350     print 'IN MEM REQUESTS', count
   377     print 'IN MEM APPOBJECTS', acount
   351     print 'IN MEM APPOBJECTS', acount
   378     ocount = sorted(ocount.items(), key=lambda x: x[1], reverse=True)[:20]
   352     ocount = sorted(ocount.items(), key=lambda x: x[1], reverse=True)[:20]
   379     pprint(ocount)
   353     pprint(ocount)
   380     print 'UNREACHABLE', gc.garbage
   354     print 'UNREACHABLE', gc.garbage
       
   355 
       
   356 def run(config, debug):
       
   357     # create the site
       
   358     root_resource = CubicWebRootResource(config, debug)
       
   359     website = server.Site(root_resource)
       
   360     # serve it via standard HTTP on port set in the configuration
       
   361     port = config['port'] or 8080
       
   362     reactor.listenTCP(port, channel.HTTPFactory(website))
       
   363     baseurl = config['base-url'] or config.default_base_url()
       
   364     print "-> Instance started on", baseurl
       
   365     if not debug:
       
   366         daemonize()
       
   367         if config['pid-file']:
       
   368             file(config['pid-file'], 'w').write(str(os.getpid()))
       
   369     if config['profile']:
       
   370         prof = hotshot.Profile(config['profile'])
       
   371         prof.runcall(reactor.run)
       
   372     else:
       
   373         reactor.run()