eventman_server.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 RootHandler(tornado.web.RequestHandler):
  13. angular_app_path = os.path.join(os.path.dirname(__file__), "angular_app")
  14. @gen.coroutine
  15. def get(self):
  16. with open(self.angular_app_path + "/index.html", 'r') as fd:
  17. self.write(fd.read())
  18. MOCKUP_PERSONS = {
  19. 1: {'name': 'Silvia', 'surname': 'Castellari',
  20. 'email': 'hackinbo.it@gmail.com',
  21. 'id': 1},
  22. 2: {'name': 'Daniele', 'surname': 'Castellari',
  23. 'email': 'hackinbo.it@gmail.com',
  24. 'id': 2},
  25. 3: {'name': 'Mario', 'surname': 'Anglani',
  26. 'email': 'hackinbo.it@gmail.com',
  27. 'id': 3}
  28. }
  29. import datetime
  30. MOCKUP_EVENTS = {
  31. 1: {'title': 'HackInBo 2015', 'begin-datetime': datetime.datetime(2015, 5, 23, 9, 0),
  32. 'end-datetime': datetime.datetime(2015, 5, 24, 17, 0),
  33. 'location': 'Bologna', 'id': 1},
  34. 2: {'title': 'La fiera del carciofo', 'begin-datetime': datetime.datetime(2015, 6, 23, 9, 0),
  35. 'end-datetime': datetime.datetime(2015, 6, 24, 17, 0),
  36. 'location': 'Gatteo a mare', 'id': 2},
  37. }
  38. import json
  39. class ImprovedEncoder(json.JSONEncoder):
  40. def default(self, o):
  41. if isinstance(o, datetime.datetime):
  42. return str(o)
  43. return json.JSONEncoder.default(self, o)
  44. json._default_encoder = ImprovedEncoder()
  45. class PersonsHandler(tornado.web.RequestHandler):
  46. @gen.coroutine
  47. def get(self, id_=None):
  48. if id_ is not None:
  49. self.write({'person': MOCKUP_PERSONS[int(id_)]})
  50. return
  51. self.write({'persons': MOCKUP_PERSONS.values()})
  52. class EventsHandler(tornado.web.RequestHandler):
  53. @gen.coroutine
  54. def get(self, id_=None):
  55. if id_ is not None:
  56. self.write({'event': MOCKUP_EVENTS[int(id_)]})
  57. return
  58. self.write({'events': MOCKUP_EVENTS.values()})
  59. def main():
  60. define("port", default=5242, help="run on the given port", type=int)
  61. define("data", default=os.path.join(os.path.dirname(__file__), "data"),
  62. help="specify the directory used to store the data")
  63. define("debug", default=False, help="run in debug mode")
  64. define("config", help="read configuration file",
  65. callback=lambda path: tornado.options.parse_config_file(path, final=False))
  66. tornado.options.parse_command_line()
  67. application = tornado.web.Application([
  68. (r"/persons/?(?P<id_>\d+)?", PersonsHandler),
  69. (r"/events/?(?P<id_>\d+)?", EventsHandler),
  70. (r"/(?:index.html)?", RootHandler),
  71. (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
  72. ],
  73. template_path=os.path.join(os.path.dirname(__file__), "templates"),
  74. static_path=os.path.join(os.path.dirname(__file__), "static"),
  75. debug=options.debug)
  76. http_server = tornado.httpserver.HTTPServer(application)
  77. http_server.listen(options.port)
  78. tornado.ioloop.IOLoop.instance().start()
  79. if __name__ == '__main__':
  80. main()