server.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. model.setup()
  11. valid_routes = ['login', 'register', 'depot', 'activate_key', 'order', 'orders', 'news', 'transactions', 'orders_on']
  12. @route('/<path>', method='POST')
  13. def process(path):
  14. if path not in valid_routes:
  15. return not_found()
  16. response.content_type = 'application/json'
  17. method_to_call = getattr(server_controller, path)
  18. try:
  19. model.drop_expired_orders()
  20. resp = method_to_call()
  21. if response.status_code == 200:
  22. model.connection.commit()
  23. else:
  24. model.connection.rollback()
  25. return resp
  26. except sqlite3.IntegrityError as e:
  27. print(e)
  28. model.connection.rollback()
  29. return server_controller.bad_request('Action violates database constraints.')
  30. run(host='localhost', port=connection.port, debug=debug)
  31. model.db.connection.disconnect()