run_server.py 2.0 KB

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