eventman_server.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. """Event Man(ager)
  3. Your friendly manager of attendants at a conference.
  4. """
  5. import os
  6. import tornado.httpserver
  7. import tornado.ioloop
  8. import tornado.options
  9. from tornado.options import define, options
  10. import tornado.web
  11. from tornado import gen
  12. class MainHandler(tornado.web.RequestHandler):
  13. @gen.coroutine
  14. def get(self):
  15. self.redirect('/index.html')
  16. MOCKUP_PERSONS = [
  17. {'name': 'Silvia', 'surname': 'Castellari',
  18. 'email': 'hackinbo.it@gmail.com',
  19. 'id': 1},
  20. {'name': 'Daniele', 'surname': 'Castellari',
  21. 'email': 'hackinbo.it@gmail.com',
  22. 'id': 2},
  23. {'name': 'Mario', 'surname': 'Anglani',
  24. 'email': 'hackinbo.it@gmail.com',
  25. 'id': 3}]
  26. class PersonsHandler(tornado.web.RequestHandler):
  27. @gen.coroutine
  28. def get(self, id_=None):
  29. self.write({'persons': MOCKUP_PERSONS})
  30. def main():
  31. define("port", default=5242, help="run on the given port", type=int)
  32. define("data", default=os.path.join(os.path.dirname(__file__), "data"),
  33. help="specify the directory used to store the data")
  34. define("debug", default=False, help="run in debug mode")
  35. define("config", help="read configuration file",
  36. callback=lambda path: tornado.options.parse_config_file(path, final=False))
  37. tornado.options.parse_command_line()
  38. application = tornado.web.Application([
  39. (r"/persons/?(?P<id_>\d+)?", PersonsHandler),
  40. (r"/", MainHandler),
  41. (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
  42. ],
  43. template_path=os.path.join(os.path.dirname(__file__), "templates"),
  44. static_path=os.path.join(os.path.dirname(__file__), "static"),
  45. debug=options.debug)
  46. http_server = tornado.httpserver.HTTPServer(application)
  47. http_server.listen(options.port)
  48. tornado.ioloop.IOLoop.instance().start()
  49. if __name__ == '__main__':
  50. main()