1
1

run_server.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import sqlite3
  2. import connection
  3. import server_controller
  4. import model
  5. from bottle import run, response, route
  6. from server_controller import not_found
  7. from util import debug
  8. if __name__ == '__main__':
  9. print('sqlite3.version', model.db.version)
  10. valid_routes = ['login',
  11. 'register',
  12. 'depot',
  13. 'activate_key',
  14. 'order', 'orders',
  15. 'news',
  16. 'transactions',
  17. 'orders_on']
  18. @route('/<path>', method='POST')
  19. def process(path):
  20. path = path.strip().lower()
  21. if path not in valid_routes:
  22. return not_found()
  23. response.content_type = 'application/json'
  24. method_to_call = getattr(server_controller, path)
  25. try:
  26. model.drop_expired_orders()
  27. resp = method_to_call()
  28. if response.status_code == 200:
  29. model.connection.commit()
  30. else:
  31. model.connection.rollback()
  32. return resp
  33. except sqlite3.IntegrityError as e:
  34. print(e)
  35. model.connection.rollback()
  36. return server_controller.bad_request('Action violates database constraints.')
  37. run(host='localhost', port=connection.port, debug=debug)
  38. model.db.connection.disconnect()