api.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import datetime
  2. import json
  3. import tornado.web
  4. from config import web as config
  5. import db.queries
  6. class APIHandler(tornado.web.RequestHandler):
  7. def set_default_headers(self):
  8. self.set_header('Access-Control-Allow-Origin', '*')
  9. self.set_header('Access-Control-Allow-Headers', 'X-Requested-With, X-Request')
  10. def compute_etag(self):
  11. return None
  12. def respond_json(self, data):
  13. self.set_header('Content-Type', 'application/json; charset=UTF-8')
  14. for chunk in json.JSONEncoder(indent='\t').iterencode(data):
  15. self.write(chunk)
  16. self.finish()
  17. def options(self, *args):
  18. return
  19. class SearchHandler(APIHandler):
  20. def get(self):
  21. q = self.get_argument('q')
  22. data = db.queries.search(q)
  23. self.respond_json(data)
  24. class KillListHandler(APIHandler):
  25. def get(self, entity_type, entity_id):
  26. kills = db.queries.kill_list(entity_type, int(entity_id))
  27. self.respond_json(kills)
  28. class KillHandler(APIHandler):
  29. def get(self, kill_id):
  30. kill = db.queries.kill(kill_id)
  31. self.respond_json(kill)
  32. def start():
  33. tornado.web.Application(
  34. handlers=[
  35. (r'/search', SearchHandler),
  36. (r'/(alliance|corporation|character)/(.+)', KillListHandler),
  37. (r'/kill/(.+)', KillHandler),
  38. ],
  39. debug=config.debug,
  40. ).listen(config.api_port)
  41. print('listening on :%d' % config.api_port)