application.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:application.py
  6. author:rabin
  7. """
  8. from demeter.core import *
  9. import tornado.web
  10. import tornado.ioloop
  11. import tornado.httpserver
  12. def start(url):
  13. config = Demeter.config[Demeter.web]
  14. settings = {
  15. "static_path": os.path.join(os.path.dirname(__file__), "static"),
  16. "template_path": os.path.join(os.path.dirname(__file__), 'templates'),
  17. "cookie_secret": "61oETzKXQAGaYekL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
  18. "login_url": "/user/login",
  19. "xsrf_cookies": True,
  20. "debug": Demeter.bool(config['debug']),
  21. #"autoreload": Demeter.bool(config['autoreload']),
  22. "port": config['port'],
  23. "max_buffer_size": int(config['max_buffer_size']),
  24. "process": int(config['process'])
  25. }
  26. handlers = []
  27. def application_setting():
  28. handlers.append((r"/upload/(.*)", tornado.web.StaticFileHandler, {"path": Demeter.path + 'runtime/upload/'}))
  29. handlers.append((r"/static/(.*)", tornado.web.StaticFileHandler, {"path":"static"}))
  30. handlers.append((r"/(apple-touch-icon\.png)", tornado.web.StaticFileHandler, dict(path=settings['static_path'])))
  31. handlers.extend(url)
  32. application_setting()
  33. application = tornado.web.Application(handlers=handlers, **settings)
  34. if settings['debug'] == True:
  35. application.listen(settings['port'])
  36. tornado.ioloop.IOLoop.instance().start()
  37. else:
  38. server = tornado.httpserver.HTTPServer(application, settings['max_buffer_size'])
  39. server.bind(settings['port'])
  40. server.start(settings['process'])
  41. try:
  42. print 'running on port %s' % settings['port']
  43. tornado.ioloop.IOLoop.instance().start()
  44. except KeyboardInterrupt:
  45. tornado.ioloop.IOLoop.instance().stop()