PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/run_server.py

https://gitlab.com/lotaku/tornado_jinja
Python | 69 lines | 56 code | 8 blank | 5 comment | 4 complexity | c48794ab7074d0761d17442b57a8ff54 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. from july.util import reset_option, parse_config_file
  5. from july.app import JulyApplication
  6. from july.web import init_options, run_server, JulyHandler
  7. import motor
  8. import tornado
  9. from tornado.options import options
  10. from app.util.july import JinjaLoader, NewJulyApplication
  11. import datetime
  12. PROJDIR = os.path.abspath(os.path.dirname(__file__))
  13. ROOTDIR = os.path.split(PROJDIR)[0]
  14. TEMPLATES_DIR = PROJDIR + '/templates'
  15. try:
  16. import app
  17. print('Start app version: %s' % app.__version__)
  18. #todo 怎么是ROOTDIR
  19. sys.path.append(ROOTDIR)
  20. except ImportError:
  21. import site
  22. site.addsitedir(ROOTDIR)
  23. print('Development of app')
  24. reset_option('template_path', os.path.join(PROJDIR, "templates"))
  25. reset_option('login_url', 'account/signin', type=str)
  26. reset_option('static_path', os.path.join(PROJDIR, 'static'))
  27. reset_option('static_url_prefix', '/static/', type=str)
  28. # reset_option('locale_path', os.path.join(PROJDIR, 'locale'))
  29. #todo ?
  30. reset_option('sqlalchemy_kwargs', {}, type=dict)
  31. def create_application():
  32. try:
  33. client = motor.MotorClient('%s:%s'%(options.mongo_host, options.mongo_port))
  34. mongo_db = client[options.mongo_db]
  35. except:
  36. print "cannot connect mongodb, check the config.yaml"
  37. sys.exit(0)
  38. settings = dict(
  39. debug=options.debug,
  40. autoescape=options.autoescape,
  41. cookie_secret=options.cookie_secret,
  42. cookie_expires=31,
  43. xsrf_cookies=False,
  44. login_url=options.login_url,
  45. template_path=options.template_path,
  46. july_template_path=options.template_path,
  47. static_path=options.static_path,
  48. static_url_prefix=options.static_url_prefix,
  49. mongo_db=mongo_db,
  50. template_loader=JinjaLoader(TEMPLATES_DIR)
  51. )
  52. #: init application
  53. application = NewJulyApplication(**settings)
  54. application.register_app('app.learn.handlers.core.learn', url_prefix='/learn')
  55. return application
  56. if __name__ == '__main__':
  57. init_options()
  58. parse_config_file(os.path.join(PROJDIR, 'etc/server.conf'))
  59. tornado.options.parse_command_line()
  60. application = create_application()
  61. run_server(application)