PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/main.py

https://gitlab.com/forevercat/Project
Python | 60 lines | 51 code | 8 blank | 1 comment | 10 complexity | 6cbe1951f90198744a09848c3639d598 MD5 | raw file
  1. #!/usr/bin/python
  2. import sys
  3. import api
  4. import types
  5. import tornado.ioloop
  6. import tornado.web
  7. import sqlalchemy
  8. from sqlalchemy import create_engine
  9. from sqlalchemy.orm import sessionmaker
  10. from sqlalchemy import func
  11. import util.config
  12. reload(sys)
  13. sys.setdefaultencoding('utf-8')
  14. class ESApplication(tornado.web.Application):
  15. def __init__(self, api_entry, **settings):
  16. handlers = self.load_handlers(api_entry)
  17. username = util.config.get("mysql", "username")
  18. password = util.config.get("mysql", "password")
  19. host = util.config.get("mysql", "host")
  20. database = util.config.get("mysql", "database")
  21. self.engine = create_engine('mysql://%s:%s@%s/%s?charset=utf8' % (username, password, host, database),\
  22. encoding='utf-8', echo=False, pool_size=20, max_overflow=0)
  23. super(ESApplication, self).__init__(handlers, **settings)
  24. def load_handlers(self, m):
  25. handlers = []
  26. if hasattr(m, "__all__"):
  27. for sub_module in m.__all__:
  28. __import__("%s.%s" % (m.__name__, sub_module))
  29. for attr in dir(m):
  30. if attr.startswith("__"):
  31. continue
  32. inst = getattr(m, attr)
  33. if type(inst) == types.ModuleType:
  34. if not inst.__name__.startswith(m.__name__):
  35. continue
  36. else:
  37. handlers += self.load_handlers(inst)
  38. elif type(inst) == types.TypeType:
  39. if inst.__name__.endswith("Handler"):
  40. handlers.append((r"/%s/(.*)" % m.__name__.replace('.', '/'), inst))
  41. handlers.append((r"/%s" % m.__name__.replace('.', '/'), inst))
  42. print "loading handler", "/%s/(.*)" % m.__name__.replace('.', '/')
  43. return handlers
  44. def main():
  45. port = util.config.get("global", "port")
  46. application = ESApplication( api, **{
  47. 'debug': True
  48. })
  49. application.listen(port)
  50. tornado.ioloop.IOLoop.instance().start()
  51. if __name__ == '__main__':
  52. main()