run_server.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sqlite3
  2. import time
  3. from bottle import run, response, route, redirect
  4. import connection
  5. import model
  6. import server_controller
  7. import trading_bot
  8. from debug import debug
  9. from server_controller import not_found
  10. if __name__ == '__main__':
  11. print('sqlite3.version', model.db.version)
  12. model.connect()
  13. valid_routes = ['login',
  14. 'register',
  15. 'depot',
  16. 'activate_key',
  17. 'order', 'orders',
  18. 'news',
  19. 'trades',
  20. 'trades_on',
  21. 'orders_on',
  22. 'old_orders',
  23. 'cancel_order',
  24. 'leaderboard',
  25. 'tradables',
  26. 'gift',
  27. 'change_password']
  28. @route('/<path>', method='POST')
  29. def process(path):
  30. start = time.clock()
  31. path = path.strip().lower()
  32. if path not in valid_routes:
  33. print('Processing time:', time.clock() - start)
  34. return not_found()
  35. response.content_type = 'application/json'
  36. method_to_call = getattr(server_controller, path)
  37. try:
  38. expired_orders = model.drop_expired_orders()
  39. trading_bot.notify_expired_orders(expired_orders)
  40. resp = method_to_call()
  41. if response.status_code == 200:
  42. model.connection.commit()
  43. else:
  44. model.connection.rollback()
  45. print('Processing time:', time.clock() - start)
  46. return resp
  47. except sqlite3.IntegrityError as e:
  48. print(e)
  49. model.connection.rollback()
  50. print('Processing time:', time.clock() - start)
  51. return server_controller.internal_server_error('Action violates database constraints.')
  52. @route('/', method='GET')
  53. def process():
  54. redirect('http://koljastrohm-games.com/downloads/orderer_installer.zip')
  55. run(host='0.0.0.0', port=connection.port, debug=debug)
  56. model.connection.close()