server.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. import os
  3. import cleancss
  4. import tornado.httpclient
  5. import tornado.ioloop
  6. import tornado.web
  7. from config import web as config
  8. import web.api
  9. class BaseHandler(tornado.web.RequestHandler):
  10. def render(self, *args, **kwargs):
  11. kwargs['api_host'] = config.api_host
  12. return super(BaseHandler, self).render(*args, **kwargs)
  13. def render_string(self, *args, **kwargs):
  14. s = super(BaseHandler, self).render_string(*args, **kwargs)
  15. return s.replace(b'\n', b'') # this is like Django's {% spaceless %}
  16. class MainHandler(BaseHandler):
  17. def get(self):
  18. self.render('home.html')
  19. class SearchHandler(BaseHandler):
  20. def get(self):
  21. self.render('search.html')
  22. class KillListHandler(BaseHandler):
  23. def get(self, entity_type):
  24. self.render('kill_list.html')
  25. class KillHandler(BaseHandler):
  26. def get(self):
  27. self.render('kill.html')
  28. class CSSHandler(tornado.web.RequestHandler):
  29. def get(self, css_path):
  30. css_path = os.path.join(os.path.dirname(__file__), 'web', 'static', css_path) + '.ccss'
  31. with open(css_path, 'r') as f:
  32. self.set_header('Content-Type', 'text/css')
  33. self.write(cleancss.convert(f))
  34. if __name__ == '__main__':
  35. tornado.web.Application(
  36. handlers=[
  37. (r'/', MainHandler),
  38. (r'/search', SearchHandler),
  39. (r'/(alliance|corporation|character)/.+', KillListHandler),
  40. (r'/kill/.+', KillHandler),
  41. (r'/(css/.+)\.css', CSSHandler),
  42. ],
  43. template_path=os.path.join(os.path.dirname(__file__), 'web/templates'),
  44. debug=config.debug,
  45. ).listen(config.port)
  46. web.api.start()
  47. print('listening on :%d' % config.port)
  48. tornado.ioloop.IOLoop.instance().start()